Jump to content

Adding pictures into SQL


Fukushousha

Recommended Posts

Hello friends!My project is proceeding smoothly (or so I want to think) and I am in a very good mood :)This morning I was looking at how I could add images into my users SQL table. Is this what we call a BLOB? In addition how do I add it from php and how do I check an image's size and resize it if need be? I realize I ask a lot of questions in very few lines. There is no need to answer me with a full text if you are bored, just point me to a nice tutorial if it exists since I could not find something :)

Link to comment
Share on other sites

BLOB is the right data type to store image data in. You can just retrieve the images content and INSERT

INSERT INTO image (image) VALUES ('" . mysql_real_escape_string(file_get_contents("image.gif")) . "')");

You can resize images with imagecopyresampled() http://au2.php.net/manual/en/function.imagecopyresampled.php and the easiest way to check the file size is while it is still in the filesystem (e.g. as a temp file just after uploading).

Link to comment
Share on other sites

Thanks a lot synook. That really helped. I was using 150x150 user images and was naive enough to think that a tinyblob would be okay >_<Anyway ... thanks a lot!As for recalling that image from the DB is this the right way? I used my test php file for the testing:

/*Already tested ! :)$istring = file_get_contents("css/defaultUImage.png");$sql->updatetable("users","userImage","'".mysql_real_escape_string($istring)."'","username","'Lef'");*//*Now lets see that image*/$arr = $sql->select("SELECT userImage FROM users WHERE username='Lef'");file_put_contents("tempImage.png",$arr['userImage']);?><html><img src="tempImage.png" /></html>

It worked ofcourse but I just wanted to check with you guys and see if I am doing this correctly. Again thanks!

Link to comment
Share on other sites

You don't have to go so far as creating a temporary static file, you can dynamically generate the images with PHP and serve the page with a image content-type.user_image.php

<?php//connect to DB$user = mysql_real_escape_string($_GET['user']);$image = //get image data from DB WHERE username=$user;$content_type = //get content_type from DB;//(you can store the type at upload, there is an index in the $_FILE array)header("content-type", $content_type);echo $image;?>

To get Lef's image:

<img src="user_image.php?user=Lef" />

Link to comment
Share on other sites

Thanks synook, I will keep that in mind. Thanks for answering :)EDIT: Also if anyone can help with this ... how the heck did you do that up there? With that code? I thought that http headers were used to specify stuff about the data to be sent. You can specify what is about to go trhough so that the browser will know how to "parse" it? Because without the header I get a bunch of gibberish

Link to comment
Share on other sites

If you leave out the content-type header then PHP will send the default content-type header, which is text/html, so the browser will try to render the binary image data as an HTML page. The content-type tells the browser what type of data it is. It would be something like image/jpeg or image/gif depending on the image.

Link to comment
Share on other sites

Hmm I see. Actually I did not figure out how to get the image type. From the DB at least. When I upload it I can do it easily from the $_FILE['name']['type'] assoc array. But when I read it from the DB as a string in order to output it to the user I don't know how to get the type. But since I knew the php script would be used with images I put image/jpeg as the content type and it displayed it (it was saved by me as a .png). Would I have a problem with other image types later if I do it like that? If yes how can I get the image type directly from the DB? The only way I can think of is to save the image type too at upload.

Link to comment
Share on other sites

Yeah, you need to save the image type also. Instead of saving the type, it will probably be better to save the extension, and then use the extension to figure out what type to use. Different browsers will send different types for the same thing, so it's best to use the extension.If you use something like jpeg for everything then you may lose some features, like transparency. If you save your PNG with a transparent or partially transparent background then you'll see what I mean.

Link to comment
Share on other sites

Okay I will do so. Thanks for the tip.One more question (Sorry for the barrage but the whole area has a lot of literature surrounding it)This is the code that executes if the user click the submit picture button of a file input form:

//if the user clicks the submit picture buttonif(isset($_POST['subpic'])){	//then let's check the $_FILES array for it and get it	$istring = file_get_contents($_FILE['userpic']);		//use the real escape string since it is binary data	$imageData = mysql_real_escape_string($istring);		//then add it to the table 	$sql->updatetable("users","userImage","'".$imageData."'","username","'".$username."'");}

The problem is that the image does not appear at the recall from the Database. I can rule out that the recall from the DB code is correct since I have used it before. The problem must be on the above code. Specifically I am not sure if the file(image) data is saved in the $_FILE['userpic'] assoc array as I used it above. Thanks for the help again!

Link to comment
Share on other sites

$_FILES['userpic'] is an array. $_FILES['userpic']['name'] contains the filename. You'll also want to check in $_FILES['userpic']['error']. And note that it's $_FILES, not $_FILE. You might also want to enable all error messages:ini_set("display_errors", 1);error_reporting(E_ALL);http://www.php.net/manual/en/features.file-upload.php

Link to comment
Share on other sites

Aha! Thanks again someguy. Imagine ... I was using the link you gave me as a reference but still I used $_FILE instead of $_FILES ... sometimes, you need a second pair of eyes >_<EDIT: Damn guys sorry ... I feel like I am overusing w3schools forum but I really can't find what is wrong here. The past 2 hours I was googling to find the reason why I get an undefined index error but I still don't get it. I understood many other things about the whole proccess but I don't get the undefined index error.So here it is, the code for the form

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">		<input type="hidden" name="MAX_FILE_SIZE" value="6000" />		Want to change your picture?<input name="userpic" type="file" />		<input name="subpic" type="submit" value="Upload New Picture" />		</form>

Nothing difficult ... I copied it yesterday when I worked on it and modified from the php manual. Still though I get an undefined index for the $_FILES['userpic']['whateverIneedfromthefilegoeshere'] ....I don't know what to do. I checked php.ini and confirmed that file_uploads are on ... I am using easy php with phpmyadmin. Is this some known glitch it has?Also another question more directed to justsomeguy. You said that I should also check the size of the image(whenever I get past the index error). Isn't this hidden input field supposed to do just that? That is the idea the php manual gave me.

Link to comment
Share on other sites

You need to check the error code for the file, it will contain a number based on what happened. 0 means success, 4 means they didn't pick a file, and the other values mean different things. The error codes are all explained in the PHP manual that I linked to.If the max size is set to 6000 then there might be an error that the file is larger than the allowed size, I'm not sure if it would set anything other than the error code. You can always use print_r($_FILES) to see what's in there though.

Link to comment
Share on other sites

Yeah but the problem lies exactly there. I tried checking to see what the error was but to do so I needed to check inside the $_FILES array. For which I still got an undefined index.Then I tried what you said with print_r to see the contents of the array ... and it is empty. That can only mean that the file is not uploaded. But I can not understand the reason why such a thing would happen.EDIT: As for the file size I had thought to check but still with bigger size limit it doesn't work.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...