I followed this tutorial on how to upload a photo in a codeigniter folder:
http://codeigniter.com/user_guide/libraries/file_uploading.html
Can anybody tell me how can I store the path of the photo I just uploaded in my db?
Let's say have table called photo_paths
thanks in advance
This assumes you have an Images table.
Configure the file upload as follows:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
Then on successful upload insert the file information into the database.
$insert_data = array(
'id_fk' => $this->input->post('page_id'),
'imgfilename' => $upload_info['file_name'],
'imgfilepath' => $upload_info['file_path']
);
$this->db->insert('images', $insert_data);
$upload info is retrived from the file_uploader class on successful upload using the following:
$upload_info = $this->upload->data();
If you want to see exactly what is returned:
echo var_dump($upload_info);
For me, since most of my uploaded images are about the same thing (for example, product images) I might just store the filename (e.g. productshot1.jpg) in the database, since I know where it is anyway (for example, I might keep all product images in "/images/products/"). That way if you need to change something later, it's not so difficult.
Related
I am uploading a file on server and storing the its path in database in codeigniter, however i am after the upload i am not getting the correct path
The code that i have used is
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array('upload_data' => $this->upload->data());
$image_path = $data['upload_data']['full_path'];
I wish to save the path that i am getting in $image_path inside database.
Now the path that i get is something like this
/home/litehepn/public_html/project_name/uploads/xyz.docx
But the path that i want is
project_name/uploads/xyz.docx
Can anyone please tell how to get the correct path
This is the correct path. Because you have stored your files inside that home directory. But You can use chop($url,"/home/litehepn/public_html/"); for getting it.
In my form have 3 file upload input option.
bot are going to different directory. I can work with one directory but not work with multiple.
My code
$config['upload_path'] = './uploads/video';
$config['allowed_types'] = 'flv|mov|m4v|mp4';
$config['max_size'] = '30720';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$video_upload=$this->upload->data();
$config2['upload_path'] = './uploads/video';
$config2['allowed_types'] = 'jpg|jpeg|bmp|png';
$config2['max_size'] = '30720';
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
$this->upload->do_upload('thumbnail1');
$thumbnail_upload=$this->upload->data();
here video file upload successfully but image file not upload
$this->load->library() doesn't reload or reinitialize the library if it's already loaded.
In this case, you need to modify the existing loaded library options:
$this->upload->initialize($config2);
instead of
$this->load->library('upload', $config2);
Should do the trick.
<?php if($row->resumes != null){
$attachment= base_url().'uploads/resumes/'.$row->resumes;
echo '<a target="_blank" href="'.$attachment.'">
Show Resume</a>';
}
?>
Suppose, my file name is, my plan.pdf
so, on click of Show Resume then in url it's open like,
../uploads/resumes/my%20plan.pdf
in my resumes folder is store like my plan.pdf
there for 404 Page Not Found error is coming
i tried to using urlencode & urldecode, but it's not working.
I am not sure, but it might be the problem of white-space.
You may use trim() while inserting and retrieving time. or read PHP Manual
It won't work because of the space in the filename. I suggest that you use the Codeigniter upload class if you aren't already and remove any spaces from the file name before uploading.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = preg_replace('/\s+/', '', $filename); //Remove all whitespace
$this->load->library('upload', $config);
$this->upload->initialize($config);
Once the file has been uploaded you can get the name of the file by doing
$aUploadData = $this->upload->data();
$aUploadData['file_name'];
Then once you have that value you should insert it into your database because that way you will know that the value stored in the database will match the name of the file uploaded
Also as an extra check you should check to see if the file exists before linking to it
<?php if($row->resumes != null) {
if(file_exists(base_url().'uploads/resumes/'.$row->resumes)) {
$attachment= base_url().'uploads/resumes/'.$row->resumes;
echo '<a target="_blank" href="'.$attachment.'">Show Resume</a>';
}
}
?>
Hope that helps
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";
}
I am trying to upload video in server using Codeigniter, In the localhost i can do it, but when in the server it does not work. Even though do_upload() function returns true.
When i'm testing the server root, print_r($_SERVER['DOCUMENT_ROOT']); it returns
/var/www/mjpp/data/www/supps.mydomainname.com
Also the
$updata = $this->upload->data(); returns
[file_path] => /var/www/mjpp/data/www/supps.mydomainname.com/public/video
Can you tell me why /var/www/mjpp/data/ is appearing before my main domain name , and is it the cause of any error for uploading video?
Thank you in advance, sorry for my bad english.
/var/www/mjpp/data/www/supps.mydomainname.com is absolute location of your document root in a system in general. However you do not usually have access to anything above your user dir.
public/video or /var/www/mjpp/data/www/supps.mydomainname.com/public/video (as absolute path) is what you pass to Upload library as upload_path. This is where the library will try yo upload the files. This folder should exist and be writeable (have 0777 permissions on it for example).
No need to use Document Root, Codeigniter itself having configurations when file uploading.
Here is sample code for image uploading,
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
You just need to change the configuration($config array), for your issue you should use the $config['upload_path'].
Follow codigniter file upload tutorial
Dont use $_SERVER['DOCUMENT_ROOT'] in CI, use URL Helper
$this->load->helper('url');
echo base_url();