PHP

Simple PHP Upload

On request I have provided a simple beginners guide to uploading file to the server. I would like to remind everyone this is just simple, very little (if any) security and options included within the script. This tutorial is designed to just walk you through actually uploading.

I am splitting the files in this tutorial into two files, one for displaying the form and one for processing the upload. I have done this to make the learning curve much easier for noobies :).

Ok, let us begin with the HTML page:

<html>
<head>
<title>Upload Dat File!!</tite>
</head>

<form action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="userfile" size="30"/>
<input type="submit" value=" Upload ">

</form>

</html>

Now this HTML page simply displays a form with one element, a file input box. The file input box allows you to select a file to upload and then when you are done you simply click submit and the form will transfer the file over to upload.php. The reason we use multipart/form-data is because this option allows us to split the file into many parts (so it uploads in bits) but keeps it as form data which allows us to submit the complete data like a form (one whole chunk). This method of splitting the file not only works better for the server but also for your Internet connection. It is nicer for your Internet connection since not many Internet connections in the world have a bandwidth of over 10MB (remember you might but what about everyone else) so uploading in parts allows us to upload a big file on any connection.

now for upload.php:

//Get the data for the file. When the file is sent to server it will not be within the $_POST or $_GET array, instead it will be within its own array called $_FILES. This array is then indexed by the name of your element i.e. userfile and then by the attribute i.e. name for file name.
				$fileName = $_FILES['userfile']['name'];
				$tmpName = $_FILES['userfile']['tmp_name'];
				$fileSize = $_FILES['userfile']['size'];
				$fileType = $_FILES['userfile']['type'];

//Just set a upload path for where your files will be stored. Make sure you run chmod 777 (or 755) on the folder before trying to upload.
$filePath = "/home/cheese/upload";
chmod($filePath, 0777 );

//After you have the folder and file path you need to move the file from its temp directory to the actual directory as so
if(@move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath)){
//do something in here to describe that the file has been uploaded
}else{
//do something in here to describe that there has been an error with uploading
}

When the form within the HTML page completes the file is saved to your temp directory (Operating System default unless specified) and then move_uploaded_file moves that file from the temp directory to the file path you specified. Please do be aware that on Windows you only need to specify the FOLDER but on Linux you need to specify the FILE.

This is very basic uploading now officially covered, enjoy 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s