【问题标题】:"Only variables should be passed by reference" error“只有变量应该通过引用传递”错误
【发布时间】:2012-08-15 22:05:05
【问题描述】:

我无法真正阅读 PHP,但我试图通过 php 在我的页面中上传一个 csv 文件并收到以下错误。我知道有很多线程在谈论这个,但我不太明白它们的意思。如果有人可以向我解释发生了什么并提供解决方案,请寻找答案。

严格标准:只能通过引用传递变量 在 C:\xampp\htdocs\search\php\loader\csvFileUploader1.php 上 第9
上传文件失败>>>错误码: 1

这是 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"]);
        }       
}


?>

【问题讨论】:

  • The documentation of end()讲述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.的需求。
  • 在使用同一个 PHP 文件之前,我已经能够将其他 .csv 文件上传到我的页面中,为什么?
  • 这是个好问题。此外,要获取文件的扩展名,您可以简单地使用$extension = pathinfo($file["name"], PATHINFO_EXTENSION);。更干净,不涉及任何技巧。

标签: php csv


【解决方案1】:

您不能将函数的返回值传递给end()

制作:

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

您在使用其他 PHP 函数时也会遇到同样的问题。例如empty(someFunction()) 将不起作用。

【讨论】:

  • 好的,我改成这样,但现在出现新错误“--要加载 CSV 文件:上传文件失败>>>错误代码:1
  • 注意在 PHP 5.5 中你应该可以在一个空的变量之外的东西。应该很有用!如果他们为结束/重置做类似的事情会很棒
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2017-04-03
  • 2011-10-16
相关资源
最近更新 更多