Have the HTML form have this tag definition added: ENCTYPE="multipart/form-data"
Have a field in the HTML form of: <INPUT TYPE=FILE NAME="imagefile">
The following functions do some fun stuff:
function ImgCheck() {
global $imagefile, $imagefile_name, $img, $err;
if ($imagefile and $imagefile != 'none' and $imagefile != '') {
//split up the filename and see if its an acceptable image type
$imgfilename = basename($imagefile_name);
$tmpjunk = explode('.',$imgfilename);
$n = count($tmpjunk) - 1;
$img['ext'] = strtolower($tmpjunk[$n]);
//set up image details array
$tmpsize = GetImageSize($imagefile);
$img['width'] = $tmpsize[0];
$img['height'] = $tmpsize[1];
if ($img[width] < 1) $img['width'] = '0';
if ($img[height] < 1) $img['height'] = '0';
//is it ok?
if ($img[ext] !='gif' and $img[ext] !='jpg' and $img[ext] !='jpeg') {
$err['imagefile'] = true;
$err['imagefiletype'] = true;
return false;
}
//its ok
else {
return true;
}
}
//no image uploaded
else {
return false;
}
}
function ImgSave($filename,$imagetype) {
global $db, $config, $imagefile;
$imglocation = "$config[path_webfilesroot]/images/$imagetype/$filename";
if (file_exists($imglocation) and is_file($imglocation)) {
unlink($imglocation);
}
return copy($imagefile,$imglocation);
}
function ImgDelete($filename,$imagetype) {
global $config;
$imglocation = "$config[path_webfilesroot]/images/$imagetype/$filename";
if (file_exists($imglocation) and is_file($imglocation)) {
unlink($imglocation);
}
}
Now in your php script your HTML form action goes too, you can hanlde the image with the following code:
$config['path_webfilesroot'] = '/path/to/html/dir';
if (ImgCheck()) {
$filename = $forms_id.'.'.$img[ext];
$imagetype = 'screenshots';
ImgSave($filename,$imagetype);
}
(then store to database the imagename.ext so you can use it later, you could also optionally store the $img[width] and $img[height] too)
If you wish to delete an image, use the delete function:
$filename = (pull from database, that image name you stored)
$imagetype = (pull from database of what image group it was)
ImgDelete($filename,$imagetype);
You'll have to doublecheck the code for errors this website did to it... sometimes things get ripped out.