我有一个网站,显示一个目录/文件夹,我可以将多个文件上传到所述目录/文件夹,以轻松访问重要文档/文章。它已经正常工作了大约一年,但上个月突然停止工作。我知道我没有意外更改我的代码,因为我有几个文件夹,其中相同的 php 文件复制/粘贴到每个文件夹中,并且我不可能更改每个 php 文件。
奇怪的是,如果我尝试一次上传 1 个文件,它会失败并上传 0 字节文档。如果我上传多个文件,第一个文件会失败并上传 0 字节文档,但所有后续文件都能够成功上传。
我不知道我的托管提供商是否发生了某些变化,或者输入类型=文件使用情况是否发生了变化,但我很乐意帮助。谢谢!!
HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple="multiple">
<input type="submit" value="Upload" name="submit">
</form>
PHP
<?php
$targetDir = __DIR__ . '/'; // Directory where uploaded files will be stored
if(isset($_POST["submit"])) {
// Loop through each uploaded file
for ($i = 0; $i < count($_FILES["fileToUpload"]["name"]); $i++) {
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"][$i]); // Get the filename of the uploaded file
// Check if file already exists
if (file_exists($targetFile)) {
echo '<script>alert("Sorry, the file '. basename($_FILES["fileToUpload"]["name"][$i]). ' already exists.");</script>';
continue; // Skip to the next iteration of the loop
}
// Check file size (optional)
if ($_FILES["fileToUpload"]["size"][$i] > 100000000) { // Adjust the file size limit as per your requirements
echo '<script>alert("Sorry, your file '. basename($_FILES["fileToUpload"]["name"][$i]). ' is too large.");</script>';
continue; // Skip to the next iteration of the loop
}
// If the upload is successful, move the uploaded file to the target directory
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $targetFile)) {
echo '<script>alert("The file '. basename($_FILES["fileToUpload"]["name"][$i]). ' has been uploaded.");</script>';
} else {
echo '<script>alert("Sorry, there was an error uploading your file.");</script>';
}
}
// Redirect back to the upload page
$redirectUrl = rtrim(dirname($_SERVER['PHP_SELF']), '/') . '/list.php';
echo '<script>window.location.href = "'.$redirectUrl.'";</script>';
}
?>
我已经在几台不同的计算机(个人和公共计算机)上尝试了 chrome、safari、phonebrave 应用程序,但没有任何效果,因此怀疑它与代码相关或服务器相关。
我尝试更改权限(当前为 0755),但没有运气。
我不太了解如何更改托管服务器上的文件/设置的细节...我确实尝试禁用 htaccess 并重新启用,但这没有任何作用。
如果无法访问服务器,很难为您提供帮助,但我会尽力为您提供一些指导。
问题可能来自于损坏的 pdf 文件。即使您可以使用 pdf 阅读器打开文件,上传也可能会失败。所以首先要做的就是用其他 pdf 文件进行测试。
检查php日志和服务器的容量。文件可能太大,服务器无法处理。您可以通过修改 PHP 配置(upload_max_filesize、post_max_size 等)来增加上传文件的最大大小。
该问题可能是由于防火墙配置的更改造成的,尽管这种情况不太可能发生,因为有些上传可以通过,而另一些则不能。
编写一个测试文件,尽可能最基本的并用
var_dump($_POST)
等检查发布的数据
希望它能有所帮助。