Mafaz Ahsan
Mafaz Ahsan

Reputation: 103

Upload image and replace the previous image in a locally hosted PHP website

It's my first experience on PHP web development and I am facing an issue. I made a HTML form to upload image that works fine. However, I am trying to add a feature in it so that when the new image is uploaded the previous image at the same link is deleted.

    <?php
        if(isset($_POST["submit"])) {
            $a= $_POST['a'] ;
            if ($a == 'fesectionatimetable'){
                $target_dir = "content/timeTables/FE/";
                unlink ('content/timeTables/FE/A') ;
            }
           $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
           echo '$_FILES["fileToUpload"]["name"]' ;
           $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
             if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been 
                 uploaded.";
                 }
                } 
    ?>

        <html>
        <body>
        <form action="index.php" method="post" enctype="multipart/form-data">
        <h2 style = "color : black ; "> Select image to upload: </h2>
        <h6 style = "color : black ; "> <input type="file" name="fileToUpload" id="fileToUpload"> 
        </h6>
        <h6 style = "color : black ; "> <input class="btn btn-primary" href="#" role="button" 
         type="submit" value="Upload Image" name="submit"> </h6>
        <input type="text" name='a' value="<?php echo $a;?>" style="display:none">
        </form>

        </body>  
        <html>

I tried using unlink() function and it did not remove that image, showing a warning that unlink(content/timeTables/FE/A): No such file or directory while there is an image at the same link and of the same name.Please guide how to do this.

Upvotes: 1

Views: 77

Answers (1)

bilal malik
bilal malik

Reputation: 86

Two things, One the path is not dynamic, which I did, secondly make sure value="<?php echo $a;?>" has value "fesectionatimetable"..

<?php

        if(isset($_POST["submit"])) 
        {

            $a= $_POST['a'] ;
            if ($a == 'fesectionatimetable'){
                $target_dir = "content/"; //your path
                $old_files = glob('content/*');
                foreach($old_files as $file){ // iterate files
                  if(is_file($file))
                    unlink($file); // delete file
                }
            }
           $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
           $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
             if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been 
                 uploaded.";
                 }
        } 
    ?>

    <html>
    <body>
    <form action="index.php" method="post" enctype="multipart/form-data">
    <h2 style = "color : black ; "> Select image to upload: </h2>
    <h6 style = "color : black ; "> <input type="file" name="fileToUpload" id="fileToUpload"> 
    </h6>
    <h6 style = "color : black ; "> <input class="btn btn-primary" href="#" role="button" 
     type="submit" value="Upload Image" name="submit"> </h6>
    <input type="text" name='a' value="fesectionatimetable" style="display:none"> //your value 
    </form>

    </body>  
    <html>

Upvotes: 1

Related Questions