Setting a config value dynamically in the controller in Codeigniter? - codeigniter

I'm trying to upload an image using Codegniter's Upload Library, the image should be uploaded to a directory which is created and returned by the model, do the upload directory changed every time, this is the code I'm supposed to use to change the config upload array:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload');
$this->config->set_item('upload', array('upload_path' => $path));
// I've also used
// $this->config->set_item('upload_path', $path);
if (!$this->upload->do_upload('image'))
echo $this->upload->display_errors();
else {
$image = $this->upload->data();
$data['image'] = $image['file_name'];
}
}
but it doesn't seem to work. The upload_path doesn't change, the image is still uploaded in the path I've specified in config/upload.php file.

Try this:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload', TRUE);
$config = $this->config->item('upload');
$config['upload_path'] = $path;
$this->upload->initialize($config);
//etc.
}

Related

How to choose were Image Intervention should store the images?

Im trying to save a resized copy of an image that has been uploaded, but Image Interventions doesent seem to know in what root directory to begin in, or maybe it is just me that dosent know how to configure it propertly.
This is my code in AvatarImageController#store
public function store(Request $request, Game $game)
{
if ($request->hasFile('game_image')) {
$request->validate([
'game_image' => 'mimes:jpeg,png|max:1014'
]);
$file = $request->file('game_image');
$filename_thumbnail = "thumb_". $file->hashName();
$path_full = $request->file('game_image')->store('images/test_folder');
$path_thumb = $request->file('game_image')->storeAs('images/test_folder/thumbs', $filename_thumbnail);
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
$image = new Image;
$image->full = basename($path_full);
$image->thumb = $filename_thumbnail;
$game->images()->save($image);
return back()->with('success', "Success!! Image uploaded.");
}else{
return back()->with('success', 'Ooops.. something went wrong.');
}
abort(500, 'Could not upload image :(');
}
The error i receive:
Intervention\Image\Exception\NotReadableException
Image source not readable
I suspect this is because im not in the correct path, because of these lines from above function:
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
And it is a real hassel working with explicit paths when using git as deployment.
You can make image directly like this:
$uploadedFile = $request->file('game_image');
$image = Image::make($uploadedFile);
Resize it:
$image = $image->resize(300, 200);
and save it like this:
$image_data = $image->encode('jpg')->__toString();
$relative_file_path = 'images/test_folder/';
Storage::put($relative_file_path, $image_data)
use the storage() helper?
Intervention::make(storage_path('app/public/") . $path_thumb)

Is it possible to move the files in folder to different subfolders according to database school_id

I have done an image upload function for large registration functionality. My uploaded files are stored in a single folder.. but I want to move those files into a separate folder with id stored in the database. is it possible to move those files into separate folders now??
I want to move the uploaded files into a folder with the name of id in the database. I cant do it manually since the uploads folder has many thousands of images. is it possible to do it in code?
public function add_applicants()
{
if (isset($_FILES['photo']) == 1) {
$config['upload_path'] = FCPATH . 'uploads/';
$this->upload_path = $config['upload_path'];
if ($this->validate_upload_path() == TRUE)
{
$config['upload_path'] = $this->upload_path;
}
$config['allowed_types'] = 'jpg';
$config['max_size'] = 2000;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = true;
$config['file_name'] = date('Ymdhis') . '.jpg';
$file_name = $config['file_name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo')) {
$this->upload->display_errors();
}
}
$data = $this->input->post();
$data['photo'] = $file_name;
$insetapplicants = $this->Welcome_Model->insertApplicants($data);
if ($insetapplicants == 'Success') {
echo "<script>
alert('New Applicants Added Successfully');
window.location.href='dashboard';
</script>";
}
}
public function index() {
$data['mediumwise_details'] = $this->Welcome_Model->mediumwise_home();
$this->load->view('home',$data);
}
I expect that the files are moved to different folders
For example, my uploads folder has 10 photos of different school_Id stored in the same folder. but I want if 2 photos are from one school_id means, a folder name with that school_id is created and that 2 photos are placed inside that folder. likewise different folder for different school_id 's
I hope the following code will help you. let me know if it does not work.
create on function or method in controller place following code and run that function make necessary changes I have created below code based on the information you have given us.
$result = $this->db->get('mts_applicants')->result_array();
if($result){
foreach($result as $row){
$old_path = FCPATH . 'uploads/';
$new_path = FCPATH . 'uploads/'.$row['school_code'].'/';
$old_name = $old_path.$row['photo'];
$new_name = $new_path.$row['photo'];
if(!is_dir($new_path)){
mkdir($new_path, 0777, true);
}
rename($new_name, $old_name) ;
}
}

Laravel Image source not readable error

I am working on uploading a video locally and then use it locally to upload to s3. It successfully uploads to s3 but I get this error:
Image source not readable
/var/www/yt/vendor/intervention/image/src/Intervention/Image/AbstractDecoder
I found this error when I was trying to delete the image after I uploaded it to s3. I think it is the reason the delete does not work. How do I fix the error?
public function avatar(Request $request)
{
$imageData = $request->get('image');
$img = Image::make($request->get('image'))->fit(300)->encode('jpg');
// calculate md5 hash of encoded image
$hash = md5($img->__toString());
// use hash as a name
$path = "avatars/{$hash}.jpg";
// save it locally
$test = $img->save(storage_path($path));
// $url = "/images/{$hash}.jpg"
$url_local = $path;
$path = Storage::putFile('avatars', new File(storage_path($url_local)));
if($path){
//TODO make into event
Storage::disk('local')->delete(storage_path($url_local));
// \Debugbar::error($test);
}
$user = User::find(Auth::id());
$user->avatar_url = $path;
if ($user->save()) {
return response()->json(['avatar' => $user->gen_avatar()]);
}
}

How to upload image file using codeigniter?

I want upload image file on server & want to stored image in database.
How can I store image file in database & also get the image file from database. Please help to solved this problem.
i have done this code in project..
but when uploading image on server upload_path not found.
In controller
$config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path
$config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/
$config['max_size'] = '3048';// maximum file size that can be uploaded (2MB)
$this->load->library('upload',$config);
if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
{
echo( $config['upload_path']);
die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
$this->load->library('upload',$config); /* this loads codeigniters file upload library*/
}
if ( !$this->upload->do_upload() )
{
$error=array('error'=>$this->upload->display_errors());
//echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
// $this->load->view('main_view',$error);
}
else
{
$file_data=$this->upload->data();
$data['img']=base_url().'.application/upload/'.$file_data['file_name'];
echo("Image upload successfully..");
//================================================
// $this->load->model('insert_model');
// $this->insert_model->submit_image();
//==========================================
//$this->insert_model->insert_images($this->upload->data());
// $this->load->model('insert_model');
//$this->insert_model->submit_image();
}
That is quite easy. Just create image upload function once in your controller. Here is one function I wrote:
function image_upload($path, $size, $width, $height, $new_name, $ext)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = $size;
$config['file_name'] = time() . $new_name . "." . $ext;
$config['max_width'] = $width;
$config['max_height'] = $height;
$this->load->library('upload', $config); //Load codeigniter's file upload library
if (!$this->upload->do_upload())
{
return false;
} else
{
return $config['file_name'];
}
}
Now in the action function of form where you have file input field call this function as follows do as follows:
if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user
{
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file
$picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext);
//Now save this $picture_name in database
}
else
{
print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully
}
Thats it.
Read This, then you will get how to upload file.
Now how to store it into DB.!
In controller use this :
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$img_data = array('upload_data' => $this->upload->data());
$full_url = "/uploads/".$img_data['upload_data']['file_name'];
$reply = $this->model_name->upload_image($full_url);
}
In model using upload_image($full_url) function store image name with path, so that you can get back that path whenever needed.

How to store uploaded file into folder using CodeIgniter?

How to store upload file into folder using CodeIgniter?
Please Be sure with your gallery folder it must be in root alongside with application folder name gallery
$config['upload_path']='./gallery/';
$config['allowed_types']='gif|png|jpeg|jpg';
$config['max_size']='100';
$config['max_width']='1024';
$config['max_height']='700';
$this->upload->initialize($config);
$this->upload->do_upload('upload_photo');
Hey try this code inc controller. This the function when u hit the upload button in your vie form..
function sendresume()
{
$config['upload_path'] = 'C:\xampp\htdocs\upload\application'; //path where to save in the systme
$config['allowed_types'] = 'doc|docx|pdf'; //file types to accept while uplaoding
$config['max_size'] = '10240'; //size limit
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "Resume Successfully uploaded to database.............";
$file = $data['upload_data']['full_path']; //upload file path
}
}

Resources