2016-03-24 3 views
2

Ich versuche, zwei Tasten Datei-Upload wie in codeigniter zu verwenden, unterVerwenden Sie zwei Datei-Upload-Button in 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" > 

in Controller

$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']; 
    } 

ich so versucht, aber es gab Sie haben Wählen Sie keine Datei zum Hochladen aus. Irgendwelche Hilfe, wie man das macht ??? danke

Antwort

0

zuerst in der php.ini file_uploads = On

Sekunde, die von

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

dritten Überprüfung, dass auf codeigniter documentaion über Upload-Datei sicher sein. https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

vergessen Sie nicht, die Erweiterung von Dateien

+0

Ich konnte Datei hochladen, wenn ich userfile als Name des Textfelds verwendet aber ich möchte es in meinen eigenen Namen umbenennen – sasy

0

allowd Wenn Sie Bibliotheksdatei unter Systemordner sehen upload.php dann werden Sie wissen, dass CI ‚userfile‘ Feldnamen als Standard nimmt, also, wenn Sie tun

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

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

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

Versuchen vorbei Feldnamen, wie Sie in anderem Zustand getan haben, oder eines des Eingabefeldnamen ‚userfile‘ zu setzen.

3

Wenn Sie Ihr Formular in der Frage sehen, gehe ich davon aus, dass Sie zwei Dateien aus zwei verschiedenen Eingabefeldern hochladen möchten. Ist es das ?

Also, es dir in den Weg machen, sollten Sie Ihre Form, wie sein:

<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> 

Und Ihre Controller sollte so sein:

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' 
     } 
    } 
} 

Nehmen wir nun an, Sie eine neue Bilddatei sein wollen hochgeladen an einem anderen Ort oder was auch immer, von der gleichen Form; dann in der Config-Array, müssen Sie nur die Array-Elemente ändern, die Sie anders sein wollen als:

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

Dann müssen Sie wie die Config Array initialisieren:

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

und dann haben Sie

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

und jetzt können Sie die lets_upload() Funktion aufrufen: die Upload-Klasse mit dieser neuen Konfiguration laden

$this->lets_upload('img_file'); 

in der Funktion upload().

Verwandte Themen