Part of the EllisLab Network
   
 
File upload library docs error
Posted: 10 November 2006 02:57 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  05-30-2006

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

Profile
 
 
Posted: 10 November 2006 03:16 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  481
Joined  03-08-2006

If you load the library first, you must then use the initialize(); function to set the configuration - not load the library again:

[quote author=“Documentation”]

// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);

It is in the documentation wink

 Signature 

Flickr | Last.fm | Del.icio.us

Profile
 
 
Posted: 11 November 2006 04:54 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  88
Joined  03-12-2006

This User Guide typo has been reported already - http://codeigniter.com/forums/viewthread/46684/

 Signature 


Jan Onesork
Czech SEO consultant

Profile
 
 
Posted: 11 November 2006 12:18 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  9
Joined  05-30-2006

Craig: Whoops, there it is. I got so wrapped up in my debugging that I didn’t notice that point further down the page. Thanks for pointing it out.

Jan: Ah, good. Unfortunately I didn’t hit that page when I was searching the forums while trying to figure out the error.

I’d still like to see the expanded error messages added to the library. Any library that gives the same general error message for multiple conditions has the potential for confusion.

-Mythago

Profile
 
 
Posted: 21 November 2006 12:22 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  6
Joined  10-05-2006

Hmmm… I’ve followed the tutorial, made the corrections and I still get:
“An empty upload path is not valid.”

Somewhere either the file_path is not getting set or is getting blown away…

Profile
 
 
Posted: 21 November 2006 12:30 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  9
Joined  05-30-2006

Can you post the relevant parts of your upload code for us to look at?

Profile
 
 
Posted: 21 November 2006 01:14 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  6
Joined  10-05-2006

I will update to 1.5 (I have 1.4.1) and see if it gets fixed….

Profile
 
 
Posted: 21 November 2006 11:13 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  6
Joined  10-05-2006

I’ve updated to 1.5 and the problem has disappeared…

Thanks!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66427 Total Logged-in Users: 38
Total Topics: 84790 Total Anonymous Users: 3
Total Replies: 455008 Total Guests: 239
Total Posts: 539798    
Members ( View Memberlist )
Newest Members:  llogocsaturkeyPeter BryanttherendStudioGeorgiaJZeerfedegheEdgedcenticeRoger_Mx