我在CodeIgniter中上传文件时遇到问题。这是我的自动加载:$autoload['helper'] = array('url', 'form', 'file');
我已经回应了realpath(APPPATH . '../images');
,它是正确的位置。当我点击提交时,没有任何反应,也没有显示错误;它只是重新加载视图。
型号:
<?php
class Gallery_model extends CI_Model{
var $gallery_path;
function __construct()
{
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
}
function do_upload()
{
//handle userfile
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
}
}
?>
控制器:
<?php
class Gallery extends CI_Controller
{
function index()
{
$this->load->model('Gallery_model');
if($this->input->post('upload'))
{
//handle upload
$this->Gallery_model->do_upload();
}
$this->load->view('gallery');
}
}
?>
查看:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Gallery</title>
</head>
<body>
<div id="gallery">
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</body>
</html>
有什么问题?
答案 0 :(得分:1)
确保“应该放置上传文件夹的路径。文件夹必须是可写的,路径可以是绝对路径或相对路径。”
我只是复制代码,图片可以上传。
答案 1 :(得分:0)
如果您有$this->upload->do_upload()
,请将其替换为:
if(!$this->upload->do_upload())
{
die($this->upload->display_errors());
}
这样你就可以找出错误是什么。