我的上传文件中有此错误。语法是正确的,问题出在哪里?
错误:
is_uploaded_file()期望参数1为字符串,数组在第34行的C:\ Program Files \ EasyPHP-5.3.5.0 \ www \ bena_website \ admin \ variables \ upload_img.php中给出
<h3>Please Choose a File and click Submit</h3>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="userfile[]" type="file" />
<input type="submit" value="Submit" />
</form>
<?php
// check if a file was submitted
if(!isset($_FILES['userfile'])) {
echo '<p>Please select a file</p>';
}
else
{
print_r( $_FILES );
try {
upload();
// give praise and thanks to the php gods
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e) {
echo $e->getMessage();
echo 'Sorry, could not upload file';
}
}
// the upload function
function upload(){
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
// check the file is less than the maximum file size
if($_FILES['userfile']['size'] < $maxsize)
{
// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
// $imgData = addslashes($_FILES['userfile']);
// get the image info..
$size = getimagesize($_FILES['userfile']['tmp_name']);
// put the image in the db...
// database connection
mysql_connect("localhost", "root", "") OR DIE (mysql_error());
// select the db
mysql_select_db ("bena") OR DIE ("Unable to select db".mysql_error());
// our sql query
$sql = "INSERT INTO testblob
( image_id , image_type ,image, image_size, image_name)
VALUES
('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";
// insert the image
if(!mysql_query($sql)) {
echo 'Unable to upload file';
}
}
}
else {
// if the file is not less than the maximum allowed, print an error
echo
'<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.'</div>
<div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
<hr />';
}
}
?>
答案 0 :(得分:7)
在upload()函数的开头发出要学习的$ _FILES数组的结构,结构内部是什么:
print_r( $_FILES );
如其中一条评论中所述,如果你有一个像
这样的表格<form ... method="post" enctype="multipart/form-data">
<input type="file" name="userfile[0]" />
<input type="file" name="userfile[1]" />
...
</form>
或
<form ... method="post" enctype="multipart/form-data">>
<input type="file" name="userfile[]" />
<input type="file" name="userfile[]" />
...
</form>
您需要访问此
$_FILES[ 'userfile' ][ 'tmp_name' ]
就像一个数组:
$_FILES['userfile'][ 'tmp_name' ][ 0 ]
$_FILES['userfile'][ 'tmp_name' ][ 1 ]
答案 1 :(得分:0)
您还可以查看$_FILES['userfile']['error']
是否与零不同。
例如,如果它是2,则文件大小大于HTML中设置的大小。
有关错误消息的更多信息,请选中http://php.net/manual/en/features.file-upload.errors.php