Php图片上传脚本

时间:2012-12-28 23:10:17

标签: php upload

你好教程和自己的hink我写了这段代码

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.';
}
}

我收到了这个错误;

  

注意:未定义的索引:第16行的C:\ WebServer \ htdocs \ PicSide \ admin \ addimage.php中的用户文件

如何修复任何人都知道的?上传服务器是否正确?

4 个答案:

答案 0 :(得分:5)

基本上,您的错误意味着您的表单中没有以下(或类似的)元素:

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

其中重要的部分是实际的name="userfile",它指定了$_FILES数组中的键。

答案 1 :(得分:0)

不要忘记连续:

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

应该是:

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

答案 2 :(得分: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!';


}

答案 3 :(得分:-2)

<?php
error_reporting(0);
?>