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().
Ich konnte Datei hochladen, wenn ich userfile als Name des Textfelds verwendet aber ich möchte es in meinen eigenen Namen umbenennen – sasy