Hier is een script dat ik heb aangepast om met je tabelstructuur te werken.
function addImageToDB($imageArray, $title = '', $subject = '', $visible = 0) {
$allowedExts = array("gif","jpeg","jpg","JPG","png","PNG");
$extension = end(explode(".", $imageArray["name"]));
if (
(($imageArray["type"] == "image/gif") // is image type acceptable?
|| ($imageArray["type"] == "image/jpeg")
|| ($imageArray["type"] == "image/jpg")
|| ($imageArray["type"] == "image/png")
)
&& ($imageArray["size"] < 1048576) // set maximum image size
&& in_array($extension, $allowedExts) // is image file extension in $allowedExts?
) {
if ($imageArray["error"] > 0) { // check uploaded image for errors
echo $imageArray['error'];
} else {
$tempImage = $imageArray['tmp_name'];
$fp = fopen($tempImage, 'r');
$image = fread($fp, filesize($tempImage));
$image = addslashes($image);
fclose($fp);
$queryAddImageToDB = "INSERT INTO image (
title,
subject,
image,
visible
) VALUES (
'$title'
'$subject',
'$image',
'$visible'
)";
mysql_query ($queryAddImageToDB) or die ('queryAddImageToDB failed');
$imageID = mysql_insert_id();
return $imageID;
}
} else {
echo 'IMAGE UPLOAD ERROR: The image ie either too large or the file format is unacceptable.';
echo '<pre>';
print_r($imageArray); // display image array for debugging
echo '</pre>';
}
';} }
Je kunt de functie als volgt aanroepen:
$imageArray = $_FILES['image'];
$title = $_POST['title'];
$subject = $_POST['subject'];
$visible = 1;
addImageToDB($imageArray, $title, $subject, $visible);
Houd er rekening mee dat dit script NIET VOLLEDIG IS, omdat het de juiste validatie, escape, enz. nodig heeft.
Veel succes, ik hoop dat dit voor je werkt en anders hoor ik graag feedback.