我正在尝试将现有的图像文件从临时文件夹移动到具有正确文件名的正确位置,由于某种原因它总是失败。
function move_temp_image($article_id)
{
global $db, $config;
if (($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.jpg", 'r+')) && ($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.png", 'r+')) && ($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.gif", 'r+')))
{
$this->error_message = "Could not find temp image to load?";
return false;
}
else
{
$image_info = getimagesize($image_load);
$image_type = $image_info[2];
$file_ext = '';
if( $image_type == IMAGETYPE_JPEG )
{
$file_ext = 'jpg';
}
else if( $image_type == IMAGETYPE_GIF )
{
$file_ext = 'gif';
}
else if( $image_type == IMAGETYPE_PNG )
{
$file_ext = 'png';
}
// give the image a random file name
$imagename = rand() . 'id' . $article_id . 'gol.' . $file_ext;
// the actual image
$source = $image_load;
// where to upload to
$target = $_SERVER['DOCUMENT_ROOT'] . "/uploads/articles/topimages/" . $imagename;
if (rename($source, $target))
{
// remove old temp image
if ($image['article_top_image'] == 1)
{
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/temp/' . $image_load);
}
unset($_SESSION['temp_tagline']);
$db->sqlquery("UPDATE `articles` SET `article_top_image` = 1, `article_top_image_filename` = ? WHERE `article_id` = ?", array($imagename, $article_id));
return true;
}
else
{
$this->error_message = 'Could not move temp file to tagline uploads folder!';
return false;
}
}
}
我不确定我做错了什么,我读重命名是这样做的方法,但我显然忽视了一些事情。
答案 0 :(得分:1)
$source
中没有文件名 - 在您的代码中,$source
包含文件资源...
重命名不仅适用于文件名,因此您必须更改代码。
即。用以下代码替换您的条件
$types = array('jpg', 'png', 'gif');
$file = $_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.";
$image_load = false;
foreach ($types as $type) {
if (file_exists($file . $type)) {
$image_load = $file . $type;
break;
}
}
if (!file_exists($image_load))