如何在我们的项目中应用codeigniter文件上传程序?
我用codeigniter做了一个简单的文件上传程序,然后作为我的特例,当我尝试在我的项目中应用时,程序运行得很好。已执行方法do_upload,但无法上载文件。这是我的密码 控制器路径如何在我们的项目中应用codeigniter文件上传程序?,codeigniter,Codeigniter,我用codeigniter做了一个简单的文件上传程序,然后作为我的特例,当我尝试在我的项目中应用时,程序运行得很好。已执行方法do_upload,但无法上载文件。这是我的密码 控制器路径C:\xampp\htdocs\pasar\application\modules\crud 上传路径C:\xampp\htdocs\pasar\uploads 查看v_foto.php <form method="post" action="<?php echo bas
C:\xampp\htdocs\pasar\application\modules\crud
上传路径C:\xampp\htdocs\pasar\uploads
查看v_foto.php
<form method="post" action="<?php echo base_url().'index.php/crud/c_foto/do_upload' ?>" enctype="multipart/form-data" >
<h3>Upload Foto </h3>
<input type="file">
<input type="submit" value="upload" />
</form>
首先:注意到您缺少的是输入中的name=“userfile”
Second:在控制器上,索引()中没有视图
Third:创建一个名为$input\u name=“userfile”的变量代码>就在do上传上方。然后在do\u upload添加$input\u name
类似$this->upload->do\u upload($input\u name)
第四:注意到的事情
$config['upload_path']='.uploads/'代码>
改为
$config['upload_path']='./uploads/'代码>
Codeigniter文档
用户指南:
这只是一个文件上传
看法
上传表单
控制器
<?php
// MX HMVC
// CI Codeigniter
class C_foto extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
/*
Main Directory:
application
uploads
system
index.php
*/
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0'; // No Limit
$config['max_width'] = '0'; // No Limit
$config['max_height'] = '0'; // No Limit
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Set your input name from <input type="file" name="userfile" size="20" />
$input_name = "userfile";
if ( ! $this->upload->do_upload($input_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
您需要使用我在1983年使用过的PHP函数,现在会出现一条新消息:上传路径无效,之后该路径无效,之后我更改$config['upload_path']='。/uploads/';对于$config['upload_path']=base_url().'uploads/',我认为进展不大,但还没有成功。您的uploads文件夹位于项目的主目录中吗?在这里是“C:\xampp\htdocs\pasar\uploads”,所以我称之为base_url()。进行动态定位。实际上,我在更改代码后获得了新的进展:$this->load->library('upload',$config);到$this->upload->initialize($config);然后错误信息变成:“上传的文件超过了PHP配置文件中允许的最大大小”
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('index.php/crud/c_foto/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
<?php
// MX HMVC
// CI Codeigniter
class C_foto extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
/*
Main Directory:
application
uploads
system
index.php
*/
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0'; // No Limit
$config['max_width'] = '0'; // No Limit
$config['max_height'] = '0'; // No Limit
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Set your input name from <input type="file" name="userfile" size="20" />
$input_name = "userfile";
if ( ! $this->upload->do_upload($input_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>