简体   繁体   English

PHP rename()函数删除文件(如果已存在)

[英]PHP rename() function deletes file if already exists

I've been looking around and appearently the // if sucess should be executed when the file gets renamed: 我一直在环顾四周,显然// if sucess重命名文件时应该执行// if sucess

if(rename("$filepath$oldfilename", "$filepath$filename")===true) { // if success }

Unfortunately, since this function renames the file even if another file is there with the same name, it's always a success. 不幸的是,由于此函数重命名文件,即使另一个文件具有相同的名称,它也总是成功的。

But yet worse...since another file with same name already exists, it somehow gets deleted... 但更糟糕的是......因为已存在同名的另一个文件,它会以某种方式被删除...

Anyone know a way how to prevent this? 任何人都知道如何防止这种情况? And why this is happening?! 为什么会发生这种情况?!

Additional info: 附加信息:

Im giving the user the opportunity to change the file name through a textarea, when it's posted, the rename function will start: 我给用户提供了通过textarea更改文件名的机会,当它发布时,重命名功能将启动:

        if(rename("$filepath$oldfilename", "$filepath$filename")===true)
        {
            $WhatToUpdateQueryResult = mysql_query($WhatToUpdateQuery) or die ("query fout ". mysql_error() );      

            if ($WhatToUpdateQueryResult == 1)
            {
                $uploadmsg = "Document name successfully updated.<br/> From: $oldfilename <br/> To: $filename.";
            }
        }
        else
        {
            $uploadmsg = "Can't update document. A file with the same name already exists.";
        }

Note: As long I change the name to something which doesn't already exists, it works fine. 注意:只要我将名称更改为尚不存在的名称,它就可以正常工作。 But still, it always ends up true. 但是,它总是最终成真。

You will have to make a function to check if filename already exists: 您必须创建一个函数来检查文件名是否已经存在:

function rename_if_free($newPath, $oldPath) {
    if (file_exists($newPath)) return false;
    else {
        rename($oldPath, $newPath);
        return true;
    }
}

And put that function in your if statement. 并将该函数放在if语句中。

Now it will be 现在它会

if (rename_if_free($filepath.$oldfilename, $filepath.$filename) === true) { 
    $WhatToUpdateQueryResult = mysql_query($WhatToUpdateQuery) or die ("query fout ". mysql_error() );      

        if ($WhatToUpdateQueryResult == 1)
        {
            $uploadmsg = "Document name successfully updated.<br/> From: $oldfilename <br/> To: $filename.";
        }
}
else {
    $uploadmsg = "Can't update document. A file with the same name already exists.";
}

http://php.net/manual/en/function.move-uploaded-file.php http://php.net/manual/en/function.move-uploaded-file.php

take move_uploaded_file instead. 取而代之的是move_uploaded_file This will return false, if file can't be moved. 如果无法移动文件,则返回false。

Edit: Ah you are not necessarily doing an upload? 编辑:啊,你不一定要上传? Then you would have to check if the destination file already exists manually i assume. 然后你必须检查目标文件是否已经手动存在我假设。 check out file_exists() 检查file_exists()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM