【问题标题】:Copy image from url using timthumb使用 timthumb 从 url 复制图像
【发布时间】:2017-11-03 16:17:15
【问题描述】:

我目前正在编写上传、调整大小和裁剪脚本。 我在从 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/SummerVibeCover_1389227432.jpg&w=398):无法打开流:/home/[MYHOST]/public_html/pt/includes/crop/index 中没有这样的文件或目录.php 在第 108 行

无法复制文件,请重试。

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

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

最诚挚的问候, SmK1337

【问题讨论】:

  • 嗨,您是从自己的服务器复制并保存文件吗?.. 如果是这样,最好使用 $source 的路径而不使用所有参数? .. 失败了,那么file_get_contents 呢?
  • 所有获取争论对我来说都是必要的,以便使用 timthumb 调整图像大小,如果有更好的选择我愿意听到它:) - 我也对 file_get_contents 做了一些研究,我不确定我将如何将其应用于我的代码。问候,SmK1337。
  • 在你有$source 的地方添加file_get_contents 就像file_get_contents(timthumb.php?src=http://[MYDOMAIN]/includes/crop/temp/'.$name.'.'.$extension.'&w=398) 你可能需要添加完整的网址

标签: php image resize copy timthumb


【解决方案1】:

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

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

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

您应该改为通过 http 向timthumb.php 脚本发送请求。

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

copy('http://[MYDOMAIN]/path/to/timthumb/script/' . $source, $dest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 2014-09-18
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2011-10-28
    相关资源
    最近更新 更多