I just spent an hour trying to figure out why my file uploads weren’t working. I tried many different things and checked the content of many different variables. I was using the example code from the docs to test out the library and get a feel for it.
The error message I was getting was:
“The upload path does not appear to be valid.”
I was specifying a path in my config. I was passing my config to library loader. The path existed. The path was writable.
It turns out that the problem is due to the documentation code used to demonstrate the uploading library. I suppose I should have caught the offending line sooner but overlooked it.
The original code:
function do_upload()
{
$this->load->library('upload'); # Bad! Don't do it!
$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);
# (other code removed)
See that first $this->load->library line? It should not be there. That single line is what caused my problem. It was causing the upload_path to be empty even though the second load line passed the config. I assume the library loader didn’t reload the library on the second call which means the config didn’t get passed.
The error message I was getting was correct but not as helpful as it could have been. A suggested update to the library is to note the empty path and provide an appropriate error message. I have also added another error message to make the error clearer when a specified path is not a directory.
In system/language/english/upload_lang.php:
$lang['upload_filepath_empty'] = "An empty upload path is not valid.";
$lang['upload_path_not_folder'] = "The upload path must point to a folder.";
In system/libraries/upload.php:
function validate_upload_path()
{
if ($this->upload_path == '')
{
$this->set_error('upload_filepath_empty'); # Update this line
return FALSE;
}
if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
{
$this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
}
if ( ! @is_dir($this->upload_path))
{
$this->set_error('upload_path_not_folder'); # Update this line
return FALSE;
}
# (other code removed)
-Mythago
