如何在 CodeIgniter 中上传图片?
Posted
技术标签:
【中文标题】如何在 CodeIgniter 中上传图片?【英文标题】:How to upload image in CodeIgniter? 【发布时间】:2013-06-23 08:23:34 【问题描述】:在视图中
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
在控制器中
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());
我这样称呼这个函数
$this->data['data'] = $this->do_upload();
并查看此图像:
<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
我不知道是什么错误。
【问题讨论】:
在您的视图中写入<?php var_dump($data['upload_data']); ?>
并检查结果。附注:上传失败时不要在控制器中回显'error'
。
它给了我空值。我想问一个问题,上传文件夹放在哪里?
当您将./uploads/
设置为上传文件夹时,它应该位于您的CodeIgniter 安装的根目录(靠近index.php
)。
您的控制器似乎有问题,请发布更多信息。
我把这个文件夹放在 index.php 附近,但它仍然给我空输出。
【参考方案1】:
看来问题是您将表单请求发送到welcome/do_upload
,并通过$this->do_upload()
在另一个中调用Welcome::do_upload()
方法。
因此,当您在第二种方法中调用$this->do_upload();
时,$_FILES
数组将为空。
这就是为什么var_dump($data['upload_data']);
返回NULL
。
如果您想从welcome/second_method
上传文件,请将表单请求发送到您调用$this->do_upload();
的welcome/second_method。
然后将表单辅助函数(在View内)更改如下1:
// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');
使用 CodeIgniter 上传文件
CodeIgniter has documented the Uploading process 非常好,通过使用文件上传库。
您可以查看用户指南中的示例代码;此外,为了更好地了解上传配置,请查看手册页末尾的配置项说明部分。
还有几篇关于在 CodeIgniter 中上传文件的文章/示例,您可能需要考虑:
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://jamshidhashimi.com/image-upload-with-codeigniter-2/ 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 用户指南)作为一个旁注:在使用 CodeIgniter 示例代码之前,请确保您已经加载了 url
和 form
辅助函数:
// Load the helper files within the Controller
$this->load->helper('form');
$this->load->helper('url');
// Load the helper files within the application/config/autoload
$autoload['helper'] = array('form', 'url');
1.表格必须是“多部分”类型的文件上传。因此,您应该使用返回的 `form_open_multipart()` 辅助函数: ``
【讨论】:
我在谷歌搜索另一个问题时偶然发现了这个问答。您可能需要使用以下链接 codeigniter.com/userguide3/libraries/… 更新您的答案,因为 ellislab.com/codeigniter/user-guide/libraries/… 库已过期。 @Fred-ii- 谢谢弗雷德。答案几乎是 3 岁 :) 我没有机会回到它。将使用 v3 ASAIC 更新答案。 我找到了关于这个cloudways.com/blog/codeigniter-upload-file-image的教程 上传文件的同时发送post变量可以吗?【参考方案2】:codeigniter 中的简单图片上传
找到以下代码,方便上传图片
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'];
希望这可以帮助您上传图片
【讨论】:
上传图片时提交的数据表单怎么样,可以@Hiren吗? 是的,这是可能的,您可以将数据与图像一起传递【参考方案3】://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();
【讨论】:
如何通过$error
ti 视图而不出现任何错误?
是否可以在上传的同时发布数据?【参考方案4】:
像这样更改代码。它完美地工作:
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);
【讨论】:
这个调用是从多部分形式的 POST 条目中执行的吗?我只是想知道表单是否成功提交了整个数据以及图像......【参考方案5】:$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);
【讨论】:
见... 首先创建 然后在codeigniter控制器中,编写上面的代码 因此您的图像将保存在特定文件夹中,并且只有该图像的名称将存储在数据库表中【参考方案6】:以下代码用于一次上传单个文件。 这对于上传单个文件是正确且完美的。 阅读所有注释说明并遵循代码。 毫无疑问,它奏效了。
public function upload_file()
***// Upload folder location***
$config['upload_path'] = './public/upload/';
***// Allowed file type***
$config['allowed_types'] = 'jpg|jpeg|png|pdf';
***// Max size, i will set 2MB***
$config['max_size'] = '2024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
***// load upload library***
$this->load->library('upload', $config);
***// do_upload is the method, to send the particular image and file on that
// particular
// location that is detail in $config['upload_path'].
// In bracks will set name upload, here you need to set input name attribute
// value.***
if($this->upload->do_upload('upload'))
$data = $this->upload->data();
$post['upload'] = $data['file_name'];
else
$error = array('error' => $this->upload->display_errors());
【讨论】:
【参考方案7】:检查 $this->upload->initialize($config);这对我来说很好
$new_image_name = "imgName".time() . str_replace(str_split(' ()\\/,:*?"<>|'), '',
$_FILES['userfile']['name']);
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = $new_image_name;
$config['max_size'] = '0';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|mp4|jpeg';
$config['file_name'] = url_title("imgsclogo");
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$this->upload->initialize($config);
$this->upload->do_upload();
$data = $this->upload->data();
【讨论】:
以上是关于如何在 CodeIgniter 中上传图片?的主要内容,如果未能解决你的问题,请参考以下文章
如何在视频上传时创建缩略图? [php/codeigniter]