1

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. The error is "application/octet-stream". what should I do?

The data is very simple, stored in .csv format

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

Error output: "-- 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() . "\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";
    }
}
?>
3
  • 2
    The ['type'] parameter is set by the sending client. If you're getting app/octet, then that's what the browser is supplying.
    – Marc B
    Aug 17, 2012 at 18:11
  • ??????????????? sorry, not sure what you mean and what you are suggesting. Would you please suggest course of action and fix?
    – FongYu
    Aug 17, 2012 at 18:16
  • @Marc B, if you are suggesting to use different browsers, I've tried that but still doesn't load. Many web discussions suggested that but nothing conclusive...
    – FongYu
    Aug 17, 2012 at 18:24

1 Answer 1

1

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\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";
//}
}
7
  • still only prints the same error "- CSV file to load: Invalid type: application/octet-stream"
    – FongYu
    Aug 17, 2012 at 18:22
  • Allright, so you load the CSV in another file? There should be the same code in that file, comment out the line(s) where the type is checked (should be about the same as in my example). Aug 17, 2012 at 18:25
  • Yes, you should check the file where you load the CSV, and allow application/octet-stream. if( $file["type"] == "application/vnd.ms-excel" || $file["type"] == "text/plain" || $file["type"] == "application/octet-stream"){ [...] Aug 17, 2012 at 18:31
  • Thanks John It works now! I am uploading the data from CSV to a bar graph. But would you please explain why it worked? Because the changes were just doing the check of file attributes aren't they? Even the fgetcsv() is validating the file right? Is the key in moving the uploaded file to a new location? I didn't specify it, so i guess it stores in some temp default folder. But what does this do and why does it resolve the application/octet-stream error?
    – FongYu
    Aug 17, 2012 at 18:59
  • application/octet-stream is the type of your file. This is set by the browser of the client. fgetcsv() is just to check if your CSV file can be loaded as CSV to check is it the the right filetype, as substitute for the lines I removed. Aug 17, 2012 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.