hvad er galt i følgende upload af billede kode?
Hvad hulen er galt med følgende kode? Den består af 2 filer. 1. upload, og 2 update.php3Min mysql-tabel ser således ud:
CREATE TABLE `billede` (
`id` int(11) NOT NULL auto_increment,
`thumbnail` blob NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
1. upload
<HTML>
<form enctype="multipart/form-data" method="post" action="update.php3" name="form1">
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="photo" accept="image/jpeg">
<input type="hidden" name="id" value="12345">
<input type=submit value="Send">
</form>
</HTML>
2. update.php3
<?php
$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{ $aFileNameParts = explode(".", $sPhotoFileName);
$sFileExtension = end($aFileNameParts); // part behind last dot
if ($sFileExtension != "jpg"
&& $sFileExtension != "JPEG"
&& $sFileExtension != "JPG")
{ die ("Choose a JPG for the photo");
}
$nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
if ($nPhotoSize == 0)
{ die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
}
if ($nPhotoSize > 102400)
{ die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
}
// read photo
$sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
$oTempFile = fopen($sTempFileName, "r");
$sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
// Try to read image
$nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
$oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
error_reporting($nOldErrorReporting);
if (!$oSourceImage) // error, image is not a valid jpg
{ die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
}
}
?>
<?php
$nWidth = imagesx($oSourceImage); // get original source image width
$nHeight = imagesy($oSourceImage); // and height
// create small thumbnail
$nDestinationWidth = 80;
$nDestinationHeight = 60;
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
/*$oResult = imagecopyresampled(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
*/
imagecopyresized(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
ob_start(); // Start capturing stdout.
imageJPEG($oDestinationImage); // As though output to browser.
$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
ob_end_clean(); // Dump the stdout so it does not screw other output.
?>
<?php
require_once("../members/common.php");
$sBinaryThumbnail = addslashes($sBinaryThumbnail);
$oDatabase = mysql_connect("localhost", "softplanet_dk", "xxxkodexxx");
mysql_select_db("softplanet_dk", $oDatabase);
$sQuery = "UPDATE billede
SET thumbnail = '$sBinaryThumbnail',
WHERE id = '$nId'";
mysql_query($sQuery, $oDatabase);
?>
