dongtazu3080 2012-08-17 18:08 采纳率: 100%
浏览 1398
已采纳

文件上传错误“application / octet-stream”

我不是很精通 PHP,我用以下的PHP代码上传一个csv文件却怎么都实现不了。我已经修复了上传最大尺寸等相关属性的问题。其在我的本地网站上运行良好,但在沙盒网站上就不行了。错误:“ application / octet-stream”。 我该怎么办?

数据非常简单,以.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 file to load: Invalid type: 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() . "
";
    $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"] . "
";   
            }
        }
        else 
        {   
            echo "Invalid extension: " . $file["name"]."
";
        }
    }
    else
    {
        echo "Invalid type: " . $file["type"] . "
";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douzi0609 2012-08-17 18:17
    关注

    Try the last piece of code like this, as $file['type'] is set by the client, not the server. Should be fine this way. Make sure to upload the files in a location unreachable to public.

    <?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
    ";  //should never happen  
                }
            }
            else
            {
                echo "Invalid file size: " . $file["size"] . "
    ";   
            }
        }
        else 
        {   
            echo "Invalid extension: " . $file["name"]."
    ";
        }
    }
    //else
    //{
    //    echo "Invalid type: " . $file["type"] . "
    ";
    //}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?