Image Upload Script
Dit is een upload functie die gebruikt kan worden om je images te uploaden. Hij is vrij duidelijk. Als iemand nog feedback heeft hoor ik dat graag. Groeten, R. Vandenbussche Author: Ruben Vandenbussche Website: http://www.RVandenbussche.nl Contact: info (at) RVandenbussche (dot) nl Date: 11 Nov 2008 ******************************************* Script: Image Upload v1.0 It does: 1. Upload the image. 2. Rename the image. 3. Move the image. 4. Rename if already exists. 5. Check if image is valid if not it gets deleted It doesn't: 1. Upload other than .JPG, .JPEG, .GIF and .PNG. 2. Do anything to the image itself. 3. Create directories Summary: Function to upload an image from the formfield ($img_ff) to a specified path ($dst_path). It checks the image and gives it an other name ($dst_img).
<?php
// Author: Ruben Vandenbussche
// Website: http://www.RVandenbussche.nl
// Contact: info (at) RVandenbussche (dot) nl
// Date: 11 Nov 2008
//*******************************************
// Script: Image Upload v1.0
//It does:
// 1. Upload the image.
// 2. Rename the image.
// 3. Move the image.
// 4. Rename if already exists.
// 5. Check if image is valid if not it gets deleted
//*
//It doesn't:
// 1. Upload other than .JPG, .JPEG, .GIF and .PNG.
// 2. Do anything to the image itself.
// 3. Create directories
//*******************************************
// Summary:
// Function to upload an image from the formfield ($img_ff) to a specified path ($dst_path), check the image and give it an
// other name ($dst_img).
//Script start here.
function uploadImage($img_ff, $dst_path, $dst_img){
//Get variables for the function.
//complete path of the destination image.
$dst_cpl = $dst_path . basename($dst_img);
//name without extension of the destination image.
$dst_name = preg_replace('/\.[^.]*$/', '', $dst_img);
//extension of the destination image without a "." (dot).
$dst_ext = strtolower(end(explode(".", $dst_img)));
//Check if destination image already exists, if so, the image will get an extra number added.
while(file_exists($dst_cpl) == true){
$i = $i+1;
$dst_img = $dst_name . $i . '.' . $dst_ext;
$dst_cpl = $dst_path . basename($dst_img);
}
//upload the file and move it to the specified folder.
move_uploaded_file($_FILES[$img_ff]['tmp_name'], $dst_cpl);
//get type of image.
$dst_type = exif_imagetype($dst_cpl);
//Checking extension and imagetype of the destination image and delete if it is wrong.
if(( (($dst_ext =="jpg") && ($dst_type =="2")) || (($dst_ext =="jpeg") && ($dst_type =="2")) || (($dst_ext =="gif") && ($dst_type =="1")) || (($dst_ext =="png") && ($dst_type =="3") )) == false){
unlink($dst_cpl);
die('<p>The file "'. $dst_img . '" with the extension "' . $dst_ext . '" and the imagetype "' . $dst_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
}
}
//Script ends here.
// Needed for the function:
// If the form is posted do this:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
//Variables needed for the function.
$img_ff = 'image'; // Form name of the image
$dst_img = strtolower($_FILES[$img_ff]['name']); // This name will be given to the image. (in this case: lowercased original image name uploaded by user).
$dst_path = '../directory/'; // The path where the image will be moved to.
uploadImage($img_ff, $dst_path, $dst_img);
}
//HTML Starts Here.
<!-- // Form needed to upload the image.
// You can change the name of the form, dont forget to change that in the variable $img_ff.
// You can change the action file. (In this case i use the same name as this file.)-->
<form enctype="multipart/form-data" name="image" method="post" action="EmptyPHP.php">
<label for="image">Image:</label>
<input type="file" id="image" name="image" />
<br />
<input type="submit" value="Upload" /> <input type="reset" value="Reset" />
</form>
//HTML Ends Here .
?>
Reacties
0