如何用PHP命名上传的图像文件?

时间:2012-11-01 16:29:53

标签: php counter uploading

我想更改我的php上传页面上的代码,以便上传带有连续编号的照片1.jpg 2.jpg 3.jpg ... 9999999.jpg

我想以1开头的升序命名图像并进入无限远。

我不希望任何图像被覆盖,我认为计数器可以用于此,但我不知道如何正确编码。

上传的图像存储在两个目录中。 1.原始文件2.缩略图

$DestinationDirectory    = 'user-uploads/'; //original file
    $DestinationDirectorytn  = 'user-uploads-thumbnails/'; //original file thumbnail

目前,它通过time()函数

命名文件
// current time value, will be added as the new image name
    $CurrentTime    = time(); 

//Construct a new image name (time) for our new image.
    $NewImageName = $CurrentTime.'.'.$ImageExt;

2 个答案:

答案 0 :(得分:2)

试试这个:

$directory = "user-uploads/";
$filecount = count(glob($directory . "*"))+1;
//Construct a new image name (time) for our new image.
$NewImageName = $filecount.'.'.$ImageExt;

答案 1 :(得分:2)

您可以对目录中的文件进行计数,并将值增加到文件名。

该链接有示例:http://www.brightcherry.co.uk/scribbles/php-count-files-in-a-directory/

在计算文件之后,设置文件名:concat值with name。

由于

但还有很多其他方法。