在codeigniter

时间:2016-03-24 09:16:20

标签: php codeigniter

我正在尝试在codeigniter中使用两个文件上传按钮,如下所示

<label class="control-label" for="default">PO File</label>
<input type="file"  id="po_file" name="po_file"  multiple="multiple" >
<label class="control-label" for="default">Invoice File</label>
<input type="file"  id="in_file" name="in_file"  multiple="multiple" >
控制器中的

    $file1 = $_FILES['po_file']['name'];
    $file2 = $_FILES['in_file']['name'];
    $config['upload_path'] = $pathToUpload;
    $config['allowed_types'] = 'pdf';
    $config['overwrite' ] =TRUE;
    $config['max_size'] =0;


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

    if ( ! $this->upload->do_upload())
    {
        echo $this->upload->display_errors();

        // $this->load->view('file_view', $error);
    }
    else
    {

        $this->upload->do_upload(file1);
        $upload_data = $this->upload->data();
        $file_name = $upload_data['file_name'];
    }

我试过这样但是它给了 您没有选择要上传的文件。 任何帮助如何做到这一点??? 谢谢

3 个答案:

答案 0 :(得分:3)

在问题中看到您的表单,我假设您希望从两个不同的输入字段上传两个文件。是吗?

所以,按照自己的方式做,你的表格应该是:

<form enctype="multipart/form-data" method="post">    <!-- enctype="multipart/form-data" is must, method 'post' or 'get' is depend on your requirement -->

    <?php
        if( !empty( $notification ) )
        {
            echo '
            <p>Notifications : </p>
            <p>'.$notification.'</p>';    <!-- For the status of the uploaded files ( error or success ) -->
        }
    ?>

    <label for="default">PO File</label>
    <input type="file" name="po_file">    <!-- no need to add "multiple" attribute, unless you want multiple files to be uploaded in the same input field-->
    <label for="default">Invoice File</label>
    <input type="file" name="in_file">

    <input type="submit" name="upload">

</form>

你的控制器应该是:

class Image extends CI_Controller {

    private $data;    // variable to be used to pass status of the uploaded files ( error or success )

    function __construct()
    {
        // some code
    }

    public function upload()
    {
        $this->data['notification'] = '';

        if( $this->input->post('upload') )    // if form is posted
        {
            // setting the config array

            $config['upload_path']      = 'uploads/';    // $pathToUpload ( in your case )
            $config['allowed_types']    = 'pdf';
            $config['max_size']         = 0;

            $this->load->library('upload', $config);    // loading the upload class with the config array

            // uploading the files

            $this->lets_upload( 'po_file' );    // this function passes the input field name from the form as an argument

            $this->lets_upload( 'in_file' );    // same as above, function is defined below
        }

        $this->load->view('form', $this->data);    // loading the form view along with the member variable 'data' as argument
    }

    public function lets_upload( $field_name )    // this function does the uploads
    {
        if ( ! $this->upload->do_upload( $field_name ))    // ** do_upload() is a member function of upload class, and it is responsible for the uploading files with the given configuration in the config array
        {
            $this->data['notification'] .= $this->upload->display_errors();    // now if there's is some error in uploading files, then errors are stored in the member variable 'data'
        }
        else
        {
            $upload_data = $this->upload->data();    // if succesful, then infomation about the uploaded file is stored in the $upload_data variable

            $this->data['notification'] .= $upload_data['file_name']." is successfully uploaded.<br>";    // name of uploaded file is stored in the member variable 'data'
        }
    }
}

现在假设您希望将新图像文件从同一表单上传到不同的位置或其他任何位置;然后在配置数组中,你只需要改变你想要不同的数组元素:

$config['upload_path'] = '/gallery';
$config['allowed_types'] = 'gif|jpg|jpeg|png';

然后你必须将配置数组初始化为:

$this->upload->initialize($config);    // *** this is important ***

然后你必须使用这个新配置加载上传类:

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

现在你可以调用lets_upload()函数:

$this->lets_upload( 'img_file' );

在upload()函数中。

答案 1 :(得分:0)

首先在你的php.ini中    file_uploads = On

第二个确定

<form action="controller/action" method="post" enctype="multipart/form-data">

第三次检查关于上传文件的codeigniter文件。 https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

不要忘记允许的文件扩展名

答案 2 :(得分:0)

如果您在系统文件夹下看到upload.php库文件,那么您将知道CI将'userfile'字段名称作为默认值,因此当您执行

if ( ! $this->upload->do_upload())
{
    echo $this->upload->display_errors();

    // $this->load->view('file_view', $error);
}

//Passing parameter empty, then CI search for 'userfile'.

尝试在else条件下传递字段名称,或者将输入字段名称之一设置为'userfile'。