【发布时间】: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