Search code examples
phphtmlfile-uploadupload

HTML/PHP form suddenly stopped working without changing code


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!!

screenshot of website format. first file attempted to upload listed as 0 bytes, second file able to upload

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.


Solution

  • 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...

    <?php $targetDir = __DIR__ . '/'; 
    // Directory where uploaded files will be stored
     
    if(isset($_POST["submit"])) {
    
        ***sleep(0.5);***
    
    // Loop through each uploaded file 
    for ($i = 0; $i < count($_FILES["file 
    ...