1

Hi thx to tutorials and own hinking I wrote this code

if(isset($_POST['upload'])) {

$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';
$description = $_POST['imgdesc'];

$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');

if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');

if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
   $query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)"; 
   mysql_query($query);

echo 'Your file upload was successful!';


} else {
     echo 'There was an error during the file upload.  Please try again.';
}
}

I'm getting this error;

Notice: Undefined index: userfile in C:\WebServer\htdocs\PicSide\admin\addimage.php on line 16

How to fix it anyone know? And is this correct to upload o server?

8
  • can you post your html form, we need to see that as well? also notices are not exactly errors, errors stops the rest of the script from running whereas notices - depending on the server environment - do not. Commented Dec 28, 2012 at 23:11
  • Probably the file input element name is not userfiles Commented Dec 28, 2012 at 23:12
  • 1st Thank you for reply, I noticed I didn't name my input by "userfile" dummy me, but otherwise is this code correct? Commented Dec 28, 2012 at 23:13
  • @creminsn, Notices are not errors? I wonder why it is an option in PHP error reporting? =oP It is an error, just a less severe error. Notices should be fixed as having them means the code is not properly being done to account for variables not existing, which does not make robust code. Commented Dec 28, 2012 at 23:18
  • You shouldn't be telling anyone to CHMOD directories to 777. CHMODing to 777 is a very amateurish way to get around actually dealing with user file permissions in an appropriate manner. Commented Dec 28, 2012 at 23:18

4 Answers 4

5

Basically, what your error means is that you don't have the following (or somewhat similar) element in your form:

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

The important part in this is the actual name="userfile" which specifies the key in the $_FILES array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I actually found it out 1min after posting this, I noticed to late, sorry for unnecessary post
@Piggie, I personally would mark ninetwozero's as the solution.
0

And don't forget to concat:

$query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)";

Should be:

$query = "INSERT INTO uploads (name, description) VALUES (".$filename.", ".$description.")";

Comments

0
<form>
<input type="file" name="userfile" />
</form>

$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';
$description = $_POST['imgdesc'];
$filename = $_FILES['userfile']['name'];
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
   $query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)"; 
   mysql_query($query);

echo 'Your file upload was successful!';


}

Comments

-2
<?php
error_reporting(0);
?>

1 Comment

Funny. Great solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.