Not getting the correct path after uploading a file - php

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.

Related

Store image path in codeigniter

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;

how can I change allowed file type in codignetor which actually work?

Here is my controller code but only jpg and png file are uploaded I also changed it my process model.php also.
Is there any way to change it? And it doesn’t show any warning. You can check it from here but you need to register first...
public function saveTestimonial() {
$config['upload_path'] = './testimonial_photo/';
$config['allowed_types'] = 'gif|jpg|png|psd|zip|tif|ai|eps';
$config['max_size'] = '1024';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
You should declare file upload like the below code. Since you are uploading images, docx, zip files you should not declare max_width and max_height. But you can change the max_size for the file.
$config['upload_path'] = 'testimonial_photo/';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('gif|jpg|png|jpeg|zip|pdf|doc|docx');
$this->upload->do_upload('filename_in_html_input')

Codeigniter server root

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();

codeigniter file upload failure

Can't upload file. $this->upload->do_upload('anonsphoto'); returns false. Can't find out why.
I added upload library to autoload this way: $autoload['libraries'] = array('upload');
Here is my view:
<?php
echo form_open_multipart('InterviewController/saveInterview');
$anons_data = array(
'name' => 'anons',
'id' => 'anons_area',
'value' => 'Введите анонс'
);
echo form_textarea($anons_data); ?>
<p>Картинка анонса</p>
<?php
echo form_upload('anonsphoto'); ?>
Here is controller which uploads file:
public function saveInterview() {
$config['upload_path'] = '/application/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['file_name'] = 'img_' . (string) time();
$this->upload->initialize($config);
$this->upload->do_upload('anonsphoto');
return;
}
Uploading file is 3 kb sized png file. I can't understand why do_upload is returning false. Any ideas?
File uploading class says all what you need. https://www.codeigniter.com/userguide3/libraries/file_uploading.html
This is a key:
$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);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
"." (dot) means currently directory of upload path. So your app is not working because you are on root path not started with (dot).
Your folder should be with full permission (CHMOD) to 0755.
This:
$config['upload_path'] = './uploads/';
means: currently directory and to uploads directory
or:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/upload/';
means: your full document URI path and on directory '/application/upload/' as Can says.
Don't forget to put (dot) . between $_SERVER['DOCUMENT_ROOT'] and '/application/upload'.
Max size should be in KiB values: $config['max_size'] = '10000';
Hope this helps for understanding how it works.
I think your upload path is wrong. try using
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/application/upload/';
and make sure that the directory is writable.

Trying to upload photo in Codeigniter - not finding my upload folder - ideas why?

I'm trying to upload profile pics during a registration process with CI ($me !== 'experienced' )
Here is my method ... nothing strange
function do_upload()
{
$config['upload_path'] = base_url('/upload_pic/');
$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['message'] = $this->upload->display_errors() . $config['upload_path'];
$this->load->view('profile_photo_view', $data);
}
else
{
$data['message'] = $this->upload->data();
$this->load->view('upload_success', $data);
}
}
.. but it is failing. As you can see, I have the upload path passing to my fail page for display, and this is what it is showing..
The upload path does not appear to be valid.
http://localhost:8888/MY_SITE/upload_pic
That is where my upload folder is. And I have full 777 permissions on it..
.. any idea what could be wrong? And will trailing slashes always be stripped like that?
base_url will give you...wait for it...a URL :)
You need a path for uploads.
$config['upload_path'] = '/full/path/to/upload_pic/';
or you can use relative paths:
$config['upload_path'] = '../upload_pic';
but you have to give it a path, not a URL.
As #stormdrain pointed out, base_url() is there to get the URL, not a filesystem path. Try:
$config['upload_path'] = dirname(FCPATH) . '/upload_pic/';
Try this
$config['upload_path'] = base_url() . '/upload_pic/';
or inside index.php append this
define('UPLOADS', 'upload_pic');
then set config to
$config['upload_path'] = UPLOADS;

Categories