Reputation: 257
I am trying to create the functionality to upload a file in PHP and store it in my mysql database as a BLOB.
I have run into a few issues that I need some help on. The first issue is how can I do this without using forms? I am currently using the codeigniter framework and I do not want have the form do anything on the action, I would prefer just using javascript/ajax/jquery to grab my values and perform the file conversion to bytes and then insert into the blob,
The major problem I am having is in FF3 I can not get the path name to a file on the input. I have found out this is a known problem and I cant seem to find any way around this.
Lastly I am unclear on this but do I have to upload the file to a temp dir before I put it into my db? I would like to just encode the file and insert it but I am getting confused as to what the proper steps are.
Any info or a good tutorial that you have would be great. Thanks
Upvotes: 1
Views: 1628
Reputation: 62894
I suggest you rethink your entire approach.
There is a class of databases that are highly optimized for storing file data. These are called "filesystems". Unless you have a very clear, articulable, reason for wanting to store files as BLOBs in an RDBMS, you should take a step back and think about what you're doing.
As for your form issues, I'm not sure what you want to do. It sounds like you just want an ajax-y file upload. That is doable. However, the underlying mechanism is going to have to be a typical form. In no browser will you be able to get at file data (or even it's path) from Javascript. This is not a "known issue" -- it's a security feature. How would you like it if, in the background, SO secretly poked around on your hard drive looking for personal data?
There are plenty of tutorials out there on handling file uploads via jquery, I would start there.
Once you can POST a file (and any related metadata), look into CodeIgniter's form/file handling. I've not used CI, so I can't be much help there.
Then, I would suggest you seriously consider storing files in the filesystem, and simply storing (relative) paths to them in your database. I'm sure if I thought long and hard I could come up with a good scenario for storing file data in the database, but in the time it's taken to type up this answer, I haven't yet come up with one.
Upvotes: 2