【问题标题】:delete image in folder and his ''mini'' size in same time同时删除文件夹中的图像和他的“迷你”尺寸
【发布时间】:2016-03-02 21:32:42
【问题描述】:

我创建了一个包含我的图片的文件夹“../img/travaux/villa”,所以当我上传图片时,我创建了一个脚本,在新文件夹中创建了该图片的“迷你”副本“.. /img/travaux/villa/mini",同名。

现在我想制作一个按钮来删除我的图像,但我也想同时删除“迷你”图像我该怎么做?

这是我的显示和删除图像的代码,但这不会删除迷你图像:

<?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';
    }
}
$folder = glob("../img/travaux/villa/*jpg");
foreach ($folder as $picture) {
    echo "<div class='divimages'>"; 
    echo '<img src="'.$picture.'"/>';
    echo '<form method="post">';
	echo '<input type="hidden" value="'.$picture.'" name="delete_file" />';
    echo '<input type="submit" class="delete-button" value="Delete image" />';
    echo '</form>';
    echo "</div>";  
}
?>

【问题讨论】:

  • 在第 4 行,而不是 unlink($filename);键入回声 $ 文件名。看看会发生什么,如果它真的进入 if (),可能是文件路径问题。您可以使用 DIR 或 $_SERVER['DOCUMENT_ROOT'] 或其他方法构建路径。

标签: php image delete-file


【解决方案1】:

你可以这样试试:

$fileName = basename($_POST['delete_file']);
// this will remove the path and leave only the name of the file.
$filePath = 'your full path'.'/'.$fileName;
// now build the 'mini image' full path - like '/var/www/project/img/travaux/villa/mini'.'/'.$fileName
if (file_exists($filePath)) {
    unlink($filePath);
    echo 'File '.$filePath.' has been deleted';
} else {
    echo 'Could not delete '.$filePath.', file does not exist';
}

注意:这只会删除“迷你”图像。您需要将其添加到现有代码中才能删除主图像

【讨论】:

    【解决方案2】:

    在取消链接主图后添加这行代码即可:

    unlink(pathinfo($filename, PATHINFO_DIRNAME).'/mini/'.pathinfo($filename, PATHINFO_BASENAME));
    

    【讨论】:

      【解决方案3】:

      您应该创建一个简单的文件删除器函数,该函数采用文件名数组。并使用展位名称调用该功能。这是一个灵活的解决方案,因为更换部件发生在外部。

      【讨论】:

        【解决方案4】:

        您的代码中存在漏洞,有人可以从您的网站根文件夹中删除任何文件,小心!

        你可以试试下面的代码:

        $paths = array(
            'orig' => '../img/travaux/villa/',
            'mini' => '../img/travaux/villa/mini/',
        );
        
        if (array_key_exists('delete_file', $_POST)) {
            $filename = basename($_POST['delete_file']);
            foreach($paths as $path) {
                if (file_exists($path.$filename)) {
                    if(!unlink($path.$filename)) {
                        $error = 'File '.$filename.' could not be deleted';
                    }
                    else {
                        echo 'File '.$filename.' has been deleted';
                    }
                }
                else {
                    $error = 'Could not delete '.$filename.', file does not exist';
                }
                if($error) echo $error;
            }
        }
        

        【讨论】:

        • 实际访问这个页面需要密码
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多