php check file name exist, rename the file

可紊 提交于 2019-11-27 22:39:57

问题


How do I check if file name exists, rename the file?

for example, I upload a image 1086_002.jpg if the file exists, rename the file as 1086_0021.jpg and save, if 1086_0021.jpg is exist, rename 1086_00211.jpg and save , if 1086_00211.jpg is exist, rename 1086_002111.jpg and save...

Here is my code, it only can do if 1086_002.jpg exist, rename the file as 1086_0021.jpg, maybe should do a foreach, but how?

//$fullpath = 'images/1086_002.jpg';

if(file_exists($fullpath)) {
    $newpieces = explode(".", $fullpath);
    $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
    $newpath = $frontpath.'1.'.end($newpieces);
}

file_put_contents($newpath, file_get_contents($_POST['upload']));

回答1:


Try something like:

$fullpath = 'images/1086_002.jpg';
$additional = '1';

while (file_exists($fullpath)) {
    $info = pathinfo($fullpath);
    $fullpath = $info['dirname'] . '/'
              . $info['filename'] . $additional
              . '.' . $info['extension'];
}



回答2:


Why not just append a timestamp onto the filename? Then you won't have to worry about arbitrarily long filenames for files which have been uploaded many times.




回答3:


I hope this helps

$fullPath = "images/1086_002.jpg" ;
$fileInfo = pathinfo($fullPath);
list($prifix, $surfix) = explode("_",$fileInfo['filename']);
$x = intval($surfix);
$newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
while(file_exists($newFile)) {
    $x++;
    $newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
}

file_put_contents($newFile, file_get_contents($_POST['upload']));

I hope this Helps

Thanks

:)




回答4:


I feel this would be better. It will help keep track of how many times a file with the same name was uploaded. It works in the same way like Windows OS renames files if it finds one with the same name.

How it works: If the media directory has a file named 002.jpg and you try to upload a file with the same name, it will be saved as 002(1).jpg Another attempt to upload the same file will save the new file as 002(2).jpg

Hope it helps.

$uploaded_filename_with_ext = $_FILES['uploaded_image']['name'];
$fullpath = 'media/' . $uploaded_filename_with_ext;
$file_info = pathinfo($fullpath);
$uploaded_filename = $file_info['filename'];

$count = 1;                 
while (file_exists($fullpath)) {
  $info = pathinfo($fullpath);
  $fullpath = $info['dirname'] . '/' . $uploaded_filename
  . '(' . $count++ . ')'
  . '.' . $info['extension'];
}
$image->save($fullpath);



回答5:


You can change your if statement to a while loop:

$newpath = $fullpath;
while(file_exists($newpath)) {
    $newpieces = explode(".", $fullpath);
    $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
    $newpath = $frontpath.'1.'.end($newpieces);
}

file_put_contents($newpath, file_get_contents($_POST['upload']));


来源:https://stackoverflow.com/questions/10015232/php-check-file-name-exist-rename-the-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!