Part of the EllisLab Network
   
 
Accessing $_FILES without having to upload a file
Posted: 09 June 2008 09:54 AM   [ Ignore ]  
Summer Student
Total Posts:  8
Joined  06-09-2008

Hi all.

I’m a real n00b when it comes to CodeIgniter, as I’ve just started my first project in it a few days ago.

There is an issue that I can’t seem resolve when it comes to uploading files, and reading the data of the uploaded file.

For this specific project, I’m storing my images as a LONGBLOB in a database. Because of this, I don’t really need to store the file in a permanent location on the server when uploading it. All I need is access to the uploaded file’s details, and I’ll then insert the raw data into the LONGBLOB field.

Now my real question is, is there functionality native to CI that will allow for this, or am I going to have to write my own class to handle these types of uploads?

Any help whatsoever would really be appreciated.

Thanks,
G.

Profile
 
 
Posted: 09 June 2008 05:55 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  869
Joined  09-25-2007

I do a similar thing to upload into a MEDIUMBLOB, but couldn’t get around it without uploading a file.. instead I delete (unlink) the file, after I gathered the content.

Step 1: I use the upload library to upload the file (read the documentation)
Step 2: after the call to do_upload() I read in the file, get the content, delete the file and then insert it into the database.

//read upload library documentation
....
....
     
$config['allowed_types'] = 'gif|jpg|png|mpg|wmv|mp3|wav';
     
$config['max_size']    = '16777215';
     
$config['max_width']  = '3000';
     
$config['max_height']  = '3000';

     
$this->load->library('upload', $config);

     if ( !
$this->upload->do_upload())
     {
         $error
= $this->upload->display_errors();
         
$ret = array();
         
$ret['ErrorType'] = 2;
         
$ret['ErrorMessage'] = $error;
         return
$ret;
     
}    
     
else
     
{
         $data
= array('upload_data' => $this->upload->data());
         
$tmpName  = $data['upload_data']['full_path'];
         
$fileSize = $data['upload_data']['file_size'];

         
$fp      = fopen($tmpName, 'r');
         
$content = fread($fp, filesize($tmpName));
         
$content = addslashes($content);
         
fclose($fp);
         
// delete file
         
unlink($tmpName);
         
// now just insert $content into the database LONGBLOB. (remove slashes when reading in)
.....
.....

you have to ensure you database can handle longblobs as I had to change the mysql config to cope with this.

Profile
 
 
Posted: 10 June 2008 05:01 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  8
Joined  06-09-2008

Awesomeness! Thanks for the suggestion. I was playing around with solutions yesterday, and I came across a very similar idea.

The help is appreciated smile

Profile
 
 
Posted: 22 July 2008 05:33 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  20
Joined  04-03-2008

thanks very helpful

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: 77571 Total Logged-in Users: 20
Total Topics: 101562 Total Anonymous Users: 2
Total Replies: 544409 Total Guests: 181
Total Posts: 645971    
Members ( View Memberlist )
Newest Members:  Idril616tonybernardcarterstarksColeJLinskitnealsemperjrawhallshiusbozzlynobluff