Upload problem with PHP: Thumbnail picking up the image name - php

I have the following model(codeigniter) code to upload image and thumbnail.
However the result of image path becomes, images/comfort_big.jpg and images/comfort_big.jpg.jpg.
The thumbnail image picks up the name of image and add .jpg.
I am uploading images/confort_thumb.jpg for thumb.
Can anyone tell me what's wrong please?
if ($_FILES){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (strlen($_FILES['image']['name'])){
if(!$this->upload->do_upload('image')){
$this->upload->display_errors();
exit();
}
$image = $this->upload->data();
if ($image['file_name']){
$data['image'] = "images/".$image['file_name'];
}
}
if (strlen($_FILES['thumbnail']['name'])){
if(!$this->upload->do_upload('thumbnail')){
$this->upload->display_errors();
exit();
}
$thumb = $this->upload->data();
if ($thumb['file_name']){
$data['thumbnail'] = "images/".$thumb['file_name'];
}
}
}

I had a quick look at File Uploading Class in the user guide
In the preferences tables it states:
Note:The filename should not include a file extension.
So my guess is that you get the user to name the file
$config['file_name'] = "User defined";
or remove the extension programmatically. Since you know and list the extensions already you could do
$types = array(".gif",".jpg",".png");
$image['file_name'] = str_replace($types , "", $image['file_name'] );

Your problem is that when you create a thumb you are passing the full image.
So it will add another extension to it.
Use Below code to remove extension from file name.
<?php
$filename=$image['file_name'];
$file_name_with_dot = substr($filename,0, strrpos($filename, '.') + 1);
$only_file_name = substr($file_name_with_dot,0,strlen($file_name_with_dot)-1);
?>
Now pass this $only_file_name variable to your thumb function..
It will be better to use automatic thumb creation from main image.
Hope this will help for you.

Related

adding timestamp while uploading image file is not working

I am developing a ad site that anyone able to upload 3 images.
So I am coding to upload that 3 images by adding timestamp in front of their file name to make them unique. I use codeigniter framework and attaching the code below.
when I submitting form data, localhost server shows that images have been saved correctly with some code infornt of image filename. But problem is there are no images saved in relevant image folder in that name and I cannot retrieve that images in next preview advertisement page.
I don't know much about php or codeignitor. Your help is highly appreciated.
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5120;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'no_image.jpg';
}
else {
$data = array('upload_data' => $this->upload->data());
$post_image1 = time().$_FILES['userfile1']['name'];
$post_image2 = time().$_FILES['userfile2']['name'];
$post_image3 = time().$_FILES['userfile3']['name'];
}
$result = $this->post_model->adregister($post_image1,$post_image2,$post_image3);
You can append time to 'filename' in config of upload library
$config['file_name'] = time().$_FILES['userfile1']['name'];
Or if you want unique name for all files the simply add
$config['encrypt_name'] = TRUE;
then
$this->load->library('upload', $config);
Try this one:-
$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];
I've written a possible solution for your code. You haven't shared your full code so you'll have to fill in the gaps yourself and maybe make some changes here and there; Comments are mentioned wherever necessary. See if it helps you.
public function your_function_name(){
// your-code
// your-code
// check if file is uploaded in field1
if(!empty($_FILES['userfile1']['name'])){
// call function to upload file
$userfile1 = $this->upload_file('userfile1');
}
// check if file is uploaded in field2
if(!empty($_FILES['userfile2']['name'])){
$userfile2 = $this->upload_file('userfile2');
}
// check if file is uploaded in field3
if(!empty($_FILES['userfile3']['name'])){
$userfile3 = $this->upload_file('userfile3');
}
$result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);
}
// function to upload file
function upload_file($filename){
$config['file_name'] = time().$_FILES[$filename]['name']; // append time to filename
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
$config['max_size'] = 5120;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploaded = $this->upload->do_upload($filename);
if ( ! $uploaded ){
$error = array('error' => $this->upload->display_errors());
$file = 'no_image.jpg'; // default file
}else{
$upload_data = $this->upload->data();
$file = $upload_data['file_name']; // uploaded file name
}
return $file; // return filename
}

Rename image name after upload in codeigniter

I need to rename image name after upload because my requirement is I need to add id with it so it's possible after store image name in DB.
I have done code for uploading image with and without watermark into different folder but now I want to change it's name like:
$imageName = $imgId.''.randomstring.''.$ext;
I have tried with below code but same image stored in both folder with watermark. I want to store original image in original_image folder and with watermark image in watermark_folder. Please help me to resolve this issue.
$getImgs = $this->photographer_model->get_uploaded_image($result);
$getExt = substr($getImgs[0]['img_name'], strrpos($getImgs[0]['img_name'],'.')+0);
$randomStr = $this->generate_random_string(20);
$fileName = $result."-".$randomStr."".$getExt;
$originalImgPath = base_url()."assets/original_image/".$fileName;
$watermarkImgPath = base_url()."assets/watermark_image/".$fileName;
$uploadOrgImg = 'assets/original_image/'.$fileName;
$uploadWaterImg = 'assets/watermark_image/'.$fileName;
$updateImg = $this->photographer_model->update_img_path($result, $fileName, $originalImgPath, $watermarkImgPath);
if(file_exists('assets/original_image/'.$getImgs[0]['img_name']) || file_exists('assets/watermark_image/'.$getImgs[0]['img_name'])) {
unlink('assets/original_image/'.$getImgs[0]['img_name']);
unlink('assets/watermark_image/'.$getImgs[0]['img_name']);
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('image');
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['file_name'] = $fileName; // set the name here
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('image');
}
Update Your Code Below With this
$config1['upload_path'] = 'assets/original_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['encrypt_name'] = TRUE;
And Update for the second one
$config['upload_path'] = 'assets/watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['encrypt_name'] = TRUE;

Codeigniter Resize function not working

Hi friends I am trying to use codeigniters resize image_lib .. and I am not able to resize the image using this one. Please help me to solve this issue
Error:
using gd:
The path to the image is not correct.Your server does not support the GD function required to process this type of image.
using ImageMagick:
The path to the image is not correct.The path to your image library is not correct. Please set the correct path in your image preferences.
Code:
$this->load->library('upload');
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = false;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
return false;
}
else{
$this->load->library('image_lib');
$resize['image_library'] = 'gd2';
$resize['source_image'] = $path.$file_name;
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 40;
$resize['height'] = 40;
$resize['quality'] = 100;
// print_r($path.$file_name);
// Here the path of the image is assets/Data/adv_images/2/2-537f2a3651300
// which is absolutely right also tried base_url().$path.$file_name
$this->image_lib->initialize($resize);
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
return true;
}
try to set path by this way:
$resize['source_image'] = $this->upload->data()['full_path'];

Rename and replace an image in my images folder php

I'm a newbie working on an app in which i allow users to upload their pics and when they need to change the pic I can't delete the old pic and hence cant rename too. Here's my code to upload an img as per tutorial in codeigniter.
$config['upload_path'] = './img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['file_name'] = $id.'_img';
$this->load->library('upload', $config);
if ( $this->upload->do_upload())
{
$this->view_data['message'] = "File Uploaded";
}
Here I pass the user id and hence need to rename and replace the old image with the name like 44_img..... Any help would be appreciated , Thanks in advance....
Try this for deletion of the old file:
$oldfile = $config['upload_path'].$config['file_name'];
if( file_exists( $oldfile ) ){
unlink( $oldfile );
}
You can use $this->upload->data() to retrieve data related to upload, and inside do something like that:
if ( $this->upload->do_upload())
{
$this->view_data['message'] = "File Uploaded";
$data = $this->upload->data();
$path = $data['full_path'];
$dir = dirname($path);
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(rename($path, $dir.'/'.$your_file_new_name.'.'.$ext)) {
$this->view_data['message'] .= '<br/> And renamed';
}
}
Note that you must have enough rights to upload your files, especially write righths on a directory you want to upload to. Also, good practice is to give files unique names and separate them into user related directories. For example, you can combine mysql users id with image id.
if you want to over write the existing one, i.e., delete the older one and save new one with same name then
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if you want to keep both, better save the new one with old one, the code will automatically rename the new one and you can save the name of new uploaded image to access it
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
$uploaded_filename = $this->CI->upload->file_name; //name of the save image
$this->view_data['message'] = "File Uploaded";
}

Codeigniter, how to save uploaded file to new folder?

I'm adding onto an admin CMS, and need image files uploaded to save to a separate folder I have specified. I can save them to a predefined folder of another controller, but how do I save them to the new folder I have created?
When I try, it doesn't allow it. I know it has something to do with routing, but where to I change codeigniter to allow that? I have looked in config/routes.php and don't see it listed there.
Thank you!
you can try this
function uploadFile($uploadFile,$filetype,$folder,$fileName='')
{
$resultArr = array();
$config['max_size'] = '1024000';
if($filetype == 'img') $config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = './uploads/'.$folder.'/';
if($fileName != "")
$config['file_name'] = $fileName;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload($uploadFile))
{
$resultArr['success'] = false;
$resultArr['error'] = $this->upload->display_errors();
} else {
$resArr = $this->upload->data();
$resultArr['success'] = true;
$resultArr['path'] = "uploads/".$folder."/".$resArr['file_name'];
}
return $resultArr;
}

Categories