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.

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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制