dongxi1879 2013-01-15 19:15
浏览 128
已采纳

上传大于5MB的文件

I tried to create an application wich allows me to upload files larger then 5 MB but I always get an error that says that the file is to large. I modified php.ini upload_max_filesize and post_max_size and set it to 1GB but it did not work.

This is my code:

if(is_uploaded_file($_FILES['pdf']['tmp_name'])&&($_FILES['pdf']['error'] == UPLOAD_ERR_OK)){
    $file = $_FILES['pdf'];
    $size = round($file['size']/10485760);
    if($size > 10485760){
        echo 'The uploaded file was to large.';
    }

    if(($file['type'] != 'application/pdf') && (substr($file['name'], -4) !='.pdf')){
        echo 'The uploaded file was not a PDF.';
    }

    if(!array_key_exists('pdf', $add_pdf_errors)){
        $tmp_name = sha1($file['name'].uniqid('', TRUE));
        $dest = '../pdfs/'.$tmp_name.'_tmp';

        if(move_uploaded_file($file['tmp_name'], $dest)){
            $_SESSION['pdf']['tmp_name'] = $tmp_name;
            $_SESSION['pdf']['size'] = $size;
            $_SESSION['pdf']['file_name'] = $file['name'];
            echo '<h4>The file has been uploaded!</h4>';
        }else{
            trigger_error('The file coul not be moved.');
            unlink($file['tmp_name']);
        }
    }
}else{
    switch ($_FILES['pdf']['error']){
        case 1:
        case 2:
            echo 'The uploaded file was to large.';
            break;
        case 3:
            echo 'The file was only partially uploaded.';
            break;
        case 6:
        case 7:
        case 8:
            echo 'The file could not be uploaded to a system error.';
            break;
        case 4:
        default:
            echo 'No file was uploaded.';
            break;
    }
}

How can I resolve this problem?

  • 写回答

2条回答 默认 最新

  • doukong1897 2013-01-15 19:27
    关注
    $size = round($file['size']/10485760);
    if($size > 10485760){
        echo 'The uploaded file was to large.';
    }
    

    This logic does not appear to be correct. Comparison is not accurate like that. Try

    $size = $file['size'];
    if($size > 10485760){
        echo 'The uploaded file was to large.';
    }
    

    Also check if 'The uploaded file was to large.' is being displayed from within your IF or ELSE part? because both display same message in certain situation, So first we have to find out where does it get displayed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)