我已经阅读了几十个类似的问题,但没有一个答案是我需要的。如果它存在,请指出它。
我有一个名为" txts"的文件夹。和另一个名为" content"的文件夹像这样:
files/texts files/content
我想复制" txts"到"内容"但仅当该文件尚未存在于"内容"文件夹中。
以下是我正在使用的代码:
<?php
copy('files/txts/file1.txt', 'files/content/file1.txt');
?>
问题是,如果已经存在,它会被覆盖。我需要复制文件(不删除原文)并将其添加到目标文件夹中(如果它还不存在)。
答案 0 :(得分:2)
使用file_exists()
功能。 http://php.net/manual/en/function.file-exists.php
if (!file_exists($dest_loc)){
copy($source_loc, $desk_loc);
}
答案 1 :(得分:1)
您正在寻找file_exists():
if(!file_exists('files/content/file1.txt')){
copy('files/txts/file1.txt', 'files/content/file1.txt');
}else{
echo "file already exists";
}