猿问

在本地托管的 PHP 网站中上传图片并替换之前的图片

这是我在 PHP Web 开发方面的第一次体验,我遇到了一个问题。我制作了一个 HTML 表单来上传可以正常工作的图像。但是,我正在尝试在其中添加一个功能,以便在上传新图像时删除同一链接中的前一个图像。


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

我尝试使用 unlink() 函数,但它没有删除该图像,显示警告 unlink(content/timeTables/FE/A): No such file or directory while there is an image at the same link and of the same name.请指导如何做到这一点。


侃侃尔雅
浏览 84回答 1
1回答

倚天杖

两件事,一是路径不是动态的,我做到了,二是确保value="<?php echo $a;?>"具有“fesectionatimetable”的值。<?php&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST["submit"]))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $a= $_POST['a'] ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($a == 'fesectionatimetable'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $target_dir = "content/"; //your path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $old_files = glob('content/*');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($old_files as $file){ // iterate files&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(is_file($file))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unlink($file); // delete file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uploaded.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; ?>&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <form action="index.php" method="post" enctype="multipart/form-data">&nbsp; &nbsp; <h2 style = "color : black ; "> Select image to upload: </h2>&nbsp; &nbsp; <h6 style = "color : black ; "> <input type="file" name="fileToUpload" id="fileToUpload">&nbsp;&nbsp; &nbsp; </h6>&nbsp; &nbsp; <h6 style = "color : black ; "> <input class="btn btn-primary" href="#" role="button"&nbsp;&nbsp; &nbsp; &nbsp;type="submit" value="Upload Image" name="submit"> </h6>&nbsp; &nbsp; <input type="text" name='a' value="fesectionatimetable" style="display:none"> //your value&nbsp;&nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp;&nbsp;&nbsp; &nbsp; <html>
随时随地看视频慕课网APP
我要回答