将缩略图保存到另一个文件夹 [CodeIgniter]
Save thumbnail image to another folder [CodeIgniter]
我正在尝试为要上传到我的服务器的图像创建缩略图。参考 https://www.codeigniter.com/userguide3/libraries/image_lib.html#processing-an-image 。创建完成后,我想将缩略图保存到另一个文件夹,因为原始图像和缩略图将保存在同一位置。
所以,我尝试使用 move_uploaded_file()
函数,但它无法移动文件。我不确定我的代码是否正确完成。看看吧。
Belwo 是我的代码:
if($_FILES['file_name']['size'] != 0){
$config['upload_path'] = FCPATH.MEDIA_LIB_URI.'facts';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$newFileName = removeExt($_FILES['file_name']['name']).'-'.uniqueId(); // renaming the file name without the ext
$new_name = $newFileName.getExt($_FILES['file_name']['name']); // new file name with the ext
$config['file_name'] = $new_name;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$thumbName = $newFileName.'_thumb'.getExt($_FILES['file_name']['name']); // getting the exact thumbnail name that been created by codeigniter
move_uploaded_file($thumbName, FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'.$new_name);
return $file_name;
} else {
return $this->upload->display_errors();
}
}
您使用重命名或复制找到了正确的解决方案。
它不能与 move_uploaded_file 一起工作的原因是因为此函数只会移动 $_FILES 数组中临时名称等于 "tmp_name" 的上传文件。
PHP 手册中解释了此行为的原因:“此函数检查以确保文件名指定的文件是有效的上传文件(意味着它是通过 PHP 上传的的 HTTP POST 上传机制)。如果文件有效,它将被移动到目标指定的文件名。
如果对上传文件所做的任何操作都有可能将其内容泄露给用户,甚至同一系统上的其他用户,则这种检查尤为重要。"
根据 PHP POST 方法上传手册:
$_FILES['userfile']['name'] = 客户端计算机上文件的原始名称。
$_FILES['userfile']['tmp_name'] = 上传文件存储在服务器上的文件的临时文件名。
引用结束
"tmp_name" 是服务器生成的唯一密钥,因此文件系统中没有文件名重复。
在您的代码中,您构建的 $thumbnail 字符串与 $_FILES 数组中的 "tmp_name" 不匹配。
您没有收到错误消息的原因是,同样来自 PHP 手册,但这次 move_uploaded_file:"If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE."
因此,如果您在代码中捕获 move_uploaded_file 的结果,例如 $moveResult = move_uploaded_file(... 您最终会得到 $moveResult === false。这总是一个好主意检查此类操作的结果,以便在出现问题时做出反应。
希望对您有所帮助:
在 $configer
中使用 new_image
更改文件夹路径:$config['new_image'] = '/path/to/new_image.jpg';
注 :
如果仅指定新图像名称,它将被放置在与原始图像相同的文件夹中
如果只指定路径,新图片将被放置在与原始图片同名的目标中。
如果指定了路径和图像名称,它将放置在自己的目的地并赋予新名称。
你的代码应该是这样的:
if($this->upload->do_upload('file_name'))
{
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
'new_image' => FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
return $file_name;
}
else
{
return $this->upload->display_errors();
}
更多:https://www.codeigniter.com/user_guide/libraries/image_lib.html#CI_Image_lib::resize
我正在尝试为要上传到我的服务器的图像创建缩略图。参考 https://www.codeigniter.com/userguide3/libraries/image_lib.html#processing-an-image 。创建完成后,我想将缩略图保存到另一个文件夹,因为原始图像和缩略图将保存在同一位置。
所以,我尝试使用 move_uploaded_file()
函数,但它无法移动文件。我不确定我的代码是否正确完成。看看吧。
Belwo 是我的代码:
if($_FILES['file_name']['size'] != 0){
$config['upload_path'] = FCPATH.MEDIA_LIB_URI.'facts';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$newFileName = removeExt($_FILES['file_name']['name']).'-'.uniqueId(); // renaming the file name without the ext
$new_name = $newFileName.getExt($_FILES['file_name']['name']); // new file name with the ext
$config['file_name'] = $new_name;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$thumbName = $newFileName.'_thumb'.getExt($_FILES['file_name']['name']); // getting the exact thumbnail name that been created by codeigniter
move_uploaded_file($thumbName, FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'.$new_name);
return $file_name;
} else {
return $this->upload->display_errors();
}
}
您使用重命名或复制找到了正确的解决方案。
它不能与 move_uploaded_file 一起工作的原因是因为此函数只会移动 $_FILES 数组中临时名称等于 "tmp_name" 的上传文件。
PHP 手册中解释了此行为的原因:“此函数检查以确保文件名指定的文件是有效的上传文件(意味着它是通过 PHP 上传的的 HTTP POST 上传机制)。如果文件有效,它将被移动到目标指定的文件名。 如果对上传文件所做的任何操作都有可能将其内容泄露给用户,甚至同一系统上的其他用户,则这种检查尤为重要。"
根据 PHP POST 方法上传手册:
$_FILES['userfile']['name'] = 客户端计算机上文件的原始名称。 $_FILES['userfile']['tmp_name'] = 上传文件存储在服务器上的文件的临时文件名。
引用结束
"tmp_name" 是服务器生成的唯一密钥,因此文件系统中没有文件名重复。
在您的代码中,您构建的 $thumbnail 字符串与 $_FILES 数组中的 "tmp_name" 不匹配。
您没有收到错误消息的原因是,同样来自 PHP 手册,但这次 move_uploaded_file:"If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE."
因此,如果您在代码中捕获 move_uploaded_file 的结果,例如 $moveResult = move_uploaded_file(... 您最终会得到 $moveResult === false。这总是一个好主意检查此类操作的结果,以便在出现问题时做出反应。
希望对您有所帮助:
在 $configer
中使用 new_image
更改文件夹路径:$config['new_image'] = '/path/to/new_image.jpg';
注 : 如果仅指定新图像名称,它将被放置在与原始图像相同的文件夹中
如果只指定路径,新图片将被放置在与原始图片同名的目标中。
如果指定了路径和图像名称,它将放置在自己的目的地并赋予新名称。
你的代码应该是这样的:
if($this->upload->do_upload('file_name'))
{
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
'new_image' => FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
return $file_name;
}
else
{
return $this->upload->display_errors();
}
更多:https://www.codeigniter.com/user_guide/libraries/image_lib.html#CI_Image_lib::resize