Two examples of writing Codeigniter file batch upload controller
- 2021-07-09 07:35:05
- OfStack
Example 1:
/**
* Multiple file upload
*
* @author Dream <dream@shanjing-inc.com>
*/
public function multiple_uploads() {
// Load the required class library
$this->load->library('upload');
// Configure upload parameters
$upload_config = array(
'upload_path' => '',
'allowed_types' => 'jpg|png|gif',
'max_size' => '500',
'max_width' => '1024',
'max_height' => '768',
);
$this->upload->initialize($upload_config);
// Loop through uploaded files
foreach ($_FILES as $key => $value) {
if (!empty($key['name'])) {
if ($this->upload->do_upload($key)) {
// Upload succeeded
print_r($this->upload->data());
} else {
// Upload failed
echo $this->upload->display_errors();
}
}
}
}
Example 2:
function upload() {
$config['upload_path'] = './uploads/';
/* Here's uploads Is relative to index.php Adj. , That is, the entry file , Don't make a mistake about this !
Otherwise, an error will be reported "The upload path does not appear to be valid.";
*/
$config['allowed_types'] = 'gif|jpg|png';
/* I try to upload other types of files , Here 1 Be sure to pay attention to the order !
A problem was encountered while attempting to move the uploaded file to the final destination.
This error 1 Generally, the file name of the uploaded file cannot be a Chinese name , This is very depressing ! Not yet resolved , You can use other methods , Re-alter 1 The file name can be solved !
$config['allowed_types'] = 'zip|gz|png|gif|jpg';( Correct )
$config['allowed_types'] = 'png|gif|jpg|zip|gz';( Errors )
*/
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = time(); // File name does not use original name
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
echo $this->upload->display_errors();
}else{
$data['upload_data']=$this->upload->data(); // Documentary 1 Some information
$img=$data['upload_data']['file_name']; // Get the file name
echo $img."<br>";
foreach($data['upload_data'] as $item => $value){
echo $item.":".$value."<br>";
}
}
}