Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a BLOB into MYSQL using PHP $_POST??? 1

Status
Not open for further replies.

VitoCorleone

Programmer
Jun 22, 2004
28
GB
hi,

is it possible to insert a file into my database table using the POST method in PHP. i have seen examples using the global attributes but i need to only use POST as i have an HTML form and a PHGP confirmation page which calls the previous page values to insert.

I have tried

mysql_query("INSERT INTO staff(staffno, image) VALUES ('" . $_POST['staffno'] ."','" . $_POST['image'] . "')")
or die(mysql_error());

How ever this does not work.
 
remember to escape the contents of the file after you have read it into a string.

i also find it useful to store the mimetype, filesize and dummy filename in the database in order to complete the header information prior to downloading.

 
Thanks that has cleared things up, However for admin purposes i need to insert the binary image straight into the MySQL db and not upload the file to a folder.

Any sites of reference? is it possibleto do this via $_files method?
 
I don't believe you can, at least not through PHP. If you upload a file to PHP, the language engine is going to store the uploaded file to the filesystem and provide information about how to access the file in $_FILES. Unless you want to hack the PHP sourcecode, you can't, to the best of my knowledge, change this behavior.

If you're worried about collisions of uploaded filenames, don't. PHP stores the uploaded file to the server's filesystem under a filename that it picks, and PHP's filename-generating subsystem is designed to prevent filename collisions.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
you can't do this unless you can access the file on the network from the web-server.

if you can do this, then you need to get the network location from the incoming form (not really a good way to do this other than having the user enter the full network address). you can then readfile the file and shove the contents into the db.

but the file upload using a file type input box happens automatically (unless disabled) and by the time you get to the next page in your script, the file upload has already happened and the file is sitting in a temp folder on your web-server. i believe (but am not certain) that the file gets deleted from the tmp folder once the script has completed. thus unless you have disabled file uploads and you cannot/do not wish to enable it, i recommend using file uploads rather than network fetches and would also recommend the php.net info that sleipnir214 pointed you towards.
 
Even with the IP address of the client, PHP isn't going to be able to fetch a file from the client unless the client provides some kind of server software through which PHP can fetch the file.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
really? i thought you could use fopen on a network share (assuming permissions set ok etc)? eg
Code:
$fh= fopen("file://compname/share/filename.ext","rb");

never had cause to do it myself but thought it could be done (assuming also allow_url_fopen is enabled).

I'll give it a whirl on a couple of test boxes.
 
That works because the machine making the network resource available is running server software which makes the resource available. However, anyone with a sufficiently prudent level of paranoia is not going to make network shares available from the internet. Even if the client machine is on the same network as the server, fetching the file will require that the client machine be configured to share the file.

Then there is the question of operating systems. For example, it is possible to open a Win32 network file resource from a Linux box, but it will not be as straightforward as simply using a "file://" protocol in an fopen.


VitoCorleone:
I recommend you just stick with the standard file uploads, and make sure your PHP scripts clean up after themselves on the filesystem after they've pushed the file into the database.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top