简体   繁体   English

如何在codeigniter框架中上传文件

[英]how upload file in codeigniter framework

I am new in codeigniter. 我是codeigniter的新手。 I try to upload file but i didnt get the output. 我尝试上传文件,但我没有得到输出。 please help me to find out the answer. 请帮我找出答案。 my code is 我的代码是

view->upload_form.php 查看 - > upload_form.php

<!DOCTYPE html>
<html lang="en">
    <title>
        Upload Form
    </title>

<body>
    <?php echo $error; ?>
    <?php echo form_open_multipart('upload/do_upload'); ?>


        <input type="file" name="userfile" size="20"/>
        <br><br>
        <input type="submit" value="upload"/>
    </form>
</body>
</html>

view->upload_success.php 查看 - > upload_success.php

<!DOCTYPE html>
<html lang="en">
    <title>
        Upload Form
    </title>

<body>
    <h3>Your file was successfully uploaded!</h3>

    <ul>
        <?php foreach($upload_data as $item => $value); ?>
        <li><?php echo $item; ?>:<?php echo $value; ?></li>

    </ul>
    <p>
        <?php echo anchor('upload','upload another file!'); ?>
    </p>
</body>
</html>

controller->upload 控制器 - >上传

<?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';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $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);
        }
    }
}
?>

When i run the program then it shows the frontend of browse file,but when i select the file and clicked on upload button then it shows blank page. 当我运行程序然后它显示浏览文件的前端,但是当我选择文件并单击上传按钮然后它显示空白页面。

please help me to find out the answer 请帮我找出答案

check your './uploads/' permission. 检查你的'./uploads/'权限。 You can use chmod to change the permissions. 您可以使用chmod更改权限。

chmod('/path/to/project/folder/images/upload/',0777);

Your code is works fine. 你的代码工作正常。 I have tested it on my web server. 我在我的网络服务器上测试过它。 It must be a problem with your permissions to the folder, or the place of your folder. 您对文件夹的权限或文件夹的位置一定是个问题。 Is your uploads folder placed in the directory where you have installed CodeIgniter? 您的uploads文件夹是否位于已安装CodeIgniter的目录中?

Place of your folder 您的文件夹的位置

In the CodeIgniter upload library, the 'upload_path' parameter is set to relative to the CodeIgniter folder. 在CodeIgniter上传库中,'upload_path'参数设置为相对于CodeIgniter文件夹。

$config['upload_path'] = './uploads/';

So this means the folder structure needs to be: 所以这意味着文件夹结构需要:

/system/..
/application/..
/uploads/..
/../..

Your uploads folder has to be at the same folder as the system or application folder. 您的uploads文件夹必须与系统或应用程序文件夹位于同一文件夹中。

Permissions 权限

In my webserver, I have created the uploads folder with permissions: 755. You can see the permissions of your folder anytime in your FTP program (in FileZilla: right click on the folder > File permissions..). 在我的网络服务器中,我创建了具有权限的uploads文件夹:755。您可以随时在FTP程序中查看文件夹的权限(在FileZilla中:右键单击文件夹>文件权限..)。 If it's not 755 (or higher), you can also try to set it to this value. 如果它不是755(或更高),您也可以尝试将其设置为此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM