Publishing System Settings Logout Login Register
Create a multiple file uploader
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on March 3rd, 2007
6340 views
PHP Coding
Introduction

Welcome to this tutorial on how to upload files using PHP before we start i just want to tell you how the tutorial will be structured. Firstly i will set up the part off the script which will allow us to choose how many files we wish to upload, then display that form and finally upload and report back on the files.

This script is designed as a learning aid however it is a fully working script so you may use it if you want but please not that its not the most efficient way to do this. I can't stress enough THIS SCRIPT IS TO TEACH.

Support

If you require support for this tutorial then please don't hesitate to PM me on the p2l forums. Contact me via my blog or send me a email



Step 1 - Setting up how many files we wish to upload

I'm gonna start off by pasting the code and then explaining it.

<style type="text/css">
<!--
form, .form {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-color: #FFFFCC;
border: 1px solid #999999;
padding: 5px;
}
-->
</style>
<?php
// Upload Script By Darius Aliabadi.
// I will comment the code for people who copy and paste.

// To begin with this is a multi-step upload system therefore we will use the switch function to distinguise between the steps.
switch(@$_GET['step'])
{
default:
// The variable $path is so the form knows where its going.
$path = basename(__FILE__)."?step=uploadfiles";
echo '
<h1> Uploader </h1>
<form action='".$path."' method="post" enctype="multipart/form-data">

No. of pictures: <input type="text" name="number" />

<input type="submit" value="Send" />

</form>';
break;


Ok well you may have noticed that there is some CSS, just ignore it. Its not part off the upload script its just to make the form look pretty. Ok well lets begin with the script.

The switch function - line 17

Randomer: An uploader using a switch function? What that doesn't make sense.

Your right its not for the upload part, seems as this is a multi-part upload form im using the switch function to define the multiple parts.

Line 21

 basename(__FILE__) 


Some off you may not be familiar with that particular function, it simple gets you the name off the file. saves you having to change it when doing new forms.

Thats all for this part folkes.



Step 2: Generating the upload form

 case 'uploadfiles':
$totalpictures = $_POST['number'];

echo '
<h1> Uploader </h1>
<form action=".basename(__FILE__)."?step=upload"." method="post" enctype="multipart/form-data">
<p>Folder name: <input type="text" name="dir" /></p>
<p>Files:<br /><br />';
// Using a while loop to generate the upload boxes.
$i = 0;
while ($i != $totalpictures){
echo '<input type="file" name="files[]" /><br />';
$i = $i + 1;
}
echo '
<input type="submit" value="Send" />
</p>
</form>
';

break;


Remember that when using the switch function (for dynamic URL's) that we use case to define the names that go on the end off them i.e index.php?example=yesitis would have case 'yesitis'

This part off the script is simply a form generation, i am using a type off loop here called a while loop, there are several other types off loop such as for loop, foreach loop and do-while loop. I will also be covering the foreach loop.

In the case off a while loop it will keep going untill a condition is satisfied. In this case its untill the incrementing number is equal to the total number off files uploaded.

You can also do this using the for function


for($i = 0; $i == $totalpictures; $i++)
{
//code
}

 echo "<input type="file" name="files[]" /><br />"; 


In this case the name off the file upload field is the important part, notice it has square brackets after it [] this means that its part off an array.




case 'upload':
echo '<h1> Uploader </h1>
<div class='form'>
';
foreach ($_FILES["files"]["error"] as $key) {
$dir = $_POST['dir'];
if(!is_dir($dir))
{
mkdir($dir);
if(!is_dir($dir)){
die("Can't create directory");
}
}
if (is_dir($dir))
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($tmp_name, $dir."/".$name);
echo "<p>{$_POST['dir']}/$name - Uploaded Successfully :)</p>";
}
}
echo "</div>";
break;


Line 5

A foreach loop is used here simply because for each set off variables in the array are used one after another. You can see some details on the foreach function here. The first thing we want to do is create the directory however there is no point in creating something thats already there. Line 7 is the first directory check we make. We use the is_dir() function to see if the direction is present, if its not we create it (line 9) using the mkdir function.

I then added another check to make sure the directory was created however you don't need to include this because if it doesn't create the directory the files won't upload correctly but its just good practice.

Finally if the directory exists we upload the files.

 
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($tmp_name, $dir."/".$name);


When PHP uploads a file it uploads it to a temporary place with a temporary name until its moved to its final position therefore we need the name off the temporary file before we move it. We use the built in $_FILES functions for this. The variable is built up like this, the first square bracketed area is name off the upload field in the form, the second is what we want from the array off values stored and finally the last is row in the array. The same applies to the second line off code.

Finally we use the move_uploaded_file() function to move the uploaded file (funnily enough), this consists off where the file is and where its got to go these are inputted into the function respectively.

Thats all folks, the full code is on the next page.

Arutha




Full Code

<style type="text/css">
<!--
form, .form {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-color: #FFFFCC;
border: 1px solid #999999;
padding: 5px;
}
-->
</style>
<?php
// Upload Script By Darius Aliabadi.
// I will comment the code for people who copy and paste.

// To begin with this is a multi-step upload system therefore we will use the switch function to distinguise between the steps.
switch(@$_GET['step'])
{
default:
// The variable $path is so the form knows where its going.
$path = basename(__FILE__)."?step=uploadfiles";
echo '
<h1> Uploader </h1>
<form action='".$path."' method="post" enctype="multipart/form-data">

No. of pictures: <input type="text" name="number" />

<input type="submit" value="Send" />

</form>';
break;

case 'uploadfiles':
$totalpictures = $_POST['number'];

echo '
<h1> Uploader </h1>
<form action=".basename(__FILE__)."?step=upload"." method="post" enctype="multipart/form-data">
<p>Folder name: <input type="text" name="dir" /></p>
<p>Files:<br /><br />';
// Using a while loop to generate the upload boxes.
$i = 0;
while ($i != $totalpictures){
echo "<input type="file" name="files[]" /><br />";
$i = $i + 1;
}
echo '
<input type="submit" value="Send" />
</p>
</form>
';

break;
case 'upload':
echo '<h1> Uploader </h1>
<div class='form'>
';
foreach ($_FILES["files"]["error"] as $key) {
$dir = $_POST['dir'];
if(!is_dir($dir))
{
mkdir($dir);
if(!is_dir($dir)){
die("Can't create directory");
}
}
if (is_dir($dir))
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($tmp_name, $dir."/".$name);
echo "<p>{$_POST['dir']}/$name - Uploaded Successfully :)</p>";
}
}
echo '</div>';
break;
}

?>
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Arutha

As much as i love the default message i want to just say hello and to tell you to visit my blog :)
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top