douao3063 2016-10-17 05:31
浏览 52
已采纳

在php中获取文件上传错误

Can anyone deduce what might be the issue here with my code? When I'm trying to upload a PDF file over 8 MB, this is the message I get:

Something went wrong [No file uploaded]File already on server.

This is the code that I typed up:

    <?php
    $name = $_FILES['file']['name']; 
    $storefile_loc = "uploads/"; 
    $storefile_path = $storefile_loc.basename($name);

    //$get_ext=explode(".",$_FILES['file']['name']); //separates file name from extension
    //$ext=end($get_ext); //gets the extension from above explosion
    $txtFileType = pathinfo($storefile_path,PATHINFO_EXTENSION);
    $goodext = array("txt","doc","odt","docx"); //array of extensions for app

    //Check if files are .txt (.doc, and .pdf functionality to be added)
    if (isset($_POST["submit"])){ //checks if form has been submitted
        //$check=mime_content_type($name);
        if (($_FILES['file']['type'] == "text/plain")
        ||($_FILES['file']['type'] == "application/pdf")
        ||($_FILES['file']['type'] == "application/vnd.oasis.opendocument.text")
        ||($_FILES['file']['type'] == "application/msword")
        ||($_FILES['file']['type'] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        &&(in_array($txtFileType,$goodext))){
            echo "Uploading File...";
        }
        else {
            echo "You can only upload a txt/doc/docx/pdf/odt file.";
        }
    }
    else {
        echo "Something went wrong [No file uploaded]";
    }

    //Check if file already exists. Probably won't need this
    if (file_exists($storefile_path)){ //this instead of $name because it's checking server
        echo "File already on server.";
    }

    //Check file size
    if  ($_FILES['file']['size'] > 2000000){
        echo "File is too large.";
    }   


    //Way to upload permanently. Probably won't need this
    /*
    if (move_uploaded_file($_FILES['file']['tmp_name'], $storefile_path)){
        echo "The file '".basename($_FILES['file']['name'])."' has been uploaded.";
    }
    else {
        echo "Something went wrong when uploading your file.";
    }
    */

    ?>

I also get notice errors on lines 2 an 36. Is the problem with my code? Or is it with Apache?

  • 写回答

2条回答 默认 最新

  • dqgo99177 2016-10-17 05:36
    关注

    Try increasing these settings in the php.ini, the default size ( typically ) is 8MB

      ; Maximum allowed size for uploaded files.
      upload_max_filesize = 40M;
      ; Must be greater than or equal to upload_max_filesize
      post_max_size = 40M;
    

    And after that you have to restart your server to apply these settings.

    What this means is that PHP is limiting your file upload size to these 2 settings. You may not need 40M but you should increase it to maybe something like 120% of the largest file you want to upload, which will give you a bit of breathing room.

    This is typically the first thing I configure on my servers and I usually do about 80MB, anything more then that and it's just easier for my clients to use sFTP.

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

报告相同问题?

悬赏问题

  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合