douba2011 2014-06-13 07:29
浏览 34
已采纳

使用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 :

//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?');
}

The problem is the feedback always showing error

die('error uploading File!');

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. I already set the folder (impfile) to be writeable too.

Can someone show me where did I do wrong in the code? Or maybe the php.ini? If the php.ini is the problem, how can I add the php.ini? Or maybe there's something else?

Every help would be appreciated. Thank you.

  • 写回答

1条回答 默认 最新

  • dongzhui4927 2014-06-13 07:42
    关注

    Try write impfile/ without first slash. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装