Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

PHP: Make file name original by adding an underscore and a number

Using PHP, how can I automatically make a file name original by adding an underscore and a number to the end?

If you run the script 12 times, the files saved should be named:
hello.txt
hello_1.txt
hello_2.txt
hello_3.txt
hello_4.txt
hello_5.txt
hello_6.txt
hello_7.txt
hello_8.txt
hello_9.txt
hello_10.txt
hello_11.txt



<?php

file_put_contents('hello.txt','hello');

function original_name ($str){
 while(file_exists($str)) {
  // Add underscore and number after name before extention
 }
 return $str;
}

?>
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial