简体   繁体   中英

How to name uploaded image files with PHP?

I would like to change the code on my php uploading page so it uploaded photos with sequential numbering 1.jpg 2.jpg 3.jpg... 9999999.jpg

I want to name images in an ascending order starting with 1 and going to infinity.

I don't want any images to be over written and I think a counter can be used for this, but i'm unaware of how to code it properly.

Uploaded images are stored in two directories. 1. original file 2. thumbnail

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

Currently it names the file by the time() function

// 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;

Try this:

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

You can count the files in directory and increment the value for to name of file.

The link have example: http://www.brightcherry.co.uk/scribbles/php-count-files-in-a-directory/

After that count the files, you set the name of file: concat value with name.

Thanks

But have many other ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM