简体   繁体   中英

Php Image upload script

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?

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.

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.")";
<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!';


}
<?php
error_reporting(0);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM