【问题标题】:file upload error "application/octet-stream"文件上传错误“应用程序/八位字节流”
【发布时间】:2012-08-14 05:01:12
【问题描述】:

我对 PHP 了解不多。但无论如何,我无法使用以下 php 上传 .csv 文件。我已经解决了与 upload_max 大小相关的属性的问题。在我的本地运行良好,但在沙箱上运行良好。错误是“应用程序/八位字节流”。我该怎么办?

数据很简单,以.csv格式存储

27589   16990   161.7000095 0.838494
27589   17067   161.7000095 0.838494
27820   17144   315.7000095 0.859458
27820   17221   315.7000095 0.859458
27820   17606   315.7000095 0.866033
27820   17683   315.7000095 0.866033

错误输出:“--要加载的 CSV 文件:无效类型:application/octet-stream”

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);

// 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 strict standard 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"){
        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";
    }
}
?>

【问题讨论】:

  • ['type'] 参数由发送客户端设置。如果您正在获取 app/octet,那么这就是浏览器提供的内容。
  • ???????????????对不起,不知道你的意思和你的建议。请您提出行动方案并进行修复好吗?
  • @Marc B,如果您建议使用不同的浏览器,我已经尝试过了,但仍然无法加载。许多网络讨论表明,但没有结论......

标签: php file-upload upload


【解决方案1】:

尝试这样的最后一段代码,因为 $file['type'] 是由客户端设置的,而不是服务器。这种方式应该没问题。确保将文件上传到公众无法访问的位置。

<?php // 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"){
    if( isAllowedExtension($file["name"]) )
    {
        if( $file["size"] < 200000000 )
        {
            //try to read the first line with a csv reader

             $handle = fopen( $file["tmp_name"], "r");
            if( $data = fgetcsv($handle)) !== false) {
             if( count( $data ) > 1 ){ //Set 1 to the number of fields - 1

               move_uploaded_file($file["tmp_name"], $newFileLoc);
               echo "|".$path;//"filePath : " . $newFileLoc;
             }
              else{
               echo "Error: Cannot load file, not a CSV file";   
             }

            }else{
             echo "Error: File not uploaded\n";  //should never happen  
            }
        }
        else
        {
            echo "Invalid file size: " . $file["size"] . "\n";   
        }
    }
    else 
    {   
        echo "Invalid extension: " . $file["name"]."\n";
    }
}
//else
//{
//    echo "Invalid type: " . $file["type"] . "\n";
//}
}

【讨论】:

  • 仍然只打印相同的错误“-要加载的 CSV 文件:无效类型:application/octet-stream”
  • 好的,所以您将 CSV 加载到另一个文件中?该文件中应该有相同的代码,注释掉检查类型的行(应该与我的示例大致相同)。
  • 是的,您应该检查加载 CSV 的文件,并允许 application/octet-stream。 if( $file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain" || $file["type"] == "application/octet-stream"){ [...]
  • 感谢 John 现在可以使用了!我正在将数据从 CSV 上传到条形图。但是你能解释一下它为什么起作用吗?因为更改只是在检查文件属性,不是吗?甚至 fgetcsv() 正在验证文件对吗?将上传的文件移动到新位置的关键是什么?我没有指定它,所以我猜它存储在一些临时默认文件夹中。但这有什么作用,为什么它会解决 application/octet-stream 错误?
  • application/octet-stream 是您的文件类型。这是由客户端的浏览器设置的。 fgetcsv() 只是检查您的 CSV 文件是否可以加载为 CSV 以检查它是否是正确的文件类型,以替代我删除的行。
猜你喜欢
  • 2022-10-11
  • 2012-11-17
  • 1970-01-01
  • 2016-11-17
  • 2019-08-24
  • 1970-01-01
  • 2019-05-13
  • 2011-04-30
  • 1970-01-01
相关资源
最近更新 更多