nidhin
nidhin

Reputation: 41

Uploading an image in Codeigniter shows error The upload path does not appear to be valid

$config['upload_path'] = site_path().'photos/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('upload', $config);    
if ( ! $this->upload->do_upload())
{ 
    $this->data['alert'] = $this->upload->display_errors();                     
    $this->load->view('profile/photo', $this->data);
}   
else
{
    $upload_data = $this->upload->data();

    $filename = $upload_data['file_name'];
    $width = $upload_data['image_width'];
    $height = $upload_data['image_height'];
    $config1 = array();
    $this->load->library('image_lib');
    $config1['source_image'] = site_path().'photos/'.$filename;


    $this->remove_existing_file($this->session->userdata('user_id'));
    $this->Profile_model->savephoto('Edit', $filename );
    redirect('/profile/photo');
}

I am getting this error:

The upload path does not appear to be valid.

Upvotes: 3

Views: 36456

Answers (10)

Samuel Anand
Samuel Anand

Reputation: 11

Try out this code in your Controller

$config['upload_path'] = 'images'; //name of the uploading folder
$config['allowed_types'] = 'jpeg|png|jpg'; 
$config['file_name'] = 'name_for_file'; 

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

if (!$this->upload->do_upload())
 {
  echo $this->upload->display_errors();
  exit;
 }
else
 {
  $upload_data = $this->upload->data();
 } 

Upvotes: -1

Ema4rl
Ema4rl

Reputation: 588

This should help

If you need to automatically create directories like: ./assets/2016-07-27/ when uploading files then you'll have to extend the Upload library to automatically create directories if they don't exist.

The code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * File Uploading Class Extension
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Uploads
 * @author      Harrison Emmanuel (Eharry.me)
 * @link        https://www.eharry.me/blog/post/my-codeigniter-upload-extension/
 */
class MY_Upload extends CI_Upload {

    /**
     * Validate Upload Path
     *
     * Verifies that it is a valid upload path with proper permissions.
     *
     * @return  bool
     */
    public function validate_upload_path()
    {
        if ($this->upload_path === '')
        {
            $this->set_error('upload_no_filepath', 'error');
            return FALSE;
        }

        if (realpath($this->upload_path) !== FALSE)
        {
            $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
        }

        if ( ! is_dir($this->upload_path))
        {
            // EDIT: make directory and try again
            if ( ! mkdir ($this->upload_path, 0777, TRUE))
            {
                $this->set_error('upload_no_filepath', 'error');
                return FALSE;
            }
        }

        if ( ! is_really_writable($this->upload_path))
        {
            // EDIT: change directory mode
            if ( ! chmod($this->upload_path, 0777))
            {
                $this->set_error('upload_not_writable', 'error');
                return FALSE;
            }
        }

        $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/',  $this->upload_path);
        return TRUE;
    }
}


How to use:

Simply create application/libraries/MY_Upload.php and paste the code above in it. That's all!


More info:


NOTE:

This extension is compatible with CodeIgniter 3.x and 2.x versions.

Upvotes: 2

Nasir Uddeen
Nasir Uddeen

Reputation: 399

$config['upload_path'] = './photos/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';                     

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

write this code in your controller block

this problem is common i know your wrote this code in __construct() block but right method is use this code in particular Controller where upload code are call.

Upvotes: 3

user4985526
user4985526

Reputation:

I'm using wamp on windows

$config['upload_path'] = base_url().'./resources/uploads/';

not working at all.

var_dump(is_dir($config['upload_path'])); //return false
var_dump(is_writable($config['upload_path']));  //return false

but when I change it to

$config['upload_path'] = './resources/uploads/';   //works fine

it works fine. So I guess on windows it's not a permission problem.it's caused by the base_url() method .how strange is that.

Upvotes: 0

itsols
itsols

Reputation: 5582

I know this has already been answered. Even I had difficulty trying to upload files. But in my case the issue was that I had the CodeIgniter application in a folder outside of /var/www and the index page in the www folder (mainly as a security measure).

The uploads folder should be in the /var/www folder that contains the index.php file. This is what I had to do. The next thing is to give 777 permissions to the uploads folder.

Upvotes: 0

Prabin Tp
Prabin Tp

Reputation: 766

i think problem can be solve by using

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

instead of

$config['upload_path'] = site_path().'photos/';

Upvotes: 0

Adrian P.
Adrian P.

Reputation: 5228

Check your .htaccess file not to block application folder Check is uploads is CHMOD 777 Use var_dump(is_dir('/photos/')); to see is your directory exist! Finally try this:

    $config['upload_path'] = 'photos/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '1920';
    $config['max_height'] = '1280';                     

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

Upvotes: 0

user1055293
user1055293

Reputation:

Hi on server you can connect with filezilla and right click on folder and change File Attributes

Upvotes: 0

AlunR
AlunR

Reputation: 465

If you are on windows try writing to "c:\" and see if that works.

Also what is the output of site_path() eg echo site_path(); ?

On xammp I've found I need to write c:\xammp\htdocs\myproject\photos\ instead of just using '\photos\';

Upvotes: 0

No Results Found
No Results Found

Reputation: 102735

There's only a few reasons for this error to occur:

  1. The directory site_path().'photos/' does not exist, try running is_dir() to ensure that it does.
  2. The directory exists but is not writable. Make sure you have set the appropriate permissions on the directory. Try running is_writable() to make sure.
  3. The directory you want to use exists, but you have not represented it properly to the Upload library. Try using an absolute path with a trailing forward slash, similar to the example in the User Guide.

Beyond that, there is no explanation I can think of. Here is the CI code that validates the path (part of the Upload class):

public function validate_upload_path()
{
    if ($this->upload_path == '')
    {
        $this->set_error('upload_no_filepath');
        return FALSE;
    }

    if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
    {
        $this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
    }

    // This is most likely the trigger for your error
    if ( ! @is_dir($this->upload_path))
    {
        $this->set_error('upload_no_filepath');
        return FALSE;
    }

    if ( ! is_really_writable($this->upload_path))
    {
        $this->set_error('upload_not_writable');
        return FALSE;
    }

    $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/",  $this->upload_path);
    return TRUE;
}

Update:

Per your comments, try this instead and let's see what happens before moving to the next step of debugging:

$config['upload_path'] = './community/photos/';

Upvotes: 8

Related Questions