【问题标题】:finding if a file exists in a directory in php查找文件是否存在于php的目录中
【发布时间】:2014-10-27 18:47:35
【问题描述】:

我的大脑已经筋疲力尽,似乎无法很好地构建我的代码,我的解决方案有效,但我需要简化它。好吧,说我有 $id = $_GET["id"];并且 $id = 352 ... 现在,

场景

下面的代码是用户选择要上传的图片的地方:

$query = mysql_query("SELECT * FROM properties WHERE id = '$_GET[id]'");

<input type="file" name="image1" id="image1" />

<?php if(isset($query) && file_exists("../uploads/properties/".mysql_result($query, 0, "id").".jpg")) { ?>

<img height="100" src="../uploads/properties/<?php echo mysql_result($query, 0, "id"); ?>.jpg" />

<a target="_blank" href="delete_script/deletepic.php?id=<?php echo mysql_result($query, 0, "id"); ?>">Delete image</a>

<?php } ?>

<input type="file" name="image2" id="image2" />

<?php if(isset($query) && file_exists("../uploads/properties/".mysql_result($query, 0, "id")."_2.jpg")) { ?>

<img height="100" src="../uploads/properties/<?php echo mysql_result($query, 0, "id"); ?>_2.jpg" />

<a target="_blank" href="delete_script/deletepic2.php?id=<?php echo mysql_result($query, 0, "id"); ?>">Delete image</a>

<?php } ?>

如果你看一下img标签上的src,两者之间的区别是一张图片将被上传为352.jpg,而其他将保存为 352_2.jpg。我有 10 张图片可供用户上传,因此这些图片将保存为:

1: 352.jpg

2: 352_2.jpg

3: 352_3.jpg

4: 352_4.jpg

5: 352_5.jpg

6: 352_6.jpg

7: 352_7.jpg

8: 352_8.jpg

9: 352_9.jpg

10: 352_10.jpg

现在是棘手的部分:

当点击链接时,此页面deletepic.php 将运行以下代码:

<?php
$id = $_GET["id"];
$file_path = "../uploads/properties/".$id."_".$i.".jpg";
for ($i = 2; $i < 11; $i++) { 
    $file_path = "../uploads/properties/".$id."_".$i.".jpg";
    return $file_path;
}
if (file_exists($file_path)) {
    echo "file remove requested";
}else{
    echo "no file was requested to be removed";
}
?>

我知道 for 循环不起作用,但基本上我需要脚本来查看 $id."_".$i 然后如果文件存在于被点击的链接上,则必须删除该文件。

目前对我有用的解决方案是,如果我制作 10 个 delete.php 脚本并在每个脚本中运行这段代码,我将在脚本中更改以使其工作的唯一部分是 @987654327 @ 每次将其设置为$img = "../../uploads/properties/".$id.".jpg"; 下一个 delete.php $img 的值将是$img = "../../uploads/properties/".$id."_2.jpg"; 等。

所以现在delete.php里面的代码是这样的:

<?php
    $id = $_GET["id"];
    $current_page = 'properties_page.php';
    $url = $_SERVER['REQUEST_URI'];
    $clean_this = "delete_script/deletepic.php";
    $url = str_replace($clean_this, "properties_page.php", $url);
    $img = "../../uploads/properties/".$id.".jpg";
    unlink($img);
    header("Location:$url");
?>

【问题讨论】:

    标签: php html image


    【解决方案1】:

    您的第一个代码示例存在一些问题,例如 SQL 注入。请注意,不推荐使用 mysql_* 函数;您应该改用PDO。但这不是这里的问题,所以...您的 delete.php 脚本应该如下所示:

    <?php
    
    $id = $_GET['id'];
    $imageFolder = realpath('../../uploads/properties');
    
    $imagePath = $imageFolder . '/' . $id . '.jpg';
    
    if (file_exists($imagePath) && dirname(realpath($imagePath)) == $imageFolder) {
        unlink($imagePath);
        echo "file remove requested";
    } else {
        echo "no file was requested to be removed";
    }
    

    【讨论】:

    • 检查$imageFolder 是否正确。在您的示例中,它是 ../../uploads/properties/
    • LMAO 它可以工作,但奇怪的是它删除了文件夹中的所有内容,对我来说,大坝看起来像是回到了绘图板上
    • 好吧,假设 ID 为... 352... 它将删除 352.jpg、352_2.jpg、352_3.jpg 等...但仅此而已。我测试了代码,似乎还可以!
    • 我明白你的意思,但是是否没有办法查看该值是否是从被点击触发 delete.php 脚本的链接标签设置的?
    • 但前提是选择删除该特定图像
    【解决方案2】:

    我找到了解决问题的方法,所以基本上删除了循环并将 (_2.jpg) 的扩展名添加到 deletepic2.php &lt;a&gt; 标记链接,如下所示:

    HTML

    应用修复前的代码:

    <a target="_blank" href="delete_script/deletepic2.php?id=<?php echo mysql_result($query, 0, "id"); ?>">Delete image</a>
    

    应用修复后的代码:

    <a target="_blank" href="delete_script/deletepic2.php?id=<?php echo mysql_result($query, 0, "id"); ?>_2.jpg">Delete image</a>
    

    deletepic2.php 文件中,代码将执行如下:

    <?php
        $id = $_GET["id"];
        $url = $_SERVER['REQUEST_URI'];
        $cleanLink = "delete_img.php";
        $url = str_replace($cleanLink, "properties_page.php", $url);
        $img = "../uploads/properties/".$id;
        unlink($img);
        header("Location:$url");
    ?>
    

    所以本质上,无论我的目录中有多少文件,通过将 _(任何数字都可以在这里).jpg 的唯一字符串值添加到 html 文件中的链接,我都可以运行 php 脚本和当用户单击要删除的特定文件的链接时,定位我希望用户删除的特定文件。

    @rjdown 感谢您的帮助,我很感激。 (竖起两个大拇指)

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 2014-01-11
      • 2022-12-01
      • 2012-06-28
      • 2011-03-16
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2018-10-01
      相关资源
      最近更新 更多