05-28-2008, 12:42 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
|
You can change the name in the move_uploaded_file(), as a second argument just specify the filename (and path) you desire.
In this script you don't check for safety, you should always check the Mimetype and extention of the file you are uploading.
Here a little function i made a while ago, i recommend you check this function for ideas how to make your script be save. (Oh and i also included a little function that makes tumbnails)
PHP Code:
function uploadFoto($foto, $naam) {
// Wat voor extenties zijn toegestaan?
$extenties = array('jpg', 'gif', 'jpeg', 'png', 'pjpeg');
// Wat voor mimetypes zijn toegestaan?
$mimetypes = array('image/jpeg', 'image/png', 'image/pjpeg', 'image/gif');
// Wat is de max size in bytes?
$maxsize = 2000000; // 1.5 / 2 mb
// Haal extentie van bestand
preg_match('/^(.*?)\.([a-zA-Z0-9]{2,5})$/is', $foto['name'], $match);
// Check of bestand goede extentie heeft
if(in_array(strtolower($match[2]), $extenties)) {
// Extentie goed
// Is bestand onder de maximale grootte?
if($foto['size'] < $maxsize) {
// Foto is goede size
// Foto heeft goede mimetype?
if(in_array($foto['type'], $mimetypes)) {
$image = $foto['tmp_name'];
$ext = $match[2];
// Open file adv extentie
switch(strtolower($ext)) {
case "jpeg":
$source = @imagecreatefromjpeg($image);
break;
case "jpg":
$source = @imagecreatefromjpeg($image);
break;
case "pjpeg":
$source = @imagecreatefromjpeg($image);
break;
case "gif":
$source = @imagecreatefromgif($image);
break;
case "png":
$source = @imagecreatefrompng($image);
break;
default:
return FALSE;
}
//
// source dimenties
$sWidth = imagesx($source);
$sHeight = imagesy($source);
// Maak nieuwe file
$file = imagecreatetruecolor($sWidth, $sHeight);
imagecopyresampled($file, $source, 0,0,0,0, $sWidth, $sHeight, $sWidth, $sHeight);
// Sla image op
imagejpeg($file, 'images/upload/'.$naam.'.jpg');
// Maak en resized bestand
copyResized('images/upload/'.$naam.'.jpg', 100, 100);
return $naam.'.jpg';
} else {
echo'Het bestand heeft geen goede mimetype.';
return FALSE;
}
} else {
// Foto te groot
echo'Het bestand '.$foto['name'].' is te groot, we accepteren maximaal 2 mb.';
return FALSE;
}
} else {
// Extentie niet goed
echo'Extentie van '.$foto['name'].' is niet toegestaan, we accepteren enkel jpg, png, bmp en gif. Deze foto had: '.$match[2];
return FALSE;
}
}
// kopieer een resized image
function copyResized($image, $width, $height) {
// Bestandsnaam
$fileName = basename($image);
// Get extentie
preg_match('/^(.*?)\.([a-zA-Z0-9]{2,5})$/is', $fileName, $match);
$ext = $match[2];
// Open file adv extentie
switch(strtolower($ext)) {
case "jpeg":
$source = @imagecreatefromjpeg($image);
break;
case "jpg":
$source = @imagecreatefromjpeg($image);
break;
case "pjpeg":
$source = @imagecreatefromjpeg($image);
break;
case "gif":
$source = @imagecreatefromgif($image);
break;
case "png":
$source = @imagecreatefrompng($image);
break;
default:
return FALSE;
}
// Kan image openen?
if($source == FALSE) {
echo 'Kan tumb image niet openen.<br>';
return FALSE;
}
// Dimenties
$sourceWidth = imagesx($source);
$sourceHeight = imagesy($source);
// Maak nieuwe file
$tumb = imagecreatetruecolor($width, $height);
imagecopyresampled($tumb, $source, 0,0,0,0, $width, $height, $sourceWidth, $sourceHeight);
imagejpeg($tumb, 'images/upload/'.$match[1].'_tumb.jpg');
}
?>
__________________
Nunchaku! Who doesn't like martial arts? =)
|
|
|