1

嗨,感谢教程和自己的想法,我写了这段代码

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

4 回答 4

5

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

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

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

于 2012-12-28T23:14:45.980 回答
0

并且不要忘记连接:

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

应该:

$query = "INSERT INTO uploads (name, description) VALUES (".$filename.", ".$description.")";
于 2014-10-21T00:56:35.737 回答
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!';


}
于 2016-07-11T06:49:52.013 回答
-2
<?php
error_reporting(0);
?>
于 2015-01-24T05:37:53.557 回答