hankknight
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.t xt','hello ');
function original_name ($str){
while(file_exists($str)) {
// Add underscore and number after name before extention
}
return $str;
}
?>
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.t
function original_name ($str){
while(file_exists($str)) {
// Add underscore and number after name before extention
}
return $str;
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.