Reputation: 41
$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
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
Reputation: 588
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.
<?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;
}
}
Simply create application/libraries/MY_Upload.php
and paste the code above in it.
That's all!
This extension is compatible with CodeIgniter 3.x and 2.x versions.
Upvotes: 2
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
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
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
Reputation: 766
i think problem can be solve by using
$config['upload_path'] ='./photos/';
instead of
$config['upload_path'] = site_path().'photos/';
Upvotes: 0
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
Reputation:
Hi on server you can connect with filezilla and right click on folder and change File Attributes
Upvotes: 0
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
Reputation: 102735
There's only a few reasons for this error to occur:
site_path().'photos/'
does not exist, try running is_dir()
to ensure that it does.is_writable()
to make sure.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;
}
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