I have a website which displays a directory/folders where I can upload multiple files onto said directory/folder to easily access important documents/articles. It’s been working beautifully for about a year and suddenly stopped working the last month. I know that I have not changed my code accidentally because I have several folders with the same php file copy/pasted into each folder and would be impossible for me to have changed each php file.
What’s weird is that if I try uploading 1 file at a time, it fails and uploads a 0 byte document. If I upload multiple files, the first file fails & uploads a 0 byte document, but all subsequent files are able to upload successfully.
I don’t know if something changed with my hosting provider or something changed with input type=file usage but would love help. Thanks!!
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>';
}
?>
I have tried several different computers (personal and public ones) on chrome, safari, phone brave app, and nothing works, so suspect it is code-related or server related.
I tried changing permissions (currently 0755), but no luck there.
I don’t really know much about changing specifics of files/settings on hosting server… I did try to disable htaccess and re-enable, but that didn’t do anything.
2
Answers
Solved! Ended up inserting a delay at beginning of upload.php code to account for server lag, which resolved the issue.... Still not sure what changed on the server end though since code was fine for over a year...
It’s difficult to help you without access to the server, but I’ll try to give you a few pointers.
The problem may come from a corrupted pdf. The upload may fail even if you can open the file with a pdf reader. So the first thing to do is to test with other pdf files.
Check the php logs and the server’s capacity. A file may be too large for the server to handle. You can increase the maximum size of uploaded files by modifying the PHP configuration (upload_max_filesize, post_max_size, etc.).
The problem may possibly be due to a change in the firewall configuration, although this is unlikely as some uploads get through and others don’t.
Write a test file, the most basic as possible and check the posted data with
var_dump($_POST)
, etc.Hoping it might help.