0

First of all i am a beginner and idk if the following code is a proper way to do what i want but this is how i think i could do this (using forms to pass some values).

So, i use the following code to let the user choose if he wants to delete a file in folder, while uploading one with the same name in it. The parameters of the FIRST "move_uploaded_file" have the same content with the SECOND "move_uploaded_file" (this is why i echo them) and it is working perfectly but it is for files that DONT have the same name with the uploaded one.

So any ideas what is my mistake here? The delete (unlink) is working but when i click the "Yes" button it is not uploading the new file.

If you need more information let me know and sorry if this is a dumb question but i really cant understand what is wrong.

Thanks is advance!


<?php

            $target_dir = "wp-content/themes/astra-child/myuploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;

                if (file_exists($target_file)) {
                    
                    echo "Already exists.<br>";
                    echo "Do you want to replace the file?";
                    ?> 
                    <form action="" method="post">
                        <input type="hidden" id="myfilename" name="myfilename" value="<?php echo ($_FILES["fileToUpload"]["tmp_name"]);?>">
                        <input type="hidden" id="replacefilename" name="replacefilename" value="<?php echo basename($_FILES["fileToUpload"]["name"]);?>">
                        <input type="submit" value="Yes" name="btn-replace-yes">
                        <input type="submit" value="No" name="btn-replace-no">
                    </form>
                    <?php
                    
                    if (isset($_POST['btn-replace-yes'])) {

                        $replacelink = $target_file . $_POST["replacefilename"];
                        unlink(realpath("$replacelink"));

                        $target = $target_file . $_POST["replacefilename"];

                        $last = $_POST["myfilename"];
                        $last = str_replace('\\\\', '\\', $last);

                        echo $last;
                        ?><br><?php
                        echo $target;

                        if (move_uploaded_file($last, $target)){

                            $uploadOk = 0;
    
                            echo "The file with name «". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). "» is inserted in folder successfully!";
        
                            $uploadOk = 0;
                            mysqli_close($link);
    
                        } else {
                            echo "Failure.";?>
                            <button onclick="window.location.href='secretary-panel';">
                                Go Back
                            </button>
                            <?php
                        }

                        $uploadOk = 0;

                    }else if (isset($_POST['btn-replace-no'])){
                        $uploadOk = 0;
                        header("location:../secretary-panel/");
                    }
                    $uploadOk = 0;
                }
?>

<?php

                $target_dir = "wp-content/themes/astra-child/myuploads/";
                $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);


                // if everything is ok and we dont have the same name of the files, try to upload
                 if ($uploadOk == 1) {

                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
                        echo $_FILES["fileToUpload"]["tmp_name"];?><br><?php
                        echo $target_file;

                    } else {
                        echo "Failure.";?>
                        <button onclick="window.location.href='secretary-panel';">
                            Go Back
                        </button>
                        <?php
                    }
                  }

?>

Scissors38th
  • 127
  • 10
  • You can not do it this way. If the script instance that the file upload was sent to, does not move the file out of the upload temp dir - then PHP will _delete_ the temp file automatically, when the script ends. Passing the temp name via form to the next script instance, isn't going to help you here - you will receive the temp. name, but the file will not be there any more at that point. – CBroe Dec 07 '21 at 13:45
  • 2
    You need to move the file out of the upload temp directory directly in the script instance that received it. If you need to ask the user whether they want to overwrite a file that already exists in the target location, then you need to move it _somewhere else_ first. And then, after you got the user's confirmation, you can not use move_uploaded_file any more (because this will then not be considered an uploaded file any more, and the function checks for that), but you will have to use copy or rename, to move it from your own temp. storage, to the target folder. – CBroe Dec 07 '21 at 13:45
  • Maybe after the user has selected a file in the UI, do a quick AJAX request to the server to check if a file with the same name exists in the uploads folder. If so, then ask the user to tick a box to say they're ok to overwrite it. Otherwise, don't accept the form unless they've renamed the file and re-selected it (or just ignore the uploaded file if they don't tick the box). Or to make it less complicated, after the AJAX just put a warning on to say that uploading the file will overwrite one with the same name, and suggest they either unselect it or rename it. – ADyson Dec 07 '21 at 14:04
  • Thanks for your comments guys, i understood my mistake and i will try to solve this with your ideas. I think i'm gonna choose to upload the file temporarily in another folder and if the user chooses "Yes" i will move it to the main folder, else i will delete it from the temp folder. – Scissors38th Dec 07 '21 at 14:22

0 Answers0