PHP 3位增量图像上传器

时间:2018-09-17 02:01:52

标签: php html

我是PHP的新手,所以我可以阅读但不能编写它。我正在寻找一个可以上传图像文件的PHP脚本,将其逐步重命名为“ prefix_001.jpg,prefix_002.jpg,prefix_003.jpg ...”,然后将其移至某个文件夹。

输入:

image.jpg
image(1).jpg
download.jpg
file.jpg

输出:

prefix_001.jpg
prefix_002.jpg
prefix_003.jpg
prefix_004.jpg

文件链接

/root
    /upload
        upload.php
    /imagesGoHere
        prefix_001.jpg
        prefix_002.jpg
        prefix_003.jpg
        prefix_004.jpg

任何帮助或正确方向的指导都将受到赞赏!

1 个答案:

答案 0 :(得分:0)

尝试一下:

要移动的图像位于与upload.php相同的文件夹中

upload.php

<?php


function moveImage()
{
    //Find image to rename and move to imagesGoHere/ folder.
    $files = glob("*.jpg");
    foreach ($files as $file)
    {
        $next_prefix = getNextPrefix();        
        copy($file, "imagesGoHere/prefix_00{$next_prefix}.jpg");
        echo "{$file} renamed/moved: imagesGoHere/prefix_00{$next_prefix}.jpg";
        //Remove original image
        unlink($file);
        echo "<br>";
    } 
}



function getNextPrefix()
{
    //Get last file uploaded in folder imagesGoHere
    $files = scandir('imagesGoHere/', SCANDIR_SORT_DESCENDING);
    $newest_file = $files[0];
    //Get last prefix ID
    $rest = substr("$newest_file", -5, 1);
    $next_prefix = $rest + 1;
    //Get new prefix ID
    return $next_prefix;    
}

echo moveImage();

输出:

image - Copy - Copy.jpg renamed/moved: imagesGoHere/prefix_001.jpg
image - Copy.jpg renamed/moved: imagesGoHere/prefix_002.jpg
image.jpg renamed/moved: imagesGoHere/prefix_003.jpg

下次执行新图像时

1.jpg renamed/moved: imagesGoHere/prefix_004.jpg