如何从不同的文件夹中删除2个同名文件?

时间:2014-03-28 23:00:21

标签: php file-io

这是php代码:

if (array_key_exists('delete_file', $_POST)) {
  $filename = $_POST['delete_file'];
  if (file_exists($filename)) {
    unlink($filename);
    echo 'File '.$filename.' has been deleted';
  } else {
    echo 'Could not delete '.$filename.', file does not exist';
  }
}

这是表格:

echo '<form method="post">';
  echo '<input type="hidden" value="'.$file.'" name="delete_file" />';
  echo '<input type="submit" value="Delete image" />';
echo '</form>';

我有2个文件夹,图像和缩略图。我想从两个文件夹中删除具有相同名称的文件。使用上面的代码我可以删除文件只是一个文件夹。我应该复制第一个代码并在表单中添加第二个隐藏的输入,或者解决此问题的最简单方法是什么? ($file包含缩略图的路径和文件,$file2包含大图像的路径和文件)

3 个答案:

答案 0 :(得分:1)

添加文件夹数组和foreach循环

if (array_key_exists('delete_file', $_POST)) {
    $folders = ['images/', 'thumbnails/'];
    $filename = $_POST['delete_file'];
    foreach($folders as $folder) {
        if (file_exists($folder . $filename)) {
            unlink($folder . $filename);
            echo 'File '. $folder . $filename.' has been deleted';
        } else {
            echo 'Could not delete '. $folder . $filename.', file does not exist';
        }
    }
}

答案 1 :(得分:1)

您可以调整您的命名方案,因此您只需要1个隐藏输入。你的$ file变量只需要包含文件名而不是路径。保留php脚本中的路径并将它们附加到从表单接收的值。您可以使用basename函数去除表单源中的路径。你应该在你的echo语句中使用逗号。

if (array_key_exists('delete_file', $_POST)){
 $filename='thumbs/'.$_POST['delete_file'];
 if (file_exists($filename)){
  unlink($filename);
  echo'File ',$filename,' has been deleted';
 } else {
  echo'Could not delete ',$filename,', file does not exist';
 }
 $filename='pics/'.$_POST['delete_file'];
 if (file_exists($filename)){
  unlink($filename);
  echo'File ',$filename,' has been deleted';
 } else {
  echo'Could not delete ',$filename,', file does not exist';
 }
}

答案 2 :(得分:1)

简单方法:

echo '<form method="post">';
  echo '<input type="hidden" value="'.$file.'" name="delete_image" />';
  echo '<input type="hidden" value="'.$file2.'" name="delete_thumb" />';
  echo '<input type="submit" value="Delete image" />';
echo '</form>';

......然后:

function deleteFile($filename) {
 if (file_exists($filename)) {
  unlink($filename);
  echo 'File '.$filename.' has been deleted';
 } else {
  echo 'Could not delete '.$filename.', file does not exist';
 }
}

if (array_key_exists('delete_image', $_POST)) {
 deleteFile($_POST['delete_image']);
}

if (array_key_exists('delete_thumb', $_POST)) {
 deleteFile($_POST['delete_thumb']);
}

更好的方法是让$ file只包含图像文件名(不是它的路径),并且所有拇指的命名方式与其对应的图像相同。然后你可以使用:

echo '<form method="post">';
  echo '<input type="hidden" value="'.$file.'" name="delete_image" />';
  echo '<input type="submit" value="Delete image" />';
echo '</form>';

...然后:

$folder_image = "images/";
$folder_thumb = "thumbs/";

function deleteFile($filename) {
 if (file_exists($filename)) {
  unlink($filename);
  echo 'File '.$filename.' has been deleted';
 } else {
  echo 'Could not delete '.$filename.', file does not exist';
 }
}

if (array_key_exists('delete_image', $_POST)) {
 $file_image = $_POST['delete_image'];
 deleteFile($folder_image.$file_image);
 deleteFile($folder_thumb.$file_image);
}