codeigniter image upload successful, but file not uploaded - image

I have been trying to upload an image using CodeIgniter's File Upload class. The image is used for a user's avatar. Here is a snippet for my code:
//upload file
if($_FILES['image']['name'] != ''){
if (!file_exists($dirpath))
mkdir($dirpath);
$this->load->library('upload');
$config['upload_path'] = $dirpath;
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['file_name'] = $_FILES['image']['name'];
$config['overwrite'] = true;
$config['remove_spaces'] = false;
$this->upload->initialize($config);
$data = "";
$error = "";
$photo_result = $this->upload->do_upload('image');
if ($photo_result)
if($action == 'update'){
foreach (glob($dirpath.'/'.$old_image.'*') as $file)
if (file_exists($file))unlink ($file);
}
if($new_resto->resized == 0){
if(resize_image($widths,$theuser))
$this->usermodel->update($theuser->id, array('resized'=>1), 'object',$authorization);
}
echo json_encode(array('config'=>$config,'upload_data'=>$this->upload->data()));
}
}
The file upload is supposedly successful, because when I print out $this->upload->data(), it shows that the upload data is not empty. The strange thing is, this code works for creating new users, but when I try to update users, the file is not uploaded. I have tried using chmod($dirpath, 0777) before the upload, and chmod($dirpath, 0755) after, still no luck.
Any ideas? Thanks.

My mistake, I assigned $old_image after I updated the data, so $old_image contains the current image name, causing it to be deleted when i call unlink($file).
Problem solved :)

Please update your Upload.php class with previous version of CodeIgniter

Related

how to use database saved links of image to get images from laravel folder

i have saved images in 'public/images' folder in Laravel and the link/name is saved in mysql database. all i want to get all images via api call. the problem is i can get a single image easily but couldnt get all the images.[[[enter image description here](https://i.stack.imgur.com/9gerk.jpg)](https://i.stack.imgur.com/MIBIk.jpg)](https://i.stack.imgur.com/QcPQm.jpg)
i have no issue with single image however unable to get all at once. thanks
Try this instead
Store the only image name and extension into database and then retrieve it by using DB and use asset to get image, refer the following code as practice.
$custom_data = array();
for ($i = 0; i <= $total_images; $i++) {
$image_name = $data[$i]->image;
if ($image_name != "") {
$image_with_path = asset('public/products/') . '/' . $image_name;
} else {
$image_with_path = "";
}
$custom_data['image'] = $image_with_path;
}

Laravel 8 HTTP Put With Attach

I'm creating crud API with laravel 8 as a server and it works perfectly when tested by talend/postman (running on 127.0.0.1:8000).
then i'm creating crud apps with laravel 8 as a client. everything works fine but update data with attach file.
i've try with no attach file and works
$response = Http::put('http://127.0.0.1:8000/api/memo/'.$id_memo, $input);
but, its not works when using attach file
$input['id_user'] = $request->id_user;
$input['date_memo'] = $request->date_memo;
$input['time_memo'] = $request->time_memo;
if ($request->hasFile('lampiran_memo')) {
$lampiran_memo = $request->file('lampiran_memo');
$nama_lampiran = $lampiran_memo->getClientOriginalName();
$lampiran_memo->move("memo", $nama_lampiran);
$thefile = fopen("memo/".$nama_lampiran, 'r');
$response = Http::attach('lampiran_memo', $thefile)->put('http://127.0.0.1:8000/api/memo/14', $input);
}
after stuct, finally I use attach and post (not put) and works fine.
$input['id_user'] = $request->id_user;
$input['date_memo'] = $request->date_memo;
$input['time_memo'] = $request->time_memo;
if ($request->hasFile('lampiran_memo')) {
$lampiran_memo = $request->file('lampiran_memo');
$nama_lampiran = $lampiran_memo->getClientOriginalName();
$lampiran_memo->move("memo", $nama_lampiran);
$thefile = fopen("memo/".$nama_lampiran, 'r');
$response = Http::attach('lampiran_memo', $thefile)->post('http://127.0.0.1:8000/api/updatememo', $input);
}

Using imagejpeg() in Laravel

I am migrating my existing PHP program to Laravel. I know the upload should be $request->files->store('images'); but this doesn't compress the images beforehand.
I have found Intervention Image plugin for Laravel but I don't want to use it because I want it to be pure PHP only.
My working code from pure PHP was:
$info = getimagesize($tmpFilename);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($tmpFilename);
} else if ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($tmpFilename);
} else if ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($tmpFilename);
}
imagejpeg($image, $path, 60);
I know the images get saved at /storage/app/public/ so I used that path for imagejpeg as well. This is my laravel code:
$tmp_file = $inner->getPathName();
imagejpeg($tmp_file, '/storage/app/public/images/hehe.jpg', 60);
But it doesn't work. It only says
imagejpeg(/storage/app/public/images/hehe.jpg): failed to open stream: No such file or directory
Any help would be appreciated

Codeigniter and Ckfinder csrf_exclude_uris

I'm having an issue with Codeigniter 3 and CKfinder regards the CSRF Protection
If I use the below in my Codeigniter Config file CKFinder image upload works fine
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
if I change the $config['csrf_protection'] = TRUE; CKFinder image uploads fail
What I need is to be able to exclude CKFinder from falling under the CSFR Protection - I've tried the below but nothing seems to work:
$config['csrf_exclude_uris'] = array('assets/plugins/ckfinder/.*+', 'assets/plugins/ckfinder/ckfinder.js', 'assets/plugins/ckfinder', 'admin/news/.*+');
Any pointers would be appreciated
Hope this may work. it worked in my case.
$config['csrf_exclude_uris'] = array('assets/plugins/ckfinder/[\s\S]*');
but as far my knowledge "assets" resources don't need any csrf protection.
# Its work fine #
$config['csrf_protection'] = TRUE;
if(isset($_SERVER["PHP_SELF"])){
$parts = explode("/",$_SERVER["PHP_SELF"]);
$exclude_url_arr = array('login');
if (!empty($exclude_url_arr[0])) {
foreach($parts as $part) {
if (in_array($part,$exclude_url_arr)) {
$config['csrf_protection'] = FALSE;
break;
}
}
}
}
URL which submit the form data.
Add lines below in config.php file. This will work.
$config['**csrf_exclude_uris**'] = array(
*'device/deviceProcess/pushData'*
);
-- deviceProcess : Controller
--- pushData : Method
----- URL : http://hostname/index.php/device/deviceProcess/pushData

CodeIgniter Database Connection Issue

I'm using CodeIgniter, and I'm not able to use my database with it. I'm following this tutorial exactly, but when I open in localhost I receive a 404 page.
You need to set up your application/config/database.
Here you need to add your information for your database.
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "yourdbusername";
$db['default']['password'] = "password_for_db";
$db['default']['database'] = "name_of_db";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
Have you done it?
If you're getting a 404, it may not be your database connection. Check your routing, and be sure that you are accessing a page that does indeed exist. If you're accessing a controller that loads a view, stop loading the view for now and test the connection directly from the controller - output "YES" if it is, and "NO" if anything else.
If your problem persists, double-check your database-connection configuration. Lastly, I would suggest you follow the documentation rather than another website when you are getting familiar with a framework. You can view the documentation for the Database class at http://codeigniter.com/user_guide/database/index.html
Do you try http://localhost/CodeIgniter/index.php/employee/GetAll ?
Are you using CodeIgniter 2 ?

Resources