【发布时间】: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);。更干净,不涉及任何技巧。