我为一个允许上传csv文件并自动存储在mysql数据库中的网站做了一部分。我设置了move_uploaded_file函数,以便确保正确上传,但每次尝试上传时都会失败。
表格
<form enctype="multipart/form-data" action="LoadData.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload File: <input name="uploadedfile" type="file"><br />
File/Table Name: <input name="filename" type="text"><br />
<input name="submit" type="submit" value="Upload File">
</form>
上传代码
$FileName = $_POST['filename'];
$target_path = "/director/to/all/uploaded/files/" . $FileName;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "File uploaded correctly </br>";
//uploads file to database
mysql_query("LOAD DATA LOCAL INFILE....."); //long piece of code that uploads the csv file to the database.table
echo "Table imported <br/>"';
}
else
{
echo "The file was no uploaded correctly, the table was not imported.";
}
我已经检查过,$ _FILES数组不是空的,但是文件不是在我确保存在且具有完全权限的新目录中创建的。我很确定我的语法也是正确的; move_uploaded_file(/temporary/file/location/tmp_name.csv,/new/file/location/test.csv)任何人都能看到我出错的地方?
更新1
print_r($ _ FILES)
的结果([uploadedfile] =&gt;数组([name] =&gt; Final1.csv [type] =&gt; text /逗号分隔值[tmp_name] =&gt; / tmp / phpVnOIUK [错误] =&gt; 0 [size] =&gt; 11607))
更新2
没有找到解决方案,如果你认为你有一个简单的答案请发布,但现在我只是试着解决问题。
答案 0 :(得分:2)
您的代码盲目地假设成功。永远不要假设上传成功。您的样板上传处理代码看起来应该更像
if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
// it worked, handle the upload
...
} else {
die("Upload failed with error code: " . $_FILES['uploadedfile']['error']);
}
错误代码在此处定义:http://php.net/manual/en/features.file-upload.errors.php