【问题标题】:Unable to copy() image from one folder to another无法将图像从一个文件夹复制()到另一个文件夹
【发布时间】:2016-03-28 16:25:13
【问题描述】:

我正在尝试将图像从一个文件夹复制到另一个文件夹。我觉得我的路径是错误的,但是我尝试了这么多路径组合,现在我不确定是不是问题。

我正在尝试将user_photos 文件夹中的图像(如果存在)复制到profile_pics 文件夹中。这是它们所在的位置,以及称为change_dp.php 的脚本,一旦调用,就会执行copy()

www > user_data > user_photos > photos_are_here
www > user_data > profile_pics > photos_are_here
www > inc > change_dp.php

这里是change_dp.php

$all_pics = mysqli_query ($connect, "SELECT * FROM user_photos WHERE username = '$username'");
    $row_query = mysqli_fetch_assoc($all_pics);
            $get_photo_owner = $row_query['username'];
            $get_photo_id = $_GET['id'];

$get_pic_with_id = mysqli_query ($connect, "SELECT * FROM user_photos WHERE id = '$get_photo_id'");
    $row_query2 = mysqli_fetch_assoc($get_pic_with_id);
            $img_url = $row_query2['img_url'];

    $file = $img_url;
    $arrPathInfo = pathinfo($file);
    $shortened_url = $arrPathInfo['basename'];

if (file_exists($file)) {
copy("../../" . $file, "../../profile_pics/" . $shortened_url); 
}

$pro_pic_change = mysqli_query($connect, "UPDATE users SET profile_pic='$shortened_url' WHERE username = '$username'") or die ($connect);
     header("Location: /photos/$get_photo_owner");

只是为了进一步说明问题:

  • 用户上传的照片(存储在user_photos中)存储在我的数据库中的完整路径中,例如,如果Alice上传了一个名为alice.jpg的文件,它将存储为user_data/user_photos/alice.jpg。这就是为什么我有变量$shortened_url,它只会获取文件的基本名称,即从user_data/user_photos/alice.jpg 获取alice.jpg。我需要$shortened_url,因为数据库中的profile_pics 列仅包含图像的基本名称,而不是完整路径。
  • 使用if statement,我正在尝试获取图像的基本名称,查看我的user_photos 文件中是否存在同名图像,如果存在,请将其复制到profile_pics 文件夹中并通过 UPDATE 语句将基本名称设置为 profile_pics 列。 UPDATE 语句工作正常,数据库中的图像名称确实发生了变化,但图像只是没有复制过来,这会损坏图像,因为它无法在 profile_pics 文件夹中找到它。

我尝试过的:

copy($file, "../profile_pics/" . $shortened_url); 
copy("../../" . $file, "../../profile_pics/" . $shortened_url);
copy($file, "user_data/profile_pics/" . $shortened_url);

没有哪个有效。

编辑

这是我希望代码执行的操作:

例如,让我们为$file 赋值:

$file = user_data/user_photos/alice.jpg
  1. 查看user_photos文件夹中是否存在名为$file的图像。
  2. 如果文件夹中有名为alice.jpg 的文件,则将该图像复制到profile_pics 文件夹中。
  3. 就是这样。

【问题讨论】:

  • 您应该在代码中添加一些测试以确定脚本在什么时候失败。如果文件根本没有找到要开始的文件,也许 file_exists 代码上的 else 会向自己显示一条消息。
  • @LukePittman - 嗯,看来你是对的。我有一个 else 语句,它回显了 don't exist 和上面的所有 copy() 路径,它们都回显了 else 语句 - 这意味着没有一个 copy() 可以找到该文件。但我该如何解决这个问题?我需要它来查看文件是否存在,如果没有if (file_exists($file),我想不出其他方法来检查它的存在?
  • 你能展示你更新的代码吗?
  • @LukePittman - 当然,除了 if 语句之外没有太大变化:`if (file_exists($file)) { copy("../../" . $file, ". ./../profile_pics/" . $shortened_url); } else { echo "
    不存在"; }`` .. 其他一切都保持不变,如上所示。

标签: php


【解决方案1】:

您可以使用__DIR__ 魔术常量,然后基于此您可以使用realpath() 函数甚至dirname() 函数转到目录层次结构的上层。这样一来,它就不会那么混乱,也不会那么容易出错。

例如:

$upOneLevel = realpath(__DIR__."../");
$upTwoLevels = realpath(__DIR__."../../");

所以要访问您的文件,您应该使用:

$fileRealPath = $upOneLevel."/".$file;
$fileRealDestination = $upOneLevel."user_data/profile_pics/".$shortened_url;

我还建议仅在文件存在且复制命令执行成功的情况下更新数据库中的数据:

if (file_exists($fileRealPath)) {
    if(copy($fileRealPath, $fileRealDestination)) {
        //TODO db logic here
    } else {
        //TODO error handling here        
    }
} else {
    //TODO error handling here
}

【讨论】:

  • 啊,代码需要稍微调整一下,即$fileRealPath =$upTwoLevels."/".$file; 而不是$upOneLevel。但除此之外,代码有效!非常感谢:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2016-03-22
  • 2014-09-17
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多