简体   繁体   English

使用php上传文件时出错

[英]error uploading file using php

I have a problem with my upload file function. 我的上传文件功能有问题。 I'm following this website to create the upload form to upload a text file and i just modify it a little. 我正在这个网站上创建上传表格以上传文本文件,我只是对其进行了一点修改。 Here's the code: 这是代码:

upload_form.php : 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 : 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?');
}

The problem is the feedback always showing error 问题是反馈总是显示错误

die('error uploading File!'); die('上传文件出错!');

I think the problem isn't from the code, because I can't found the php.ini in the same path that phpinfo showed me. 我认为问题不在于代码,因为我找不到phpinfo向我显示的路径中的php.ini。 I already set the folder (impfile) to be writeable too. 我已经将文件夹(impfile)设置为可写了。

Can someone show me where did I do wrong in the code? 有人可以告诉我我在代码中哪里做错了吗? Or maybe the php.ini? 还是php.ini? If the php.ini is the problem, how can I add the php.ini? 如果php.ini有问题,如何添加php.ini? Or maybe there's something else? 也许还有别的东西?

Every help would be appreciated. 每一个帮助将不胜感激。 Thank you. 谢谢。

Try write impfile/ without first slash. 尝试写impfile/而不用第一个斜杠。 This could be help if the script or directory place in nonroot directory of domain. 如果脚本或目录位于域的非根目录中,则可能会有所帮助。

Try use is_uploaded_file($_FILES['FileInput']['tmp_name']) or/and $_FILES['FileInput']['size']>0 conditions for additional check. 尝试使用is_uploaded_file($_FILES['FileInput']['tmp_name'])或/和$_FILES['FileInput']['size']>0条件进行其他检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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