繁体   English   中英

PHP-如何从目录中删除文件,如果文件已经存在?

[英]php-how to delete files from directory if files already exist?

我尝试删除文件(如果已存在)。

但是我最终没有结果。

谁能帮我这个!!!

$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';
if (!file_exists($path_user)) {
    if (mkdir( $path_user,0777,false )) {
        //
    }
} 

unlink($path_user);

if(move_uploaded_file($file['tmp_name'],$path_user.$path)){
    echo "Your File Successfully Uploaded" . "<br>";
}

整理您的代码,请尝试以下操作:

$path = 'filename.ext'; // added reference to filename
$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';

// Create the user folder if missing
if (!file_exists($path_user)) {
   mkdir( $path_user,0777,false );
}
// If the user file in existing directory already exist, delete it
else if (file_exists($path_user.$path)) {
   unlink($path_user.$path);
}

// Create the new file
if(move_uploaded_file($file['tmp_name'],$path_user.$path)) {                 
    echo"Your File Successfully Uploaded"."<br>";
}

请记住,PHP不会递归删除目录内容,您应该使用像这样的函数

也许你错过了其他条件? 和file_name变量:

$file_name = 'sample.jpg';

$path_user = '/wp-content/plugins/est_collaboration/Files/'.$send_id.'/';

if (!file_exists($path_user.$file_name)) 
{                   
  if (mkdir( $path_user,0777,false )) {

  }

} else {

  unlink($path_user.$file_name);
}

暂无
暂无

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

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