You have two main options. You can save the uploaded image as data in a MySQL blob field or you can save the actual file in a folder and reference it in a MySQL varchar field (filepath).
You can access the file dimensions, file size and file type in order to access/refuse the image. You have a choice as to the dimensions (e.g. 50 x 50), file size (e.g. 100Kb), file type (usually GIF, JPG, JPEG, PNG).
If the dimensions are greater than the allowed, you can simply refuse them or scale them yourself to the maximum allowed size.
Example:
list($w, $h) = getimagesize("$_FILES['upload']['tmp_name']");
Will give you the file dimensions ($w and $h).
$maximagesize = 307200 //(300kb or is that kib - can never remember)
if ($_FILES['upload']['size'] > $maximagesize) {
...
}
Will check your filesize
$xtn = strtolower(strrchr($_FILES['upload']['name'], ".")); //the reverse is impt because names could involve more than one period
if ($xtn!= ".gif" AND $xtn!= ".jpg" AND $xtn!= ".jpeg" AND $xtn!= ".png") {
...
}
Will check your extensions. Don't rely on Ajax/JS-based stuff as these can be bypassed.
Here's a scaling script from the php manual: http://www.php.net/manual/en/function.getimagesize.php#97564
Anyway, have fun.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Many people tend to store pictures as external files and keep only a reference in the database.
I'd rather recommend to storing them in the database in blob fields as you won't have to deal with file or directory names, updating image files and the like.
The process for that is:
Upload the file and read it from the local temporary into the database.
Create a PHP script which outputs the image field content to the browser. Include a matching image header into the script (e.g. header("Content-type: image/jpeg").
When generating the HTML with images in it, create references to your image display script instead of references to image file names.
// ardav beat me by 2 minutes ;-(
smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
First of all it's a blob field. I always use a ref and real files myself. I find the storage and retrieval a bit of a phaph. Anyway, each to his own. Each has its pros and cons.
AS smant said - use a ref to the script, if you go down the blob route, e.g.
<img src="createimg.php?id=634732" />
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080