I have a while loop issue in PHP that seems like it could have been solved using the ampersand sign to make it a reference if it were elsewhere. Below is an example. I'm trying to append _n to the filename (in the basename). If there's _1 then I want it to be _2, if there's _2 I want it to be _3 and so on. For some reason I can't update the $filename variable in the condition so I think it's not getting changed in the loop.
$dir = 'images';
$filename = '123.jpg';
$i = 0;
while (file_exists($dir.'/'.$filename)) {
$filename = preg_replace('/^(\d*?)(\.jpg)$/',"$1_".(++$i)."$2",$filename);
}
echo $filename;
What am I doing wrong?
It looks like your regex is a little off, and you are not capturing the _n
if it already exists.
while (file_exists($dir.'/'.$filename)) {
$filename = preg_replace('/^(\d+)(.*)(\.jpg)$/',"$1_".(++$i)."$3",$filename);
//-------------------------------^^^^ Capture the _n if it exists
// And stick the number in with $1 and $3 (now the .jog)
}
echo $filename;
// Current files...
123_1.jpg
123_2.jpg
123.jpg
// Outputs 123_3.jpg
If you didn't want to use a regex and you wanted to make sure you got all the jpg files in a directory you could use glob()
and some basic string manipulation functions like so:
$dir = 'images/';
foreach (glob($dir.'*.jpg') as $file) {
$ext = strpos($file, '.jpg'); // get index of the extension in string
$num = (int) substr($file, 0, $ext); // get the numeric part
$file = $num+1 . '.jpg'; // rebuild the file name
echo $file, PHP_EOL;
}
Here's an example using a function and no regex. This way works for a much wider range of circumstances.
ie any file extension works, and, underscores or period allowed in the basename
If you don't need those things then using preg_replace() is cleaner, see Michael's answer.
<?php
function nextUnusedFileName($path, $fileName){
$index = 1;
while (file_exists($path."/".$fileName))
{
$baseNameEndIndex = strrpos($fileName, '_');
$extensionStartIndex = strrpos($fileName, '.');
if($baseNameEndIndex <= 0){
$baseNameEndIndex = $extensionStartIndex;
}
$baseName = substr($fileName, 0, $baseNameEndIndex)."_".$index;
$extension = substr($fileName, $extensionStartIndex+1, strlen($fileName)-$extensionStartIndex);
$fileName = $baseName.".".$extension;
$index++ ;
}
return $fileName;
}//end nextUnusedFileName
//main
$dir = "images";
$filename = "123.jpg";
$filename = nextUnusedFileName($dir, $filename);
echo $filename;
?>
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.