我正在尝试在代码点火器中使用两个文件上传按钮,如下面的
<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'];
}
我试过了,但它失败了您没有选择要上载的文件。有什么帮助吗???感谢
看到问题中的表单,我假设您希望从两个不同的输入字段上传两个文件。就是这样吗?
所以,按照你的方式,你的表格应该是:
<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()函数中。
在php.ini中的第一个file_uploads=在上
第二,确保
<form action="controller/action" method="post" enctype="multipart/form-data">
第三次检查关于上传文件的codeigniter文档。https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
不要忘记允许的文件扩展名
如果您在系统文件夹下看到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"。