Page 1 of 1

File Upload Situation...

Posted: Sun Aug 09, 2009 11:34 am
by forestrain
Hello everyone.

I am having a problem with a file upload. The uploading of the file works great. I want my user to either select a file (that is already stored in the system) from a dropdown list OR upload a file if they don't see their file in the list. The problem that I am having is:

1) If the file is selected from the dropdown list, the value of the selection is not entering into the DB.
2) If no file is uploaded then it echos an error stating that "No file was uploaded". I don't want the file upload to be required.

Here is my code:

Code: Select all

 
 
if (!isset($_FILES['upload'])) {
    
    $i = mysqli_real_escape_string($dbc, $trimmed['logo']);
    
}else{
        
    $allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/jpg', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png', 'image/gif', 'image/GIF');
        
    if (in_array($_FILES['upload'] ['type'], $allowed)) { // Move the file over
            
        if (move_uploaded_file($_FILES['upload']['tmp_name'],"../uploadedimages/{$_FILES['upload']['name']}")){
                
            $i = $_FILES['upload']['name'];
                
        }   // End move if
        
    }else{  
        echo '<p class="error">Please upload a JPG, PNG, or GIF file type</p>';
    }
}
    
// Error Checks
        
if ($_FILES['upload']['error'] > 0 ){
            
    echo '<p class="error"> The file could not be uploaded becuase: ';
 
    switch($_FILES['upload']['error']){
            
        case 1:
            print 'The file exceeds the upload_max_filesize setting in php.ini.';       
        break;
                
        case 2:
            print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';       
        break;
                    
        case 3:
            print 'The file was only partially uploaded.';      
        break;
                    
        case 4:
            print 'No file was uploaded.';  
        break;
                    
        case 6:
            print 'No temporary folder was available.';     
        break;
                
        case 7:
            print 'Unable to write the disk.';              
        break;
        
        case 8:
            print 'File upload stopped.';       
        break;
                
        default:
            print 'A system error has occurred.';   
        break;
    }// END switch
    echo '</p>';
}
            
if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){      
    unlink($_FILES['upload']['tmp_name']);
}
 
//DB insertion code follows this...
 
 
As you can see, my logic is that if a file is not uploaded then the variable $i will be the posted value (file name) of the selection made in the dropdown menu called "logo", else when a file is uploaded then the variable $i becomes the file name of the uploaded file. So, that when I do the call to the DB it will insert the value of $i to the correct field in the DB.

If anyone can see where I am going wrong with this, your help will be greatly appreciated!!! :D

Shannon

Re: File Upload Situation...

Posted: Sun Aug 09, 2009 1:24 pm
by cpetercarter
You have not shown us the bit of code which tries but fails to update the database with the file selected from the drop down list, so it is difficult to advise you on the first point.

On the second, I think you have the logic a bit mixed. I think it should look like this:

Code: Select all

 
if (no uploaded file)  {
 
    $i = logo;
 
}  else  {
 
    if (error > 0 ) {
 
        switch error message
 
    } else {
    
        if (allowed type) {
 
            if (move uploadedfile)  {
 
            $i = filename;
    
            }
 
        } else {
 
        message - please upload jpeg etc
 
        }
 
    }
 
}
 
if (necessary) {
 
    unlink temp file:
}
 

Re: File Upload Situation...

Posted: Sun Aug 09, 2009 6:25 pm
by forestrain
I rearranged the code but it is still printing the error message that a file was not uploaded (which is case 4 in the error string). The file upload is not required. If no one uploads a file it should not print an error.

I can figure out the database error once this error is solved.

Re: File Upload Situation...

Posted: Sun Aug 09, 2009 6:30 pm
by Christopher
Have you checked for common problems like file permissions or php.ini settings that would prevent uploads?

Re: File Upload Situation...

Posted: Sun Aug 09, 2009 7:45 pm
by forestrain
Yes if I actually upload a file everything works fine. Its just making it a required action which should not be the case. In other words it won't process the form unless you upload a file.

Re: File Upload Situation...

Posted: Sun Aug 09, 2009 9:16 pm
by Christopher
Looking at your code, (and since it in now OO) I think I would make an explicit $error variable (or $success you pick the logic direction). Set it at the top and then set it as errors occur. Then you can clearly see what the logic is.

Re: File Upload Situation...

Posted: Mon Aug 10, 2009 3:18 am
by frao_0
And no problems here either?