我正在尝试创建一个接受CSV文件的网页,最终将其上传到数据库,但是如果存在另一个具有相同名称的文件,我目前仍然坚持让它覆盖自己。目前我有以下内容:
index.html -
<form action="upload.php" method="post" enctype="multipart/form-data">
Select CSV file to upload
<input type="file" name="fileToUpload" accept=".csv">
<input type="submit" value="Upload File" name="submit">
</form>
upload.php -
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
function print_array($array)
{
//---//
//print out an array in a way that doesn't make you want to vommit.
//---//
echo "<pre>";
print_r($array);
echo "</pre><p>";
}
if(isset($_POST['overwrite_YES']))
{
$_FILES['fileToUpload']['name'] = $_POST['name'];
$_FILES['fileToUpload']['type'] = $_POST['type'];
$_FILES['fileToUpload']['tmp_name'] = $_POST['tmp_name'];
$_FILES['fileToUpload']['error'] = $_POST['error'];
$_FILES['fileToUpload']['size'] = $_POST['size'];
$_FILES['fileToUpload']['tmp_name'] = str_replace("/","\\",$_FILES['fileToUpload']['tmp_name']);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
print_array($_FILES);
if (!file_exists($_FILES['fileToUpload']['tmp_name']))
{
echo "tmp file does not exists.<br>";
}
echo "fileToUpload: " . $_FILES['fileToUpload']['tmp_name'] . " -> " . $target_file . "<br>";
//unlink($target_file);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "womp womp :(";
}
exit;
}
else if(isset($_POST['overwrite_NO']))
{
$temp_name = $_POST['temp_name'];
unlink($temp_name);
echo "upload cancelled.<br>";
exit;
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($fileType != "csv" ) {
echo "Sorry, only CSV files are allowed. ";
}
else
{
if (file_exists($target_file))
{
print_array($_FILES);
$temp_name = "D:/xampp/tmp/ASMTMP.tmp";
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $temp_name);
echo "the file '" . $target_file . "' already exists.<br>";
echo "would you like to overwrite this?<br>";
echo "<form id='overwrite' action=" . $_SERVER['PHP_SELF'] . " method='post' enctype='multipart/form-data'>";
echo "<input type='hidden' name='name' value='" . $_FILES['fileToUpload']['name'] . "' />";
echo "<input type='hidden' name='type' value='" . $_FILES['fileToUpload']['type'] . "' />";
echo "<input type='hidden' name='tmp_name' value='" . $temp_name . "' />";
echo "<input type='hidden' name='error' value='" . $_FILES['fileToUpload']['error'] . "' />";
echo "<input type='hidden' name='size' value='" . $_FILES['fileToUpload']['size'] . "' />";
echo "<input name='overwrite_YES' style='height:40; width:150;' type='submit' value='Yes' />";
echo "</form>";
echo "<form id='nooverwrite' action=" . $_SERVER['PHP_SELF'] . " method='post'>";
echo "<input name='overwrite_NO' style='height:40; width:150;' type='submit' value='No' />";
echo "<input type='hidden' name='temp_name' value='" . $temp_name . "' />";
echo "</form>";
}
else if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
print_array($_FILES);
echo "fileToUpload: " . $_FILES['fileToUpload']['tmp_name'] . " -> " . $target_file . "<br>";
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
?>
所以当我通过这个时,一切都正常工作,除了一行读取'womp womp :('。 如果该文件不存在。它写得正确。如果文件确实存在,它会正确写入临时文件,但是当我尝试实际覆盖现有文件时,if(move_uploaded_file ...)会跳过并转到else语句。
成功写入的输出如下:
Array
(
[fileToUpload] => Array
(
[name] => test.csv
[type] => application/octet-stream
[tmp_name] => D:\xampp\tmp\phpFEE0.tmp
[error] => 0
[size] => 488007
)
)
fileToUpload: D:\xampp\tmp\phpFEE0.tmp -> uploads/test.csv
The file test.csv has been uploaded.
失败覆盖的输出如下所示:
Array
(
[fileToUpload] => Array
(
[name] => test.csv
[type] => application/octet-stream
[tmp_name] => D:\xampp\tmp\ASMTMP.tmp
[error] => 0
[size] => 488007
)
)
fileToUpload: D:\xampp\tmp\ASMTMP.tmp -> uploads/test.csv
womp womp :(
我试图在覆盖之前使用取消链接,以确保它没有实际覆盖的问题,但这没有帮助。而且我没有收到任何错误,所以非常感谢任何帮助。
答案 0 :(得分:0)
您正在覆盖$ _FILES变量的值。
删除以下行:
$_FILES['fileToUpload']['name'] = $_POST['name'];
$_FILES['fileToUpload']['type'] = $_POST['type'];
$_FILES['fileToUpload']['tmp_name'] = $_POST['tmp_name'];
$_FILES['fileToUpload']['error'] = $_POST['error'];
$_FILES['fileToUpload']['size'] = $_POST['size'];
$_FILES['fileToUpload']['tmp_name'] = str_replace("/","\\",$_FILES['fileToUpload']['tmp_name']);
答案 1 :(得分:0)
最后我改变了
if(move_uploaded_file($ _ FILES [&#34; fileToUpload&#34;] [&#34; tmp_name&#34;],$ target_file) ...
到
if(rename($ _ FILES [&#34; fileToUpload&#34;] [&#34; tmp_name&#34;],$ target_file)) ...
解决了我遇到的问题,以防其他人遇到这个问题。我认为&#39;也许问题可能是因为&#39; move_uploaded_file&#39;不会工作,因为我创建了一个新的tmp文件?我认为它不是那样的。