I have a form for inserting new article .my problem is in saving the full path of picture to database.The picture is successfully uploaded to a folder and I can get the path of picture without its name and extension.can you tell me what is wrong with my code?
Thanks.
I take the upload path with this function:
$path=dirname($_SERVER['SCRIPT_FILENAME']).'/upload';
$data=array('upload_data'=>$this->upload->data());
$upload_path=$path.$data['upload_data']['file_name'];
when I echo the
echo $data['upload_data']['file_name'];
it shows nothing.
I can see one mistake in your code and it is:
$path=dirname($_SERVER['SCRIPT_FILENAME']).'/upload'; // <-- No ending slash
$data=array('upload_data'=>$this->upload->data());
$upload_path=$path.$data['upload_data']['file_name']; // <-- No slash added here
So, you should separate the directory name and file name using a slash when concatenating like:
$upload_path = $path .'/'. $data['upload_data']['file_name'];
Also, you may just use:
$path = '...';
$data = $this->upload->data();
$upload_path = $path . '/' . $data['file_name'];
Make sure $data = $this->upload->data(); var_dump($data) contains something like this:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Finally I use this way to save the full path of uploaded image to database:
$path=dirname($_SERVER['SCRIPT_FILENAME'])."/upload/".$_FILES['pic']['name'];
now it works well.
Related
I am using CI version 3.1.2 to process an image and using Windows 8. My problem is every time i process an image and trying to create a thumbnail, i am countering a message "Unable to save the image. Please make sure the image and file directory are writable". This is occurred when trying to save the copy of thumbnail into different location. Here are my codes processing the image:
$this->original_path = realpath(APPPATH.'../assets/galery/full');
// Uploading image
$config = [
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->original_path,
'max_size' => 2048,
'maintain_ratio' => true,
'file_name' => 'img_'.strtotime(date('d-m-y-H-i-s')), // miss type
'overwrite' => true,
'remove_spaces' => true
];
$this->load->library('upload', $config);
$this->upload->do_upload('galery');
$file_name = $this->upload->file_name;
// Processing image, success processing image
$this->load->library('image_lib');
$img = [
'image_library' => 'gd2',
'source_image' => $this->original_path.'/'.$file_name,
'maintain_ratio' => true,
'width' => 600
];
$this->image_lib->initialize($img);
$this->image_lib->resize();
// This is fail, with message "Unable to save the image... using(var_dump())
$thumb = [
'source_image' => $this->original_path.'/'.$file_name,
'new_image' => '/assets/galery/small',
'maintain_ratio' => true,
'width' => 200
];
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
I've tried different config, like using realpath(APPPATH.'../assets/galery/small'), but still no luck. It is success, by the way, when create config using 'create_thumb'.
But my purpose is, saving that thumbnail in another folder. How can i achieve that?
UPDATE
This change seem to work well. I change realpath(APPPATH.'../assets/galery/full') to realpath(APPPATH.'../assets/galery') and make several change in:
...
'upload_path' => $this->original_path.'/full'
...
'source_image' => $this->original_path.'/full/'.$file_name
...
'new_image' => $this->original_path.'/small/thumb_'.$file_name
Until now i'm still trying to make my code simple and put it in MY_Model. Thanks for all comment(s). If some one may have better solution, it would be nice to share here.
I am using CodeIgniter Captcha Helper to generate captcha images for my website. It is working perfectly fine. But the only problem is that it generates a random name for the captcha image. How can I change the name of the image that is generated by CodeIgniter Captcha Helper? I have used the following code for generating my captcha.
$this->load->helper('captcha');
$keyword = $this->generate_random_keyword();
$vals = array(
'word' => $keyword,
'img_path' => './captcha/',
'img_url' => base_url() . '/captcha/',
'img_width' => '150',
'img_height' => '35',
'border' => 0,
'font_path' => './fonts/texb.ttf',
'expiration' => 3600
);
$captcha = create_captcha($vals);
CodeIgniter/system/helpers/captcha_helper.php line 231
$img_name = $now.'.jpg';
You can modify it directly (probably not a good idea as updates may overwrite). Or you could create your own helper by copying the original, modifying and putting it in application/helpers.
If you want to customize the name via parameter, something like this should do it:
// line 42
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '', $captcha_filename = '')
then
// line 231
if ($captcha_filename != '')
{
$img_name = $captcha_filename.'.jpg';
}
else
{
$img_name = $now.'.jpg';
}
Be aware, though, that this example may overwrite existing captcha's in the same directory if you set $captcha_filename.
So, copy captcha_helper.php into application/helpers, make the changes on line 42 and 231 (or just 231), save, and you should be good.
I'm running over this problem which I was trying for the last few hours
I'm having an image upload with some details to store in db.
I store the details and image path, working like a charm. Now comes the edit part.
I'm trying to check if the input file is empty, if so update just the details, else delete the image and reupload new image. The problem is this:
If the input file is empty it updates everything no problem, if is not empty it is updating the details, but the image is the same, doesn't get deleted or reuploaded.
here is the code
$image_input = $this->input->post('image');
if(isset($image_input) && !empty($image_input))
{
$this->db->where('id', $id);
$img = $this->db->get('menus_category', 1);
if($img->num_rows() > 0)
{
$row = $img->row();
$original_image = $row->image;
$desktop_image = $row->desk_img;
$mobile_image = $row->mob_img;
$thumb_image = $row->thumb;
$unlink_image = unlink('./uploads/menus/' . $original_image);
$unlink_desk = unlink('./uploads/menus/desk/' . $desktop_image);
$unlink_mob = unlink('./uploads/menus/mobile/' . $mobile_image);
$unlink_thumb = unlink('./uploads/menus/thumbs/' . $thumb_image);
if($unlink_desk && $unlink_image && $unlink_mob && $unlink_thumb)
{
$config = array(
'upload_path' => './uploads/menus',
'allowed_types' => 'gif|jpg|jpeg|png',
'max_size' => '15000'
);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$image_data = $this->upload->data();
$this->load->library('image_lib');
// thumb resize
$thumbnail = 'thumb_' . $image_data['file_name'];
$thumb = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'thumbs/' . $thumbnail,
'maintain_ratio' => TRUE,
'width' => '90',
'height' => '90'
);
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
$this->image_lib->clear();
// mobile resize
$mob = 'mob_' . $image_data['file_name'];
$thumb_mob = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'mobile/' . $mob,
'maintain_ratio' => FALSE,
'width' => '290',
'height' => '83'
);
$this->image_lib->initialize($thumb_mob);
$this->image_lib->resize();
$this->image_lib->clear();
// desktop resize
$desk = 'desk_' . $image_data['file_name'];
$thumb_desk = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'desk/' . $desk,
'maintain_ratio' => FALSE,
'width' => '700',
'height' => '200'
);
$this->image_lib->initialize($thumb_desk);
$this->image_lib->resize();
$this->image_lib->clear();
// insert path and details to database
$data = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['title']),
'description' => $input['description'],
'image' => $image_data['file_name'],
'desk_img' => $desk,
'mob_img' => $mob,
'thumb' => $thumbnail
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data);
}
else
{
echo $this->image_lib->display_errors();
}
}
}
}
else
{
$data2 = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['slug']),
'description' => $input['description']
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data2);
}
Note: the else statement works fine, but the first if the problem. Now I changed the if to just if(isset($image_input)) ... and if file input is not empty is reuploading the picture and updating the details fine, but if I update only the details with no picture, it is deleting the picture that is already uploaded and is not updating. (I think this is the problem but I can't figure out how to fix it).
If you will to give me some help or to put me on right direction I will be thankful.
Thanks guys
First, this would be a comment asking a question, but I don't have enough reputation do add one just yet...
If you could post your HTML form, that would be helpful, but without that...
1) I assume the form you are submitting has enctype="multipart/form-data" in the opening tag.
2) On line 2 of your code here, $this->input->post('image'), is 'image' from a form input of type file? If so, your statement won't work as 'image' is part of the $_FILES array and not $_POST anymore. Meaning when you go to check it, it is always going to be empty (or non-existent).
To verify that your form is submitting the way you think it is, do this just before the first line in the code you have in your post:
var_dump($_POST);
var_dump($_FILES);
exit; // don't let anything run after the dumps.
let me know if that puts you in the right direction or not.
Am trying to upload an image and save the images full path in my images table but i dont know how to get the full path it out of the $this->upload->data() array this is my code below.
//assigns the user's sessioned id to the variable $user_id
$user_id = $this->session->userdata('user_id');
//sets rules for the image that is being uploaded
$config['overwrite'] = FALSE; //does not overwrite any image adds +1 instead
$config['encrypt_name'] = FALSE; //encrypts the images name
$config['remove_spaces'] = TRUE; //removes spaces from the images name
$config['file_name'] = $user_id."_0.jpg";/*gives the image a new name combination of the user's id and a number*/
$config['upload_path'] = './uploads'; // the uploaded images path
$config['allowed_types'] = 'jpg|png';/*types of image extentions allowed to be uploaded*/
$config['max_size'] = '2048';// maximum file size that can be uploaded (2MB)
if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
$this->load->library('upload',$config); /* this loads codeigniters file upload library*/
//this checks for errors incase the image upload breaks a set rule.
if (! $this->upload->do_upload() ) {
echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
}
else {
// success message to show that the image was successfuly uploaded.
echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump($this->upload->data() );
}
All I needed to do was to store the array $this->upload->data(); in a variable for example
$image_info = $this->upload->data();
and from the $image_info variable I can access the images properties like so.
$image_info['full_path'];
$image_info['file_name'];
You should be seeing in the dump for $this->upload->data() the following array:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
So you can simply use something like:
if ($uploaded_image = $this->upload->do_upload() ){
// success message to show that the image was successfuly uploaded.
echo "THE IMAGE HAS BEEN UPLOADED : ";
$full_path = $uploaded_image['full_path'];
} else {
echo "UPLOAD ERROR ! ".$this->upload->display_errors();//image error
}
I have created a helper that requires some parameters and should upload a file, the function works for images however not for zip files. I searched on google and even added a MY_upload.php -> http://codeigniter.com/bug_tracker/bug/6780/
however I still have the problem so I used print_r to display the array of the uploaded files, the image is fine however the zip array is empty:
Array
(
[file_name] =>
[file_type] =>
[file_path] =>
[full_path] =>
[raw_name] =>
[orig_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
Array
(
[file_name] => 2385b959279b5e3cd451fee54273512c.png
[file_type] => image/png
[file_path] => I:/wamp/www/e-commerce/sources/images/
[full_path] => I:/wamp/www/e-commerce/sources/images/2385b959279b5e3cd451fee54273512c.png
[raw_name] => 2385b959279b5e3cd451fee54273512c
[orig_name] => 1269770869_Art_Artdesigner.lv_.png
[file_ext] => .png
[file_size] => 15.43
[is_image] => 1
[image_width] => 113
[image_height] => 128
[image_type] => png
[image_size_str] => width="113" height="128"
)
this is the function helper
function multiple_upload($name = 'userfile', $upload_dir = 'sources/images/', $allowed_types = 'gif|jpg|jpeg|jpe|png', $size)
{
$CI =& get_instance();
$config['upload_path'] = realpath($upload_dir);
$config['allowed_types'] = $allowed_types;
$config['max_size'] = $size;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$ffiles = $CI->upload->data();
echo "<pre>";
print_r($ffiles);
echo "</pre>";
$CI->upload->initialize($config);
$errors = FALSE;
if(!$CI->upload->do_upload($name))://I believe this is causing the problem but I'm new to codeigniter so no idea where to look for errors
$errors = TRUE;
else:
// Build a file array from all uploaded files
$files = $CI->upload->data();
endif;
// There was errors, we have to delete the uploaded files
if($errors):
#unlink($files['full_path']);
return false;
else:
return $files;
endif;
}//end of multiple_upload()
and this is the code in my controller
if(!$s_thumb = multiple_upload('small_thumb', 'sources/images/', 'gif|jpg|jpeg|jpe|png', 1024)): //http://www.matisse.net/bitcalc/
$data['feedback'] = '<div class="error">Could not upload the small thumbnail!</div>';
$error = TRUE;
endif;
if(!$main_file = multiple_upload('main_file', 'sources/items/', 'zip', 307200)):
$data['feedback'] = '<div class="error">Could not upload the main file!</div>';
$error = TRUE;
endif;
found the solution -> codeigniter.com/forums/viewthread/149027 Adding force-download to the allowed mime types
I got 'application/octet-stream' in $_FILES['your_file_name']['type'] , so I had to add 'application/octet-stream' in the mime types for 'zip' (in /config/mimes.php) .
The value of $_FILES['your_file_name']['type'] seems to differ based on browser/platform.