Upload path on Codeigniter returns "the upload path doesn't seem to be valid"
Asked Answered
G

9

5

Now i've tried most of the fixes that i've read, most of them mention about APPPATH, base_url(), real path and etc. but i really don't know why all of them didn't work, what worked for me is that i've used the actual path, not a url but the one with the C:\xampp\htdocs.. blah blah blah.. now i've read one thread that url and directory aren't the same thing.. and the upload_path accepts only directory path and i mean the actual location of the uploads folder on the server not the URL.. now my question is how come APPPATH don't work. as what i know it the actual directory path. but when i tried to display is it return only "../applicaiton/" what really is the best path to be used on the $config['upload_path'] on the upload.php specially when deploying it to an actual server it is really a nuisance finding the directory path of your upload folder, NOTE im not using the initialize() method i'm putting my configs on config/upload.php

EDITS:

i have this on a separate file... upload.php

<?php 
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '1600';
$config['max_height'] = '1200';

and this is my controller

     if($this->upload->do_upload('image'))
     {
         //Did something here
     }
     else
     {
        //Did something for errors like display_errors()
     }

and end result is it displays "the upload path doesn't seem to be valid" and i've tried these line of code also

Trial 1:

$config['upload_path'] ='./uploads/';

Trial 2:

$config['upload_path'] ='uploads/';

Trial 3:

$config['upload_path'] ='/admin/assets/uploads/';

Trial 4:

$config['upload_path'] ='./admin/assets/uploads/';

Trial 5:

$config['upload_path'] ='admin/assets/uploads/';

the only thing that works is this

 $config['upload_path'] ='C:\xampp\htdocs\practice\admin.abcgencon\admin\assets\uploads'';

and using the last part as a path is kinda messy so i've tried also APPPATHbut it doesn't work also it also display "../application"..

as @cryptic said i've posted this code snippet.

Gibbosity answered 24/2, 2013 at 9:21 Comment(1)
Your question is not so clear, so i'm pasting how file uploading is done in CI in the answer belowIsaiah
S
19

Question, you tried realpath and APPPATH seperately?

In Codeigniter APPPATH points to the application folder.

Example: (place your folder outside the application folder, just saying if you did not do that way) let's say the folder where we want to place the files called images.

So what you need to do is to combine realpath() and APPPATH

$image_path = realpath(APPPATH . '../images');

and pass it to your config

$config['upload_path'] = $image_path;
Sagacious answered 24/2, 2013 at 11:47 Comment(5)
nope here is my folder tree application, system, admin under admin is assets under assets is the uploadsGibbosity
btw is realpath native to php or to CI ?Gibbosity
realpath is native to php, than point it to the folder tree what you told trying what i posted :)Sagacious
ohhh this helps me alot man.. more cleaner than copying the actual path :D though can u explain to me why did you combined both?Gibbosity
because of the dots, its not really ideal so realpath cleans it up and reaturns the real full path of that folderSagacious
T
1

Create your file upload directory say uploads outside of application directory or at the root directory of CI, and set the upload path as follows:-

$config['upload_path'] = realpath(FCPATH.'uploads');

FCPATH: Path to the front controller where index.php exists (root of CI)

The above code runs on both server and local.

Tittletattle answered 12/4, 2017 at 10:29 Comment(1)
Thank you. This Works :)Domiciliate
I
0

This is how file uploading is done in CI

$cat_image_name = $_FILES["cat_image"]["name"] ; 

//file uploading params
$config['upload_path'] = './uploaded_files/categories';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $image_id."_ipad";
$config['remove_spaces'] = TRUE;

//Loading Library - File Uploading
$this->load->library('upload', $config);

//Upload the image(Ipad)
if (!empty($cat_image_name)) 
{
  $this->upload->do_upload('cat_image');
  $data = array('upload_data' => $this->upload->data());
  $category_image_ipad = $data['upload_data']['file_name'];
  $img_extension = $data['upload_data']['file_ext'];
}
Isaiah answered 24/2, 2013 at 11:23 Comment(3)
does defining $config on a separte file like upload.php and putting it in config/ and defining it on a controller the same?Gibbosity
You don't have to have it in another file. Why don't you define the $config['upload_path'] in the controller?Isaiah
Files you uploaded should be uploaded outside the application folderIsaiah
C
0

try this

$config['upload_path'] = '../uploads/;

It's working very well for me

Cauliflower answered 19/10, 2016 at 6:52 Comment(0)
N
0

use it:

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
Northeastwards answered 24/7, 2017 at 8:24 Comment(0)
V
0

file upload requires a multipart form. For this, you must have included a form helper.

goto config folder, click autoload and find the $autoload['helper'] = array(); and put 'form' :

$autoload['helper'] = array('form');

for controller:

$config = array(

    'upload_path' => "./assets/",
    'allowed_types' => "gif|jpg|png|jpeg",
    'overwrite' => TRUE,
    'max_size' => "2048000",
    'max_height' => "768",
    'max_width' => "1024"
    );

    $this->load->library('upload', $config);

    if(!$this->upload->do_upload()){
        $errors = array('error' => $this->upload->display_errors());
        $post_image = 'noimage.jpg';
    } else {
        $data = array('upload_data' => $this->upload->data());
        $post_image = $_FILES['userfile']['name'];
    }

    $this->user_model->create_photo($post_image);
    redirect('');
}

for model:

public function create_photo($post_image){

    $data = array(
        'pri' => $this->input->post('price'),
        'descrip' => $this->input->post('title'),
        'post_image' => $post_image
    );

    return $this->db->insert('table_name', $data);
}
Virg answered 17/5, 2018 at 7:49 Comment(0)
B
0

Tested on Windows & Linux:

$path = realpath(".");
$path .= "/uploads/profile_images";

Here uploads is the uploads directory on root.

Bemoan answered 30/5, 2018 at 15:19 Comment(0)
H
0

FCPATH: front controller path where index.php exists or root folder

APPPATH: application folder

Create a uploads folder under root folder:

$config['upload_path'] = realpath(FCPATH.'uploads');

Create a uploads folder under application folder:

$config['upload_path'] = realpath(APPPATH.'uploads');

In case: If you have created the uploads folder outside of root folder:

$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads'); 
Hankow answered 28/6, 2022 at 8:21 Comment(0)
V
-1

It is very simple:

Just copy your all $config in new php file called upload.php and put it in cofig folder.

You path will work ok.

Here is upload.php file contents:

<?php

    $config['upload_path'] = site_url().'uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
    $config['max_size'] = 0;
    $config['max_width']  = 0;
    $config['max_height']  = 0;
    $config['file_ext_tolower']  = TRUE;
    $config['overwrite']  = FALSE;
    $config['max_filename']  = 0;
    $config['encrypt_name']  = TRUE;
    $config['remove_spaces']  = TRUE;
    $config['detect_mime']  = TRUE;
    $config['mod_mime_fix']  = TRUE;
?>

But please keep $config data in upload_file controller as well.For example:

public function do_upload(){

    $config['upload_path'] = 'uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
    $config['max_size'] = 0;
    $config['max_width']  = 0;
    $config['max_height']  = 0;
    $config['file_ext_tolower']  = TRUE;
    $config['overwrite']  = FALSE;
    $config['max_filename']  = 0;
    $config['encrypt_name']  = TRUE;
    $config['remove_spaces']  = TRUE;
    $config['detect_mime']  = TRUE;
    $config['mod_mime_fix']  = TRUE;

//Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:

   $this->upload->initialize($config);


            if (!$this->upload->do_upload('userfile'))
    {
        $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);
    }
    }

It is simple...

Vulgarity answered 29/12, 2015 at 6:20 Comment(1)
Why would someone want to duplicate code in two places?Statuette

© 2022 - 2024 — McMap. All rights reserved.