php 使用 Codeigniter 上传文件和创建上传文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20652538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file uploading and creating upload folders with Codeigniter
提问by ltdev
I'm having this function inside my Album_Model:
我的 Album_Model 中有这个功能:
public function upload($album_id, $album){
$album= strtolower($album);
$upload_config = array(
'upload_path' => './uploads/'.$album,
'allowed_types' => 'jpg|jpeg|gif|png',
'max_size' => '2000',
'max_width' => '680',
'max_height' => '435',
);
$this->load->library('upload', $upload_config);
// create an album if not already exist in uploads dir
// wouldn't make more sence if this part is done if there are no errors and right before the upload ??
if (!is_dir('uploads')) {
mkdir('./uploads', 0777, true);
}
if (!is_dir('uploads/'.$album)) {
mkdir('./uploads/'.$album, 0777, true);
}
if (!$this->upload->do_upload('imgfile')) {
// upload failed
return array('error' => $this->upload->display_errors('<span>', '</span>'));
} else {
// upload success
$upload_data = $this->upload->data();
return true;
}
}
So what this basicly does is to upload images to an album. Also if the folder album does not exist already, it creates a new album.
所以这基本上是将图像上传到相册。此外,如果文件夹相册不存在,则会创建一个新相册。
I was doing some tests and found out something that might be a small bug. While I was forcing my form to do some invalid upload attempts and the upload fails, as expected, but the album folder (suppose the album folder doesn't exist) is still created.
我在做一些测试,发现了一些可能是一个小错误。虽然我强迫我的表单进行一些无效的上传尝试并且上传失败,但正如预期的那样,但专辑文件夹(假设专辑文件夹不存在)仍然被创建。
Whouldn't make more sence to create the album folder if there are no erros and right before uploading the image and if yes how can I fix this??
如果在上传图像之前没有错误并且就在创建相册文件夹,那么创建相册文件夹就更有意义了,如果是,我该如何解决这个问题?
回答by Kumar V
You have set a flag for checking the directory existence. I have changed your code to make it work as per your need.
您已经设置了一个用于检查目录存在的标志。我已经更改了您的代码以使其根据您的需要工作。
<?php
public function upload($album_id, $album)
{
$album = strtolower($album);
$upload_config = array('upload_path' => './uploads/' . $album, 'allowed_types' =>
'jpg|jpeg|gif|png', 'max_size' => '2000', 'max_width' => '680', 'max_height' =>
'435', );
$this->load->library('upload', $upload_config);
// create an album if not already exist in uploads dir
// wouldn't make more sence if this part is done if there are no errors and right before the upload ??
if (!is_dir('uploads'))
{
mkdir('./uploads', 0777, true);
}
$dir_exist = true; // flag for checking the directory exist or not
if (!is_dir('uploads/' . $album))
{
mkdir('./uploads/' . $album, 0777, true);
$dir_exist = false; // dir not exist
}
else{
}
if (!$this->upload->do_upload('imgfile'))
{
// upload failed
//delete dir if not exist before upload
if(!$dir_exist)
rmdir('./uploads/' . $album);
return array('error' => $this->upload->display_errors('<span>', '</span>'));
} else
{
// upload success
$upload_data = $this->upload->data();
return true;
}
}
?>
回答by Ema4rl
THE ACCEPTED ANSWER IS NOT THE RIGHT WAY!!!
接受的答案不是正确的方法!!!
The right way is to extend the Upload library
正确的方法是扩展Upload库
The code:
编码:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* File Uploading Class Extension
*
* @package CodeIgniter
* @subpackage Libraries
* @category Uploads
* @author Harrison Emmanuel (Eharry.me)
* @link https://www.eharry.me/blog/post/my-codeigniter-upload-extension/
*/
class MY_Upload extends CI_Upload {
/**
* Validate Upload Path
*
* Verifies that it is a valid upload path with proper permissions.
*
* @return bool
*/
public function validate_upload_path()
{
if ($this->upload_path === '')
{
$this->set_error('upload_no_filepath', 'error');
return FALSE;
}
if (realpath($this->upload_path) !== FALSE)
{
$this->upload_path = str_replace('\', '/', realpath($this->upload_path));
}
if ( ! is_dir($this->upload_path))
{
// EDIT: make directory and try again
if ( ! mkdir ($this->upload_path, 0777, TRUE))
{
$this->set_error('upload_no_filepath', 'error');
return FALSE;
}
}
if ( ! is_really_writable($this->upload_path))
{
// EDIT: change directory mode
if ( ! chmod($this->upload_path, 0777))
{
$this->set_error('upload_not_writable', 'error');
return FALSE;
}
}
$this->upload_path = preg_replace('/(.+?)\/*$/', '\1/', $this->upload_path);
return TRUE;
}
}
How to use:
如何使用:
Simply create application/libraries/MY_Upload.phpand paste the code above in it.
That's all!
只需创建application/libraries/MY_Upload.php并粘贴上面的代码即可。就这样!
More info:
更多信息:
- The GitHub Gist.
- My blog poston Eharry.me
NOTE:
笔记:
This extension is compatible with CodeIgniter 3.x and 2.x versions.
此扩展与 CodeIgniter 3.x 和 2.x 版本兼容。
回答by Renier
The way you have it it will always make the directory if it does not exist.
如果目录不存在,您拥有它的方式将始终创建该目录。
To answer your question.. yes, it would make more sense, to create the folder right before uploading.
要回答您的问题.. 是的,在上传之前创建文件夹更有意义。
Try putting this:
试着把这个:
if (!is_dir('uploads')) {
mkdir('./uploads', 0777, true);
}
if (!is_dir('uploads/'.$album)) {
mkdir('./uploads/'.$album, 0777, true);
}
Inside this:
这里面:
if (!$this->upload->do_upload('imgfile')) {
// upload failed
return array('error' => $this->upload->display_errors('<span>', '</span>'));
} else {
// put if statements here.
// upload success
$upload_data = $this->upload->data();
return true;
}
So it would look like this:
所以它看起来像这样:
if (!$this->upload->do_upload('imgfile')) {
// upload failed
return array('error' => $this->upload->display_errors('<span>', '</span>'));
} else {
if (!is_dir('uploads')) {
mkdir('./uploads', 0777, true);
}
if (!is_dir('uploads/'.$album)) {
mkdir('./uploads/'.$album, 0777, true);
}
// upload success
$upload_data = $this->upload->data();
return true;
}
If I understand you correctly this should do what you want.
如果我正确理解你,这应该做你想做的。
Hope it helps.
希望能帮助到你。

