CodeIgniter Forums
Uploading File to Database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Uploading File to Database (/showthread.php?tid=40475)



Uploading File to Database - El Forum - 04-09-2011

[eluser]anthrotech[/eluser]
Hello All,

I am having problems properly uploading a file to a table in a MySQL database. Similar code outside of CI works fine.

Here is my code for uploading the file in CI.

members.php - controller file

Code:
// Validate Form input and check Image Parameters
                        $fileName  = $_FILES['file']['name'];
                        $tmpName   = $_FILES['file']['tmp_name'];
                        $fileSize  = $_FILES['file']['size'];
                        $fileType  = $_FILES['file']['type'];
                        $fileError = $_FILES['file']['error'];

                           if (strlen($fileName) == 0) {
                               $data['message'] = "Please select a file to upload.";
                               $content = "";
                           }
                           else {
                            /* Process File */
                            $fp        = fopen($tmpName, 'r');
                            $content   = fread($fp, filesize($tmpName));
                            $content   = addslashes($content);
                               fclose($fp);                              
                            if(!get_magic_quotes_gpc()) {
                                $fileName = addslashes($fileName);
                            }
                           }

                        if (strlen($this->input->post('Caption')) > 0) {
                           $fileCaption = $this->input->post('Caption');
                        }
                        else {
                            $fileCaption = "Not Provided";
                        }
            
                        if ($fileError > 0) {
                             $data['message'] .= "<br/>Please select a File before Uploading.";
                        }
                        elseif ($fileSize == 0) {
                             $data['message'] .= "<br />File size cannot be zero bytes.";
                        }
                        elseif ($fileSize > $this->lang->line('site_max_file_size')) {
                             $data['message'] .= "<br />File cannot exceed ".$this->lang->line('site_max_file_size')." bytes";
                        }
                        elseif (!in_array(end(explode(".",strtolower($fileName))),$extension_approved)) {
                            $data['message'] .= "<br />Must upload either a .jpg or .png file.";
                        }
                        else {
                              $data['message'] = "";                
                        }
                        
                        $form_data = array(
                            'File'         => $content,
                            'Caption'    => (string) $fileCaption,
                            'Size'        => (int) $fileSize,
                            'Name'        => (string) $fileName,
                            'Type'        => (string) $this->input->post('Type'),
                            'Ext'        => (string) $fileType,
                            'UserID'    => (int) $this->session->userdata('userid'),
                            'FileDateAdded'  => date('Y-m-d H:i:s'),
                            'FileDateEdited' => date('Y-m-d H:i:s')            
                        
);                        

                        if ($data['message'] == "") {
                            // Delete Photo if Replacing a Photo
                            if ($this->input->post('Type') == "photo") {
                                $this->member_model->delete_files($this->session->userdata('userid'));
                            }            
                            if ($this->member_model->add_data($this->session->userdata('userid'),$form_data,'PatientFiles') === true) {
                                // Delete Temporary Upload File
                                unlink($tmpName);
                                  // Redirect back to the main Demographics Form
                                  $this->output->set_header('refresh:1; url="/members/display/action/photo"');                                  
                              }    
                              else {
                                   // Error Page
                                   $this->load->view('utils/fileuploader', $data);      
                              }
                        }
                        else {
                             // Error Page
                             $this->load->view('utils/fileuploader', $data);    
                        }

Data is uploaded to the Database, but photo does not show up.

I have also tried changing $content to (string) $content AND "$content" AND '$content'.

I note that are differences in the File data uploaded between CI and regular PHP5.

CI - File Data Column
�PNG\0\0\0IHDR\0\0\0�\0\0\0�\0\0\0f&\0\0yiCCPICC Profile\0\0x��gT���{r\"�

Custom PHP5 - File Data Column
�PNGIHDR


Uploading File to Database - El Forum - 04-17-2011

[eluser]anthrotech[/eluser]
No one have ideas? Seems like a common problem. Do I need to provide more details?


Uploading File to Database - El Forum - 04-17-2011

[eluser]Wondering Coder[/eluser]
why don't you try the CI class upload. That's what I used in my apps.


Uploading File to Database - El Forum - 04-18-2011

[eluser]Atharva[/eluser]
Why you want to upload file to database? You can simply upload it on file system and just store the Name of the file in any db column.


Uploading File to Database - El Forum - 04-18-2011

[eluser]danmontgomery[/eluser]
Need more information. Can you post the code where you're saving the file, and where you're displaying the file?

If you're using AR, it will automatically escape queries for you... Since you're escaping, it might be happening twice. That'd be the first place I'd look.