<?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
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.
Hello I have a write a code that upload image path into the database but the thing is it uploads the real path (C:/wamp/www/amref/images/student_profile/childbrah2.jpg) hence when I fetch the path form the database in order to display the image it gives me the error that the link is broken. I want to store only (images/student_profile/childbrah2.jpg).
The controller code:
$config['upload_path'] = '../images/student_profile/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = "2048000"; // Can be set to particular file size , here it is 2 MB(2048 Kb)
$config['max_height'] = "768";
$config['max_width'] ="1024";
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
echo '<h4 style="color: red;">
Failure! </h4>
<p style="color: red;">'.$this->upload->display_errors().'</p>';
} else {
$data_upload_files = $this->upload->data();
$image = base_url($data_upload_files['full_path']);
Yes. Because you inserting $config['upload_path'] = '../images/student_profile/', so path is C:/wamp/www/amref/images/student_profile/.
If you enter this as C:/wamp/www/amref/images/student_profile/ this will not work in your server. Because in your server there is no partition call C:.
So use your path as it is.
Edit
<?php
$old = 'C:/wamp/www/amref';
$fulpath = $old.'/images/student_profile/';
echo $fulpath;
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";
}
Hi I am trying to use the codigniter do_upload function below:
But get this error: "You did not select a file to upload." I do select a file and when i pass the name i can see the name but still can not upload...thanks in advance for the help.
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())
{
$data = array('error' => $this->upload->display_errors());
print_r($data);
exit;
//$this->load->view('clients_page', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
exit;
}
}
I assume you're not naming your <input type="file" /> the default userfile. It would be nice to see your upload view also.
change this
$this->upload->do_upload()
to this
$this->upload->do_upload('audioRef')
I do select a file and when i pass the name i can see the name but still can not upload
This is often because you've omitted to set your form to submit as multipart/form-data. Forms default to application/x-www-form-urlencoded, which can't contain file uploads; in this case you just get the filename instead.
<form method="post" action="..." enctype="multipart/form-data">
Incidentally,
$config['allowed_types'] = 'gif|jpg|png';
if you must use filename extension to check file type (and it's inadvisable, but that's what CodeIgniter offers, unfortunately), please also allow jpeg and any other likely extensions.
$config['upload_path'] = './uploads/';
If that uploads folder is within the webroot (you'll be serving files directly out of it), that's potentially a cross-site-scripting vulnerability. Unfortunately it is possible to upload files that look like images but in fact contain HTML, including <script>.
Doing user-uploaded-content in a safe way is hard. See eg this question for background.
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.