douchuifk90315 2017-02-16 05:12
浏览 52

php图像文件上传编号问题

So i am having an issue with a project i'm woking on, and have tried everything possible (i can think of).

now, There is a bug on the "editBanner.php" page. When you delete an image, that number is deleted. When a new file is uploaded, it is given the number of one more than the total of current banners. There is a major problem with this, e.g. if we have four banners: 1.jpg, 2.jpg, 3.jpg and 4.jpg. If we delete 2.jpg, we are left with 1.jpg, 3.jpg and 4.jpg. If we then uploaded a new image, the page would count the number of images (in this case three) and add 1, giving us a new file name of "4.jpg". We already have a file called 4.jpg, therefore, the old 4.jpg would be replaced.

I have been told i have to use the microtime function, to solve this, but as i have not be exposed to this function i really don't know how to use it. If you can have a look at my code and give me an example, that would be amazing.

   <?php 
    require_once ("Includes/simplecms-config.php"); 
    require_once  ("Includes/connectDB.php");
    include("Includes/header.php");        

    confirm_is_admin(); 

    $dir = 'Images/banner';
    $confirmation = "";

    if(isset($_GET["del"])){
        if (file_exists($dir . "/" . $_GET["del"])) {
            unlink($dir . "/" . $_GET["del"]);
            $confirmation = $_GET["del"] . " deleted. <br><br>";
        } else {
            $confirmation = $_GET["del"] . " doesn't exist. <br>   
     <br>";
        }
    }

    $files = scandir($dir);
    array_shift($files);
    array_shift($files);

    $newImage = COUNT($files)+1;

    if(isset($_GET["add"])) {
        $confirmation = "Image successfully uploaded.";
    }

    if(isset($_FILES["file"]["name"])){
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        sort($allowedExts);
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);

        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 50 * 1024 * 1024) //50mb (1024 
      * 1kb = 1mb, 50 x 1024kb = 50mb)
        && in_array($extension, $allowedExts)) {
          if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " 
      kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
     <br>"; 
            echo round(microtime(true)) . $_FILES["file"]
      ["tmp_name"]; 

            move_uploaded_file($_FILES["file"]["tmp_name"], $dir . 
       "/" . $newImage . "." . $extension);
            echo "Stored in: " . $dir . "/" . $_FILES["file"]
       ["name"];
            header("Location: editBanner.php?add=1");


            if(file_exists($_FILES["file"]["tmp_name"]["name"]
        ["type"]["size"])   &&  is_file($_FILES["file"]["tmp_name"]
      ["name"]["type"]["size"])) {

           unlink($_FILES["file"]["tmp_name"]["name"]["type"]
       ["size"]);   

           }  




          }
        } else {
          echo "Invalid file";
          }
       }
    ?>


     <div id="container">
    <div id="admin">
        <?php
            if(!empty($confirmation)) {
                echo "<div id='confirmation'>" . $confirmation . "
       </div>";
            }

            for ($i = 0; $i < COUNT($files); $i++) {

                echo '<div id="bannerImage">';
                echo $files[$i] . ' - <a href="editBanner.php?del=' . 
    $files[$i] . '">Delete</a><br>';
                echo '<img src="' . $dir . '/' . $files[$i] . '" 
       width="200px" height="100px" /><br>';
                echo '</div>';
            }
        ?>
        <div id="bannerAdd">
            <form action="editBanner.php" method="post" 
            enctype="multipart/form-data">
                <h3>Upload a New Banner Image</h3>
                <input type="file" name="file" id="file"><br>
                <input type="submit" name="submit" value="Submit">
              </form>
          </div>
        </div>
     </div>

    </div> <!-- End of outer-wrapper which opens in header.php -->

    <?php 
         include ("Includes/footer.php");
   ?>
  • 写回答

1条回答 默认 最新

  • duanchi19820419 2017-02-16 05:48
    关注

    For saving an image you can use the following option.

    Current Time - You can use time() in php which saves the image with current time in unix format. You can concatenate it with a name if you want. Example:

    $newImageName = 'banner_'.time(); // output: banner_1487223503

    For deleting an image, I see you are scanning the directory and fetching the images in $files. So, you can apply the same.

    In this way, names of all images will be unique. Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题