Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Undefined index

Featured Replies

I was trying this code for uploading files into the folder "upload/"

<?php

 

 

if ($_FILES["file"]["error"] > 0)

{

echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "<br />";

echo "Type: " . $_FILES["file"]["type"] . "<br />";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

 

if (file_exists("upload/" . $_FILES["file"]["name"]))

{

echo $_FILES["file"]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

}

}

 

 

?>

 

 

But I get some kind of "Undefined index:" blah blah blah... many times and nothing happens.

 

What should I do?

 

Thanks in advance.

  • Author

I presume there should be no problem in my html document. Anyway here's the code.

 

<html>

<head>

<title> Upload </title>

</head>

<body>

<p>

<form action="upload.php" method="post" enctype="mutlipart/form-data">

<input type="file" name="file" id="file"/>

<br/>

<input type="submit" name="submit" value="submit"/>

</form>

</p>

</body>

</html>

I presume there should be no problem in my html document. Anyway here's the code.

 

<html>

<head>

<title> Upload </title>

</head>

<body>

<p>

<form action="upload.php" method="post" enctype="mutlipart/form-data">

<input type="file" name="file" id="file"/>

<br/>

<input type="submit" name="submit" value="submit"/>

</form>

</p>

</body>

</html>

 

to prevent undefined indexes i'd create a function for files like this

 

function doFile($file,$type){
if(isset($_FILES[$file][$type])){
return $_FILES[$file][$type];
}
}
//Then use like this 
doFile('file','type');
doFile('file','name');
doFile('file','size');

//ect..

So where are you checking the file extensions? or are you allowing anyone to upload anything via the php script not a very clever plan....

 

Im not going to write this code for you but for example.

 

$allowedformats = array('.html',
   '.sql',
   '.gif',
   );

 

Then link it up with the rest of the code and ask it to check in the array ect.

 

 

Thanks Cobra

Nope not being critical and as stated it was an "example" of what could be done to improve the current bit of code, Is this not a community where people suggest things and help to improve what others post? ;) back at you

Nope not being critical and as stated it was an "example" of what could be done to improve the current bit of code, Is this not a community where people suggest things and help to improve what others post? ;) back at you

 

 

I have to agree, if he isn't told about these things now, while he's learning then it will come back and bite him on the ass later. Cobra did say it was only an example and without seeing what his code would do with that array, rallport, you really can't say how effective it will be.

I have to agree, if he isn't told about these things now, while he's learning then it will come back and bite him on the ass later. Cobra did say it was only an example and without seeing what his code would do with that array, rallport, you really can't say how effective it will be.

rallport is referring to the fact that file types can be faked, its a security issue, so yes im pretty sure as a developer he can say how effective it will be

Edited by webdesigner93

How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array.

How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array.

 

Grabs another can of stella and gets comfortable :rolleyes:

How can he say how effect code is that he cannot see? No matter what file extensions you are going to allow or reject and no matter how you are going to check them you are always going to have to tell the script the ones to allow/reject. You CANNOT tell anything about how effective the script is based on that array.

 

Exactly I posted a snippet not my full upload script. As I have stated it was an example on how he could improve his script, With a simple array it can be improved there for I shared my thoughts on how to improve the above code. As a way to contribute something to this community Nothing more.

 

Cheers

 

Cobra

Good point, shutting up now :)

 

Directed at the thread, not to you personally :)

Already got a fosters ran out of stella so its all good :)

 

I'm eating pot noddle - guess that will have to do! (to partially save my "man" status, it IS a bombay badboy one!)

I'm eating pot noddle - guess that will have to do! (to partially save my "man" status, it IS a bombay badboy one!)

 

Lol :rofl: least you got a pot noddle all I have is a crisp I coded last week :/

  • 2 weeks later...

I'm with webdesigner93 on this one- Checking that the array value is set before trying to process it. I even tend to wrap that up with an

 
if (!isset($_FILES))
{
  echo "No file information found";
}
else
{
  ...
}

 

first to ensure the mechanism is working, but that might be a bit of an over kill!

I'm with webdesigner93 on this one- Checking that the array value is set before trying to process it. I even tend to wrap that up with an

 
if (!isset($_FILES))
{
  echo "No file information found";
}
else
{
  ...
}

 

first to ensure the mechanism is working, but that might be a bit of an over kill!

 

 

php file upload code

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.