0

I can't really read PHP but I'm trying to upload a csv file in my page through php and got the following error. I know there are many threads that talks about this but I don't quite understand what they mean. Looking for an answer if someone could explain to me what's going on and provide a fix.

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\search\php\loader\csvFileUploader1.php on line 9
failure to upload the file >>> Error code: 1

Here's the PHP

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


?>
kapa
  • 77,694
  • 21
  • 158
  • 175
FongYu
  • 767
  • 4
  • 15
  • 24
  • [The documentation of `end()`](http://php.net/manual/en/function.end.php) tells about the requirement of `the array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.`. – kapa Aug 15 '12 at 22:12
  • I have been able to upload other .csv files into my page before using the same PHP file tho, how come? – FongYu Aug 15 '12 at 22:18
  • That's a good question. Also, to get the extension of a file, you could simply use `$extension = pathinfo($file["name"], PATHINFO_EXTENSION);`. Much cleaner and no tricks involved. – kapa Aug 15 '12 at 22:22
  • possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – Lorenz Meyer Jun 21 '14 at 08:37

1 Answers1

4

You can't pass along the return value of a function to end().

Make it:

$arrayVar = explode(".", $file["name"]);
$extension = end($arrayVar);

You would have the same issue with other PHP functions. For example empty(someFunction()) would not work.

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • ok I changed to that but now getting a new error "-- CSV file to load: failure to upload the file >>> Error code: 1
    "
    – FongYu Aug 15 '12 at 22:15
  • Error codes are explained here: http://php.net/manual/en/features.file-upload.errors.php – Tchoupi Aug 15 '12 at 22:17
  • Note in PHP 5.5 you should be able things other than variables inside an empty. Should be quite useful! It would be great if they did something similar for end/reset to – jleck Aug 15 '12 at 23:00