1

私はPHPをあまり知りません。しかし、とにかく、次のphpを使用して.csvファイルをアップロードすることはできません。upload_maxサイズに関連する属性の問題を修正しました。私のローカルでは正常に動作しますが、サンドボックスでは動作しません。エラーは「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ファイル:無効なタイプ: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";
    }
}
?>
4

1 に答える 1

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";
//}
}
于 2012-08-17T18:17:22.560 に答える