Created
January 13, 2021 12:05
-
-
Save zyryc/2996301a2a59e88f721c3ae1a230bd24 to your computer and use it in GitHub Desktop.
codeigniter 3 file upload method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Event extends MY_Controller{ | |
function __construct() | |
{ | |
parent::__construct(); | |
} | |
//$file is the file name from form input | |
//example is <input type="file" name="my_image" > | |
//so $file = "my_image" | |
//this for codeigniter | |
function doUpload($file) { | |
//configuration for upload your images | |
$imagePath = realpath(APPPATH . '../assets/uploads/events'); | |
$config = array( | |
'file_name' => time().uniqid(), | |
'allowed_types' => 'jpg|jpeg|png|gif', | |
'max_size' => 3000, | |
'overwrite' => FALSE, | |
'upload_path' | |
=>$imagePath | |
); | |
$this->upload->initialize($config); | |
$errCount = 0;//counting errrs | |
$data=[]; | |
if (!$this->upload->do_upload($file)) | |
{ | |
$error = $this->upload->display_errors(); | |
$theImages[] = array( | |
'errors'=> $error | |
);//saving arrors in the array | |
$this->session->set_flashdata('error', $error); | |
} | |
else | |
{ | |
$filename = $this->upload->data(); | |
$theImages[] = array( | |
'fileName'=>$filename['file_name'] | |
); | |
$params = array('file_name' => '/assets/uploads/events/'.$filename['file_name'], ); | |
$data = '/assets/uploads/events/'.$filename['file_name']; | |
}//if file uploaded | |
return($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment