1

我真的不太了解PHP。但无论如何,我无法使用以下 php 上传 .csv 文件。我已经解决了与 upload_max 大小相关的属性的问题。在我的本地运行良好,但在沙箱上运行良好。是文件类型问题吗?

我只能让它打印这样的东西......这根本没有帮助。“--要加载的 CSV 文件:>>> 不是有效的文件:1”

我应该在哪里修复?如何打印完整的 PHP 错误消息,而不仅仅是我不确定它对应的值“1”?即...输出更有用的打印,例如“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() . "\n";
    $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"]);
        }       
}
?>

这是其他用户为正确捕获错误而添加的行。请让我知道这是否正确对不起我根本不了解 PHP。无论如何,打印的错误只是“--要加载的 CSV 文件:>>> 不是有效的文件: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);
4

1 回答 1

1

有问题的块如下:

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"]);
}

请注意,您正在点击 else echo,而“1”是因为isAllowedExtension()正在返回一个布尔值,这是真的,当您打印它时,它表示为“1”(而不是“0”)。

你在那里的条件之一if是失败的。我会把它们分开一点,看看哪个特别好。

编辑例如:

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"] . "\n";   
        }
    }
    else
    {
        echo "Invalid extension: " . $file["name"] . "\n";   
    }    
}
else
{
    echo "Invalid type: " . $file["type"] . "\n";
}

或者,你可以print_r() $file看看它的价值是什么。

于 2012-08-17T17:30:55.730 回答