-3

Actually I'm using uniqid(); to generate random names:

$file = UPLOAD_DIR . uniqid() . '.png';

The output looks like:

53bd02cdc6b9b.png
53bd02cdc6bd8.png
53bd0320aafbc.png
53bd0320aaff7.png
53bd03e89b8df.png

I want to change these names each file as output:

picture_0001.png
picture_0002.png
picture_0003.png
picture_0004.png
picture_0005.png

Do you have better ideas?

Ivan
  • 1,221
  • 2
  • 21
  • 43
  • yeah. give them the names you want to? – Félix Adriyel Gagnon-Grenier Jul 10 '14 at 00:17
  • 3
    $file = UPLOAD_DIR . picture_0001 . '.png';? – sharf Jul 10 '14 at 00:17
  • But the string should be . picture_ . with indexed instances, I don't want to put static string as numbers like 0001, 0002 – Ivan Jul 10 '14 at 00:20
  • Please don't downvote my question, it hurt my feelings, I wanted to do simple question. – Ivan Jul 10 '14 at 00:21
  • 1
    @Ivan You should've atleast tried something if you didn't want them to downvote you. – Darren Jul 10 '14 at 00:22
  • 1
    **2** Options: *first option:* you store links to image in database. You get the last one (`ORDER BY imglink DESC LIMIT 1`) -> you `$t = explode('_', $imglink)` -> you get the index of the number which would be `$t[1]` -> you increment it as you please and `$file = UPLOAD_DIR . 'picture_' . $increment .'.png';` or you scan the directory and do the same explode/itterate for it. – Darren Jul 10 '14 at 00:25
  • @Darren, there no second option? – Ivan Jul 10 '14 at 00:30
  • @Ivan second option is `"or you scan the directory and do the same explode/itterate for it"` as stated. – Darren Jul 10 '14 at 00:31

2 Answers2

4

Your want to fix all your current images to the correct format (See @ Félix Gagnon-Grenier answer), then once you done that you can do something like the following:

//get array of current images
$imgs = glob(UPLOAD_DIR.'*.png');

//select last image in array, strip out all non-alpha's then pad it with 4 0's
$next = str_pad(preg_replace("/[^0-9]/","", end($imgs))+1, 4, "0", STR_PAD_LEFT);

$file = UPLOAD_DIR.'picture_'.$next.'.png';

Edit

See comments - file based counter

$count_file = UPLOAD_DIR.'count.txt'; //or put somewhere else

//make count file if not exists
if(!file_exists($count_file)){
    file_put_contents($count_file,0);
}

//get last image count + 1 
$next = file_get_contents($count_file)+1;

//set file var 
$file = UPLOAD_DIR.'picture_'.sprintf("%04s",$next).'.png';

//update counter
file_put_contents($count_file, $next);
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • This seems to assume that `glob` returns files in a guaranteed order. Is that correct? – FuzzyTree Jul 10 '14 at 00:49
  • In my tests it works as expected `GLOB_NOSORT` would have an adverse effect, but there is [alternatives](http://stackoverflow.com/questions/1095630/php-folder-file-listing-in-alphabetical-order) – Lawrence Cherone Jul 10 '14 at 00:53
  • But, we don't need to get array of current images, I mean I'm looking an alternative of uniqid(); that generates ordered name list without conflict or overwritten files Is possible?. – Ivan Jul 10 '14 at 01:13
  • @Ivan your need to get the last image number to know what number todo next, how would you do this if you dont lookup the last image? There is no inbuilt function for the job. – Lawrence Cherone Jul 10 '14 at 01:16
  • @LozCheroneツ I have no experience about storing variables in server using php, when a new picture is added in folder, it adds +1 in server before to name picture_0001, then added pic again, it would be picture_0002, sorry I know this design is stupid but it's a neccesary. – Ivan Jul 10 '14 at 01:19
  • PHP does not have persistence like that. There are a few solutions to this problem, store in database(use unique id),store on filesystem (in a counter file) or look up the last image. – Lawrence Cherone Jul 10 '14 at 01:21
  • @LozCheroneツ Is possible to get a variable as number 0 in plain text .txt, then php calls to open .txt and read this number to name, then adds 1, save and close? – Ivan Jul 10 '14 at 01:29
  • Yeah sure, but its slower because of the reading and writing. Gimme a sec ill add some code – Lawrence Cherone Jul 10 '14 at 01:31
  • @Ivan check edit, it will increment regardless of errors ect and it does nothing that original answer does but adds the risk of incrementing on failure and overwriting files if the file is edited or removed. good luck – Lawrence Cherone Jul 10 '14 at 01:47
  • @LozCheroneツ Thanks, I appreciate your help! I know it will give errors but I don't plan to use it for big traffic, only tiny traffic ;) – Ivan Jul 10 '14 at 01:51
3

well, since it hurts your feeling, you seem to be a human being, and I care for human beings. there you go:

I don't know any context, so this may be wildly far from what you actually need.

say your directory is /home/dir

$i = 1;
foreach (scandir('/home/dir') as $file)
 {if ($file == '.' || $file == '..') continue;
  rename($file,'picture_' . str_pad($i,4,'0',STR_PAD_LEFT) . '.png');
  $i++;}

this will generate:

picture_0001.png
picture_0002.png
picture_0003.png
picture_0004.png
picture_0005.png

in place of your old files

  • 2
    This would just overwrite other images with the same generated names. – Lawrence Cherone Jul 10 '14 at 00:37
  • @LozCheroneツ now this shouldn't – Félix Adriyel Gagnon-Grenier Jul 10 '14 at 00:45
  • 1
    Thanks for being kind! let's review your code, well I guess I didn't explain very well, let me explain again, I'm looking an alternative to uniqid(); would generate ordered numbers with our string like picture_0001.png , we don't need to rename existing pictures, we have to start from stratch with correct file names. do you understand? – Ivan Jul 10 '14 at 01:16