0

我为一个允许上传 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) 的结果

([上传文件] => 数组([名称] => Final1.csv [类型] => 文本/逗号分隔值 [tmp_name] => /tmp/phpVnOIUK [错误] => 0 [大小] => 11607) )

更新 2

没有找到解决方案,如果你认为你有一个简单的答案,请发布它,但现在我将简单地尝试解决这个问题。

4

1 回答 1

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

于 2013-06-25T15:37:49.923 回答