duanbushi1479 2019-06-19 09:11
浏览 85

将同一文件多次复制到其他文件夹[重复]

This question already has an answer here:

i using this script to copying files to other folder when i clicked submit button

<?php
if (isset($_POST['upload']) && isset($_POST['datae'])) {
copy('../print/'.$_POST['datae'], '../Upload/'.$_POST['datae']);
echo "<meta http-equiv='refresh' content='1'>";
}
?>

and now im wondering what if i choose same filename and submitted twice or more. normally the filename i've already choose will copy and overwrite the same file that already stored in there.

but what i want is whenever i submitted twice or more the file that already in destination folder, it will not replace or overwrite but it will duplicating same file and just different in file name like

//example in my folder
img_7878.JPG
img_7878_copy1.JPG
img_7878_copy2.JPG

maybe i could get help from here.

</div>
  • 写回答

2条回答 默认 最新

  • duangouhui0446 2019-06-19 09:29
    关注

    You should split the filename on the dot to get the extension seperately and then you can use a while loop to change the targetname until no file for the targetname is found.

    Next to that, don't do this: echo "<meta http-equiv='refresh' content='1'>"; Use Location.reload(); https://developer.mozilla.org/en-US/docs/Web/API/Location/reload

        if (isset($_POST['upload']) && isset($_POST['datae'])) {
            $exp = explode('.', $_POST['datae']);
            $targetName = '../Upload/'.$exp[0];
            $ext = $exp[1];
            $target = $targetName .".". $ext;
            $count = 0;
            while (file_exists($target)) {
                $target = $targetName . "_copy". $count .".". $ext;
                $count++;
            }
            copy('../print/'.$_POST['datae'], $target);
            echo "<meta http-equiv='refresh' content='1'>";
        }
    
    评论

报告相同问题?