我使用了来自CI
的确切代码。https://www.codeigniter.com/user_guide/libraries/file_uploading.html 但它总是给我相同的错误,上传路径似乎无效。
文件夹在那里,路径没有问题。文件夹有写权限。
控制器代码如下
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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';
print_r($config);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//echo 'path : '.$config['upload_path'];
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
//echo "submited";
}
}?>
我把问题解决了。我在控制器文件夹中创建文件夹。上传文件夹应该在安装Codeignator的根文件夹中创建。在我的例子中,CI安装在这个路径
localhost/CI/
所以我创建了名称为upload的文件夹,它适用于我。
在应用程序文件夹目录下创建一个名为'uploads'
的文件夹。在你的控制器中更改你的上传路径编码行,如下所示。
$config['upload_path'] = './uploads/';
你的问题在下一行
$this->load->library('upload', $config);
$this->upload->initialize($config);
如果不这样做,只需输入
$this->upload->initialize($config);