【发布时间】:2012-08-17 00:15:00
【问题描述】:
我对 PHP 了解不多。但无论如何,我无法使用以下 php 上传 .csv 文件。该文件小于 5MB。这是怎么回事?总共有 79,500 行记录,只有四列,简单的值如下:
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
我对文件进行了剖析,上传了多达 60,000 行数据并炸掉了,即 1.93MB。除此之外的任何事情都行不通。错误代码为“--CSV file to load: failure to upload the file >>> Error code: 1
”错误代码1。
另外,我如何打印完整的 PHP 错误消息,而不仅仅是我不确定它对应的值“1”?即...输出打印像这样“UPLOAD_ERR_INI_SIZE: 1”?
<?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");
$arrayVar = explode(".", $file["name"]);
$extension = end($arrayVar);
//commented out for “Only variables should be passed by reference” 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")
&& 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"]);
}
}
?>
这是其他用户为正确捕获错误而添加的行。请让我知道这是否正确对不起我根本不了解 PHP。无论如何,打印的错误只是“--要加载的CSV文件:上传文件失败>>>错误代码:1
”
<?php
// using upload at click from http://code.google.com/p/upload-at-click/
// FileData is the name for the input file
ini_set('display_errors', 1); error_reporting(E_ALL);
$file_result = "";
$file = $_FILES['Filedata'];
$allowedExtensions = array("csv", "txt");
$arrayVar = explode(".", $file["name"]);
$extension = end($arrayVar);
【问题讨论】:
-
你的 PHP 超时了吗?
-
不确定如何检查,但不太可能,因为我无法加载 61,000 条记录,但可以加载 60,000 条记录。加载 60,000 只需要 2 秒的一小部分时间,包括额外的处理
-
将此添加到脚本的开头
ini_set('display_errors', 1); error_reporting(E_ALL);并告诉我们您遇到的确切 PHP 错误。 -
@user1518600 - 让它打印出正在处理的记录。
-
@user1518600 - 见php.net/manual/en/features.file-upload.errors.php。看起来您的错误可能是 UPLOAD_ERR_INI_SIZE:上传的文件超出了 php.ini 中的 upload_max_filesize 指令。它不会在任何特定行上轰炸:仅当超出特定 #/bytes 时。 2MB 是常见的默认值。