douzha8489 2012-08-17 17:25
浏览 42
已采纳

在Apache上传错误

I don't really know much PHP. But anyways I am not able to upload a .csv file using the following php. I have fixed the problem with upload_max size related attributes. Works fine on my local but not on sandbox. Is it file type problem?

I can only get it to print something like this... which is not helpful at all. "-- CSV file to load: >>> NOT a file valid: 1 "

where should I fix? How do I print a complete PHP error message instead of just the value "1" which I'm not sure what it corresponds to? ie... output more useful print like this "UPLOAD_ERR_INI_SIZE: 1"?

<?php
// using upload at click from http://code.google.com/p/upload-at-click/
// FileData is the name for the input file

$file_result = "";
$file = $_FILES['Filedata'];

$allowedExtensions = array("csv", "txt");
$arrayVar = explode(".", $file["name"]);
$extension = end($arrayVar);

//commented out for “Only variables should be passed by reference” error
//$extension = end(explode(".", $file["name"]));


function isAllowedExtension($fileName) {
    global $allowedExtensions;
    return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($file["error"] > 0){
    echo "failure to upload the file >>> ". "Error code: ".$file["error"]."<br>";
}else{
    //echo " >>> CURRENT DIR: ".getcwd() . "
";
    $workDir = getcwd();

    $dir = substr($workDir, 0, -10);
    $path = $file["name"];
    $newFileLoc = $dir.$path;

    $file_result.=
    "<br>     Upload: " . $file["name"] . "<br>" .
    "     Type: " . $file["type"] . "<br>" .
    "     Size: " . $file["size"] . "<br>" .
    "     file uploaded to: ".$newFileLoc."<br>";

    // txt - text/plain
    // rtf - application/msword
    // dat/obj - application/octet-stream
    // csv - application/vnd.ms-excel
    // maximum 200 MB file - 200,000,000 k

    if (    ($file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain")
            && isAllowedExtension($file["name"])
            && ($file["size"] < 200000000)
        )
        {   
            move_uploaded_file($file["tmp_name"], $newFileLoc);
            //echo $file_result.=" >>> File uploaded successfull!!";
            echo "|".$path;//"filePath : " . $newFileLoc;

        }else{
            echo " >>> NOT a file valid: ". isAllowedExtension($file["name"]);
        }       
}
?>

This is the line that was added as suggested by another user to catch the error correctly. Please let me know if that's right sorry i don't know much PHP at all. Anyways, the error printed is just "-- CSV file to load: >>> NOT a file valid: 1 "

<?php
// using upload at click from http://code.google.com/p/upload-at-click/
// FileData is the name for the input file

ini_set('display_errors', 1); error_reporting(E_ALL);

$file_result = "";
$file = $_FILES['Filedata'];

$allowedExtensions = array("csv", "txt");
$arrayVar = explode(".", $file["name"]);
$extension = end($arrayVar);
  • 写回答

1条回答 默认 最新

  • doubu1970 2012-08-17 17:30
    关注

    The block in question is the following:

    if( ( $file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain" ) && 
        isAllowedExtension($file["name"]) && 
        ( $file["size"] < 200000000 ) )
    {   
        move_uploaded_file($file["tmp_name"], $newFileLoc);
    
        //echo $file_result.=" >>> File uploaded successfull!!";
    
        echo "|".$path;//"filePath : " . $newFileLoc;
    }
    else
    {
        echo " >>> NOT a file valid: ". isAllowedExtension($file["name"]);
    }
    

    Note that you're hitting your else echo, and the '1' is because isAllowedExtension() is returning a boolean, which is true, which is denoted as '1' (as opposed to '0') when you print it.

    One of your conditions in the if there are failing. I'd separate them out a bit, and see which in particular is.

    EDIT For example:

    if( ( $file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain" ) )
    {
        if( isAllowedExtension($file["name"]) )
        {
            if( $file["size"] < 200000000 )
            {
                move_uploaded_file($file["tmp_name"], $newFileLoc);
    
                echo "|".$path;//"filePath : " . $newFileLoc;
            }
            else
            {
                echo "Invalid file size: " . $file["size"] . "
    ";   
            }
        }
        else
        {
            echo "Invalid extension: " . $file["name"] . "
    ";   
        }    
    }
    else
    {
        echo "Invalid type: " . $file["type"] . "
    ";
    }
    

    Alternatively, you can just print_r() $file and see what it's values are.

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?