0

我目前正在处理上传、调整大小和裁剪脚本。我在从 URL 复制图像并将其保存到我的服务器上时遇到了一些问题。

这是我发生错误的代码。我用 [MYDOMAIN] 替换了我的域 - 所以请忽略 :)

if(in_array($extension, $extensions_allowed) )
{
    $name = explode(".".$extension, $_FILES['image']['name']);
    $name = $name[0]."_".time();

    move_uploaded_file($_FILES['image']['tmp_name'], "temp/".$name.".".$extension);
    // File uploaded to temporary folder, now lets replace the newly created temp image with a resized image
    $source = 'timthumb.php?src=http://[MYDOMAIN]/includes/crop/temp/'.$name.'.'.$extension.'&w=398';   // Set source to be the resized image
    $dest = $_SERVER['DOCUMENT_ROOT'].'/includes/crop/temp/r'.$name.'.'.$extension;                             // Destination = temp folder, rdy to be cropped.
    if(copy($source, $dest)) {
        // If the image was transfered/copied successfully
        unlink("temp/".$name.".".$extension);   // Remove old temp img. "not the resized"
        // The old one has been removed, so now the new file can take its place, lets rename it.
        $current = 'temp/r'.$name.'.'.$extension;
        $new = 'temp/'.$name.'.'.$extension;
        rename($current, $new); // Old temp name becomes the new.
    }
    else {
        echo "Couldnt copy file, try again.";
        die();
    }


    $_SESSION['image']['extension'] = $extension;
    $_SESSION['image']['name'] = $name;
    //REDIRECT ON SUCCESS
    header("Location: /includes/crop/edit.php");
}

我觉得没有必要编写完整的代码,因为我知道它正在工作,而且它只会在这里出错。

所以我的move_uploaded_files()工作正常,但错误发生在下面的 if 语句中。

if(copy($source, $dest)) {
    // If the image was transfered/copied successfully
    unlink("temp/".$name.".".$extension); // Remove old temp img. "not the resized"
    // The old one has been removed, so now the new file can take its place, lets rename it.
    $current = 'temp/r'.$name.'.'.$extension;
    $new = 'temp/'.$name.'.'.$extension;
    rename($current, $new);   // Old temp name becomes the new.
}
else {
    echo "Couldnt copy file, try again.";
    die();
}

我的错误信息:

警告:复制(timthumb.php?src= http://[MYDOMAIN]/includes/crop/temp/SummerVibe Cover_1389227432.jpg&w=398):无法打开流:/home/[MYHOST]/ 中没有这样的文件或目录public_html/pt/includes/crop/index.php 在第 108 行

无法复制文件,请重试。

如果您想知道第 108 行在哪里,那就是if(copy(ect))开始的那一行。

希望有人可以提供帮助。:)

最亲切的问候,SmK1337

4

1 回答 1

0

您的错误是(或者是,因为这是一个相当古老的问题),您正在尝试将相对路径复制到 php 脚本。( timthumb.php?src=...)

首先。您的错误意味着此路径不存在,这可能有两个主要原因:

  1. timthumb.php是一个相对路径,这意味着 php 将尝试相对于您当前的工作目录来解析它,这可能会根据您执行脚本的方式而有所不同。
  2. 您将尝试复制文件的实际内容,而不是执行它。这肯定不是你想要的。
  3. ?src=on a file path 不是实际的查询字符串,而只是文件名的一部分。当然,这个文件不存在

相反,您应该做的是timthumb.php通过 http 向脚本发送请求。

copy可以通过 http 复制文件,在这种情况下(如果它是正确配置的 Web 服务器上的 php 脚本),它将运行。

copy('http://[MYDOMAIN]/path/to/timthumb/script/' . $source, $dest);
于 2017-11-03T08:17:15.770 回答