我的上传文件功能有问题。我跟随this网站创建上传表单以上传文本文件,我只是稍微修改一下。这是代码:
upload_form.php:
//the jquery script is still the same with the website
.....
echo "
<form action='processupload.php' method='post' enctype='multipart/form-data' id=MyUploadForm>
<input name='FileInput' id='FileInput' type='file' />
<input type='submit' id='submit-btn' value='Upload' />
<img src='images/ajax-loader.gif' id='loading-img' style='display:none;' alt='Please Wait'/>
</form>
<div id='progressbox' ><div id='progressbar'></div ><div id='statustxt'>0%</div></div>
<div id='output'></div>
";
processupload.php:
<?php
if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
############ Edit settings ##############
//$UploadDirectory = '/impfile'; //specify upload directory ends with / (slash)
##########################################
//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}
//Is file size is less than allowed size.
if ($_FILES["FileInput"]["size"] > 5242880) {
die("File size is too big!");
}
//allowed file type Server side check
switch(strtolower($_FILES['FileInput']['type']))
{
case 'text/plain':
break;
default:
die('Unsupported File!'); //output error
}
$File_Name = $_FILES['FileInput']['name'];
if(move_uploaded_file($_FILES['FileInput']['tmp_name'], "/impfile/".$File_Name ))
{
die('Success! File Uploaded.');
}else{
die('error uploading File!');
}
}
else
{
die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}
问题是反馈始终显示错误
死亡(&#39;错误上传文件!&#39;);
我认为问题不在于代码,因为我无法在phpinfo向我展示的路径中找到php.ini。我已经将文件夹(impfile)设置为可写。
有人能告诉我代码中哪里出错了吗?或者也许是php.ini?如果php.ini是问题,我该如何添加php.ini?或者还有其他的东西?
我们将不胜感激。谢谢。
答案 0 :(得分:2)
在没有第一次斜杠的情况下尝试写impfile/
。如果脚本或目录放在域的非根目录中,这可能会有所帮助。
尝试使用is_uploaded_file($_FILES['FileInput']['tmp_name'])
或/和$_FILES['FileInput']['size']>0
条件进行额外检查。