Tutorial
Hi guys,
This is a little example of how to manage your uploads. Im not going to explain every single line because im only using the basics from PHP.
Script made by Flamer from Artwork-creations.com, made famous by Remco from Scripterplace.com!
Lets get started!
First, create a folder on your root directory where you wish to upload your files to and make sure to chmod it to (0)777 http://en.wikipedia.org/wiki/Chmod, im using a folder called "uploads".
Second, create a index.php file and insert the following code:
Code:
<?php
if(isset($_POST['submit'])){
// *** Array with allowed mime types
// *** To add just search the mime type on http://www.w3schools.com/media/media_mimeref.asp
$allowed_ext[] = "application/msword"; # Doc, dot, and maybe docx?
$allowed_ext[] = "audio/mpeg"; # mp3
$allowed_ext[] = "audio/mid"; # midi
$allowed_ext[] = "audio/x-wav"; # wav
$allowed_ext[] = "image/bmp"; # bmp
$allowed_ext[] = "image/gif"; # gif
$allowed_ext[] = "image/jpeg"; # jpg, jpeg, jpe
$allowed_ext[] = "application/pdf"; # pdf
// *** Get the mime type of the uploaded file
$mimetype = getimagesize($_FILES['file']['tmp_name']);
// *** Max allowed file size in bytes
$max_size = 10240;
if(in_array($mimetype['mime'],$allowed_ext) && ($_FILES['file']['size'] * 1024) >= $max_size){
// *** This assigns the subdirectory you want to save the file ||
// *** Make sure it exists and is chmodded 777!
$target = "uploads/";
// *** This combines the directory, the file name with extension
$target .= $_FILES['file']['name'];
// *** If the file is copied/file succesfully to the
// *** desired location it will print true, otherwise it will send false
if(@move_uploaded_file($_FILES['file']['tmp_name'], $target)){
print("The file named ".$_FILES['file']['name']." with a size of ".$_FILES['file']['size']."bytes has been succesfully uploaded");
} else {
if(($_REQUEST['file']['size'] * 1024) > $max_size){
$max = "<br>Bestand is groter dan $max_size Bytes";
}
print("The file named ".$_FILES['file']['name']." and a size of ".$_FILES['file']['size']."bytes has <b>NOT</b> been succesfully uploaded.(".$max)." or less is allowed)";
}
} else {
print("Its not allowed to upload this type of file!!");
}
}else{
?>
<html>
<head><title>Upload tut by Flamer || artwork-creations.com</title></head
<body>
<form action="index.php" name="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input name="submit" type="submit" value="Upload" />
</form>
</body>
</html>
<?
}
?>
Change $target to your upload location browse to your index.php to perform test'
The lay down!
Im using one big IF/ELSE statement to check if the upload form has been submitted. If this returns true then some code begins to run. If it returns false then it will display the HTML form.
If the upload form is submitted then the code will create a array called $allowed_ext[] with a few mime types (http://en.wikipedia.org/wiki/MIME]Mime). This will be our 'whitelist' with allowed filetypes.
After this i substract the file mime type with
Code:
$mimetype = getimagesize($_FILES['file']['tmp_name']);
This is done in the seconde IF/ELSE statement:
Code:
if(in_array($mimetype['mime'],$allowed_ext) && ($_FILES['file']['size'] * 1024) >= $max_size){
in_array is a standard function to check if the givin value is inside the selected array. If so, it returns a true. The second condition is that the filesize must be under the allowed filesize. This is all in bytes. the allowed filesize is declared above the statement in the var named $max_size.
Next thing is to set the desired upload location by setting $target.
Code:
$target = "uploads/";
Remeber to place the / at the end of the location because we need to paste the filename behind it like
Code:
$target .= $_FILES['file']['name'];
If the file comes true all the conditions then it is ready to be uploaded to your target location:
Code:
if(@move_uploaded_file($_FILES['file']['tmp_name'], $target)){
print("Het bestand genaamd ".$_FILES['file']['name']." ter grootte van ".$_FILES['file']['size']."bytes is succesvol geupload");
}
Now you notice i used $_FILES[] over in this code. Its just like the $_GET and $_POST value only this way you can seperate files better. Remember to palce the first value as the array name, which is the name of the file input type as declared in your html.
I have left out the error messages but thats all just standard stuff. If needed you can comment me here or send me a pm and ill assist if any problems occurs.
| « Previous | Next » |
Comments
Flamer - 2009-07-24
27 views and no comments?
shame shame..
Nick - 2009-08-02
alright, your comment! hehe.
It isn't working my friend, it only works with images because you're using getimagesize.
So this is only good for image upload, for other things like .doc it isn't working ;) But you already know that because we are looking for something to fix it ^^
It isn't working my friend, it only works with images because you're using getimagesize.
So this is only good for image upload, for other things like .doc it isn't working ;) But you already know that because we are looking for something to fix it ^^
You have to be logged in to write a comment.


