In view
在视野中
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
In controler
在控制器中
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
echo 'error';
} else {
return array('upload_data' => $this->upload->data());
}
}
And I call this function like this
我这样称呼这个函数
$this->data['data'] = $this->do_upload();
and view this image:
并查看此图像:
<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
I don't know what's the error.
我不知道错误是什么。
7 个解决方案
#1
13
It seems the problem is you send the form request to welcome/do_upload
, and call the Welcome::do_upload()
method in another one by $this->do_upload()
.
看来问题是您将表单请求发送到welcome / do_upload,并通过$ this-> do_upload()调用另一个中的Welcome :: do_upload()方法。
Hence when you call the $this->do_upload();
within your second method, the $_FILES
array would be empty.
因此当你调用$ this-> do_upload()时;在第二种方法中,$ _FILES数组将为空。
And that's why var_dump($data['upload_data']);
returns NULL
.
这就是为什么var_dump($ data ['upload_data']);返回NULL。
If you want to upload the file from welcome/second_method
, send the form request to the welcome/second_method where you call $this->do_upload();
.
如果要从welcome / second_method上传文件,请将表单请求发送到您调用$ this-> do_upload();的welcome / second_method。
Then change the form helper function (within the View) as follows1:
然后更改表单辅助函数(在View中),如下所示:
// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');
File Uploading with CodeIgniter
CodeIgniter has documented the Uploading process very well, by using the File Uploading library.
CodeIgniter通过使用文件上传库很好地记录了上传过程。
You could take a look at the sample code in the user guide; And also, in order to get a better understanding of the uploading configs, Check the Config items Explanation section at the end of the manual page.
您可以查看用户指南中的示例代码;此外,为了更好地理解上传配置,请查看手册页末尾的配置项说明部分。
Also there are couple of articles/samples about the file uploading in CodeIgniter, you might want to consider:
还有一些关于在CodeIgniter中上传文件的文章/示例,您可能需要考虑:
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
- http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
- http://jamshidhashimi.com/image-upload-with-codeigniter-2/
- http://jamshidhashimi.com/image-upload-with-codeigniter-2/
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://hashem.ir/CodeIgniter/libraries/file_uploading.html (CodeIgniter 3.0-dev User Guide)
- http://hashem.ir/CodeIgniter/libraries/file_uploading.html(CodeIgniter 3.0-dev用户指南)
Just as a side-note: Make sure that you've loaded the url
and form
helper functions before using the CodeIgniter sample code:
正如旁注:在使用CodeIgniter示例代码之前,请确保已加载url并形成帮助函数:
// Load the helper files within the Controller
$this->load->helper('form');
$this->load->helper('url');
1. The form must be "multipart" type for file uploading. Hence you should use form_open_multipart()
helper function which returns:<form method="post" action="controller/method" enctype="multipart/form-data" />
1.表单必须是“multipart”类型才能进行文件上传。因此,您应该使用form_open_multipart()辅助函数返回:
#2
7
Simple Image upload in codeigniter
在codeigniter中上传简单图像
Find below code for easy image upload
查找以下代码以便轻松上传图片
public function doupload()
{
$upload_path="https://localhost/project/profile"
$uid='10'; //creare seperate folder for each user
$upPath=upload_path."/".$uid;
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config = array(
'upload_path' => $upPath,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userpic'))
{
$data['imageError'] = $this->upload->display_errors();
}
else
{
$imageDetailArray = $this->upload->data();
$image = $imageDetailArray['file_name'];
}
}
Hope this helps you to upload image
希望这可以帮助您上传图像
点击了解更多
#3
0
Change the code like this. It works perfectly:
像这样更改代码。它完美地运作:
public function uploadImageFile() //gallery insert
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$new_image_name = time() . str_replace(str_split(' ()\\/,:*?"<>|'), '',
$_FILES['image_file']['name']);
$config['upload_path'] = 'uploads/gallery/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = $new_image_name;
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['$min_width'] = '0';
$config['min_height'] = '0';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('image_file');
$title=$this->input->post('title');
$value=array('title'=>$title,'image_name'=>
$new_image_name,'crop_name'=>$crop_image_name);}
#4
0
File uploads are an essential process in many web apps. Almost every website and web app requires an integrated file upload component. File and image upload in CodeIgniter powered app is a simple component that takes care of the upload process with little issues.
文件上传是许多网络应用程序中必不可少的过程。几乎每个网站和Web应用程序都需要集成的文件上载组件。 CodeIgniter驱动的应用程序中的文件和图像上传是一个简单的组件,负责处理上传过程中几乎没有问题。
Create the Controller
创建控制器
The next step is the creation of a file in the controller folder. Name the file upload_controller.php. In this file, I will load a library for initializing the Upload class through the following code:
下一步是在控制器文件夹中创建文件。将文件命名为upload_controller.php。在此文件中,我将通过以下代码加载用于初始化Upload类的库:
$this->load->library('upload');
I will also set the preferences for the file upload process through the controller function do_upload()
. this function will contain the following code:
我还将通过控制器函数do_upload()设置文件上载过程的首选项。此函数将包含以下代码:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
For further detail please have a look https://www.cloudways.com/blog/codeigniter-upload-file-image/
有关详细信息,请查看https://www.cloudways.com/blog/codeigniter-upload-file-image/
#5
0
//this is the code you have to use in you controller
$config['upload_path'] = './uploads/';
// directory (http://localhost/codeigniter/index.php/your directory)
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//Image type
$config['max_size'] = 0;
// I have chosen max size no limit
$new_name = time() . '-' . $_FILES["txt_file"]['name'];
//Added time function in image name for no duplicate image
$config['file_name'] = $new_name;
//Stored the new name into $config['file_name']
$this->load->library('upload', $config);
if (!$this->upload->do_upload() && !empty($_FILES['txt_file']['name'])) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('production/create_images', $error);
} else {
$upload_data = $this->upload->data();
}
#6
0
$image_folder = APPPATH . "../images/owner_profile/" . $_POST ['mob_no'] [0] . $na;
if (isset ( $_FILES ['image'] ) && $_FILES ['image'] ['error'] == 0) {
list ( $a, $b ) = explode ( '.', $_FILES ['image'] ['name'] );
$b = end ( explode ( '.', $_FILES ['image'] ['name'] ) );
$up = move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $image_folder . "." . $b );
$path = ($_POST ['mob_no'] [0] . $na . "." . $b);
#7
-1
Simply to Try This
只是试试这个
Form Like This
形式像这样
<form method="post" enctype="multipart/form-data" action="<?=base_url();?>controller/ins_new_worker">
<input type='file' name="gov_id_img" accept="image/*" />
<button type="submit" class="btn info">Submit</button>
</form>
Controller Like This
控制器就像这样
$img = $_FILES;
$config['upload_path'] = './assets/uploads/worker_id';
$config['allowed_types'] = 'jpg|png';
$config['file_name'] = $res;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('set_img_name')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = $this->upload->data();
print_r($data);
}
#1
13
It seems the problem is you send the form request to welcome/do_upload
, and call the Welcome::do_upload()
method in another one by $this->do_upload()
.
看来问题是您将表单请求发送到welcome / do_upload,并通过$ this-> do_upload()调用另一个中的Welcome :: do_upload()方法。
Hence when you call the $this->do_upload();
within your second method, the $_FILES
array would be empty.
因此当你调用$ this-> do_upload()时;在第二种方法中,$ _FILES数组将为空。
And that's why var_dump($data['upload_data']);
returns NULL
.
这就是为什么var_dump($ data ['upload_data']);返回NULL。
If you want to upload the file from welcome/second_method
, send the form request to the welcome/second_method where you call $this->do_upload();
.
如果要从welcome / second_method上传文件,请将表单请求发送到您调用$ this-> do_upload();的welcome / second_method。
Then change the form helper function (within the View) as follows1:
然后更改表单辅助函数(在View中),如下所示:
// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');
File Uploading with CodeIgniter
CodeIgniter has documented the Uploading process very well, by using the File Uploading library.
CodeIgniter通过使用文件上传库很好地记录了上传过程。
You could take a look at the sample code in the user guide; And also, in order to get a better understanding of the uploading configs, Check the Config items Explanation section at the end of the manual page.
您可以查看用户指南中的示例代码;此外,为了更好地理解上传配置,请查看手册页末尾的配置项说明部分。
Also there are couple of articles/samples about the file uploading in CodeIgniter, you might want to consider:
还有一些关于在CodeIgniter中上传文件的文章/示例,您可能需要考虑:
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
- http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
- http://jamshidhashimi.com/image-upload-with-codeigniter-2/
- http://jamshidhashimi.com/image-upload-with-codeigniter-2/
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
- http://hashem.ir/CodeIgniter/libraries/file_uploading.html (CodeIgniter 3.0-dev User Guide)
- http://hashem.ir/CodeIgniter/libraries/file_uploading.html(CodeIgniter 3.0-dev用户指南)
Just as a side-note: Make sure that you've loaded the url
and form
helper functions before using the CodeIgniter sample code:
正如旁注:在使用CodeIgniter示例代码之前,请确保已加载url并形成帮助函数:
// Load the helper files within the Controller
$this->load->helper('form');
$this->load->helper('url');
1. The form must be "multipart" type for file uploading. Hence you should use form_open_multipart()
helper function which returns:<form method="post" action="controller/method" enctype="multipart/form-data" />
1.表单必须是“multipart”类型才能进行文件上传。因此,您应该使用form_open_multipart()辅助函数返回:
#2
7
Simple Image upload in codeigniter
在codeigniter中上传简单图像
Find below code for easy image upload
查找以下代码以便轻松上传图片
public function doupload()
{
$upload_path="https://localhost/project/profile"
$uid='10'; //creare seperate folder for each user
$upPath=upload_path."/".$uid;
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config = array(
'upload_path' => $upPath,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userpic'))
{
$data['imageError'] = $this->upload->display_errors();
}
else
{
$imageDetailArray = $this->upload->data();
$image = $imageDetailArray['file_name'];
}
}
Hope this helps you to upload image
希望这可以帮助您上传图像
点击了解更多
#3
0
Change the code like this. It works perfectly:
像这样更改代码。它完美地运作:
public function uploadImageFile() //gallery insert
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$new_image_name = time() . str_replace(str_split(' ()\\/,:*?"<>|'), '',
$_FILES['image_file']['name']);
$config['upload_path'] = 'uploads/gallery/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = $new_image_name;
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['$min_width'] = '0';
$config['min_height'] = '0';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('image_file');
$title=$this->input->post('title');
$value=array('title'=>$title,'image_name'=>
$new_image_name,'crop_name'=>$crop_image_name);}
#4
0
File uploads are an essential process in many web apps. Almost every website and web app requires an integrated file upload component. File and image upload in CodeIgniter powered app is a simple component that takes care of the upload process with little issues.
文件上传是许多网络应用程序中必不可少的过程。几乎每个网站和Web应用程序都需要集成的文件上载组件。 CodeIgniter驱动的应用程序中的文件和图像上传是一个简单的组件,负责处理上传过程中几乎没有问题。
Create the Controller
创建控制器
The next step is the creation of a file in the controller folder. Name the file upload_controller.php. In this file, I will load a library for initializing the Upload class through the following code:
下一步是在控制器文件夹中创建文件。将文件命名为upload_controller.php。在此文件中,我将通过以下代码加载用于初始化Upload类的库:
$this->load->library('upload');
I will also set the preferences for the file upload process through the controller function do_upload()
. this function will contain the following code:
我还将通过控制器函数do_upload()设置文件上载过程的首选项。此函数将包含以下代码:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
For further detail please have a look https://www.cloudways.com/blog/codeigniter-upload-file-image/
有关详细信息,请查看https://www.cloudways.com/blog/codeigniter-upload-file-image/
#5
0
//this is the code you have to use in you controller
$config['upload_path'] = './uploads/';
// directory (http://localhost/codeigniter/index.php/your directory)
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//Image type
$config['max_size'] = 0;
// I have chosen max size no limit
$new_name = time() . '-' . $_FILES["txt_file"]['name'];
//Added time function in image name for no duplicate image
$config['file_name'] = $new_name;
//Stored the new name into $config['file_name']
$this->load->library('upload', $config);
if (!$this->upload->do_upload() && !empty($_FILES['txt_file']['name'])) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('production/create_images', $error);
} else {
$upload_data = $this->upload->data();
}
#6
0
$image_folder = APPPATH . "../images/owner_profile/" . $_POST ['mob_no'] [0] . $na;
if (isset ( $_FILES ['image'] ) && $_FILES ['image'] ['error'] == 0) {
list ( $a, $b ) = explode ( '.', $_FILES ['image'] ['name'] );
$b = end ( explode ( '.', $_FILES ['image'] ['name'] ) );
$up = move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $image_folder . "." . $b );
$path = ($_POST ['mob_no'] [0] . $na . "." . $b);
#7
-1
Simply to Try This
只是试试这个
Form Like This
形式像这样
<form method="post" enctype="multipart/form-data" action="<?=base_url();?>controller/ins_new_worker">
<input type='file' name="gov_id_img" accept="image/*" />
<button type="submit" class="btn info">Submit</button>
</form>
Controller Like This
控制器就像这样
$img = $_FILES;
$config['upload_path'] = './assets/uploads/worker_id';
$config['allowed_types'] = 'jpg|png';
$config['file_name'] = $res;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('set_img_name')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = $this->upload->data();
print_r($data);
}