Loading...
Loading

How To Upload Files Using PHP

2004-11-19by Ben Sinclair

OK, so you want to upload an image or file to your server without using your FTP? This is very simple. This tutorial will teach you how!

The Form

First of all you need to create your form to select the file you wish to upload:

form.php

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
</form>


One thing a lot of people forget is this: ENCTYPE="multipart/form-data". If that is not there, it will not work. I've forgotten it before and it took me over half an hour to work out why the script wasn't working...

The Upload Part

Now you need the script to upload the file:

upload.php

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions (CHMOD) is 0777!
// ==============
// Upload Part
// ==============
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
?>

 

That's it! If you are getting errors or the file just won't upload, make sure that the folders permissions is 0777 where you are uploading the files to. Also check that the folder exists!

Going Advanced...

But what if you want to go a little more advanced... Like choosing what file extensions that are allowed or the maximum height width of an image and even size?

To do this, first you will need to add a few things to your confuration:


<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, gif, png, pdf"; // These are the allowed extensions of the files that are uploaded
$max_size = "50000"; // 50000 is the same as 50kb
$max_height = "100"; // This is in pixels
$max_width = "100"; // This is in pixels
?>

 

And then add this code below the configuration:


<?php
// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
&nbsp; &nbsp;if ($allowed_paths[$i] == "$extension") {
&nbsp; &nbsp;$ok = "1";
&nbsp; &nbsp;}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
?>

 

And there you go. That will check your file uploads. Here is the entire code if you need it:

form.php

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
</form>


upload.php

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, gif, png, pdf"; // These are the allowed extensions of the files that are uploaded
$max_size = "50000"; // 50000 is the same as 50kb
$max_height = "100"; // This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "100"; // This is in pixels - Leave this field empty if you don't want to upload images

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
&nbsp; &nbsp;if ($allowed_paths[$i] == "$extension") {
&nbsp; &nbsp;$ok = "1";
&nbsp; &nbsp;}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
} else {
print "Incorrect file extension!";
}
?>

 

Enjoy!

news Buffer
Author

Ben Sinclair

Ben Sinclair is the Webmaster of Webmaster Resources 101(www.webmaster-resources101.com) View Ben Sinclair`s profile for more
line

Leave a Comment