$$::
Create File-Upload website using PHP:
This exclusive tutorial will show you how to
write a PHP program that can upload images to
your web site and you will learn how to create a
library consists from all uploaded images.
You will learn how to create it by two methods,
the first is that we will create it using a
TEXT file for retrieving and inserting data, The
second method is that we will create it using a
database for inserting and retrieving data.
Try The Script Here
You
will learn how to create a form consists
of a button ask the user to specify a
file.
You
will learn how to write a PHP code
responsible for uploading a file.
You
will understand the mechanism of files
uploading.
How to
write to and read from a text file.
How to
use session.
Firstly
You
should view this script on a web server or a
local host server in order to understand it .And
for dealing with script without any errors you
must change the ( $root ) variable in upload.php
file (line 22) to your (uploads) folder
path for example:if you upload our script to
http://www.example.com/script/you must change the $root to
"http://www.example.com/script/uploads/";
STEP:1Our script consists of 4web pages and a text file contains all images directories and uploads
folder contain all images uploaded.We need to
create a form that ask the user to specify an
image to be uploaded .to do that form write the
following script in the
uploadform.php
.
<?
session_start();
$_SESSION['firsttimeupload']= "ok";
?><body
bgcolor="#FF6699">
<br />
<br />
<br />
<br />
<center>
<p>Choose the image you want to upload, The
image must be less than 100 kb in size.</p>
<form action="upload.php"
enctype="multipart/form-data"
method="POST">
<p>Select a file:
<input type="file" name="user_file"
size="50">
<br />
<br />
<input type="submit" name="submit"
value="Upload Now" />
</p>
</form>
<br><h2><a href='library.php'>View Images
Library</a></h2>
</center>
NOTE: we use
$_SESSION[] variable to store a value in a session
and in this case we stored the value ((ok)) in the variable ((firsttimeupload))
, And you must enter this part of PHP code at the top of the script and I
will explain why i used the session commands
STEP 2 :Once the user
specify the file and press "upload now" button the
file will be uploaded to the tmp folder of the
server and wait your script to move it to the
permanent place.we must Know some
things about the uploaded file like its
name, its size and its type and to do that we will
use the
$_FILES [ ] super variable that
stores many information about the file in an array
and we can retrieve them easilyfor example :if we want to get
the file name we will type
$_FILES['user_file']['name']and if we want to
get the size of the file in bytes we will use
$_FILES['user_file']['size']NOTE that the
user_file variable is the name of the input located
in the form And NOTE that
$_FILES[]
also does the work of
$_POST[] in retrieving data from a form.the first chunk of
our script will be
After we get
information we need, we should change the name
of uploaded image to ensure that there is no image
overwriting will be. and also to struggle any
hacking attempts So we want a unique name
for each image.for that reason I
decided to use the
UNIX time as a name for images
(UNIX time is the time by seconds since1970 and we
can get it by
time( ) function )
$time=time();
$n=$time;
$path="uploads\\". $n;
After that we must
ensure that the file is in an image format and its
size is below 100000 bytes.NOTE when we check the file type by
$_FILES[ ] it returns
>>> image/file-extension <<< in case of it
was an
image file, for example (image/jpeg)So to ensure that
the uploaded file is in an image format
we should check the presence of "image" string
in the returned file type and to do that we
will use the
ereg ( ) function.For moving the
uploaded image from the tmp directory to its
permanent one we will use a PHP function called
move_uploaded_file( ) which receive
two parameter the first one is the current path of the file
and the second is the new path you that want to move
the
image to it.
if
($size<100000 && ereg("image", $type))
{
move_uploaded_file($_FILES['user_file']['tmp_name'],
$path);/////we
will add other decisions here
}
After that we want
to print the link to the user and view the image to
him So we will put the following code in the previous
if statement code
We want to copy the
link of the uploaded image to a text file because we
want to create a library contains all images
uploaded by users and this library will retrieve all
images directories from this text file:
NOTE : the
fopen( ) function
is used for opening any text file, and it receive
two parameter, the first is the path of the text
file and the second is the mode (whether writing or
reading or both and in this case the "a"
in "at" mode means: Write to the file and don't
delete the file contents, and "t" means open the
file as a text file)NOTE : we
concatenate the
$path2
variable with "\n"
to write each path in an independent line in
the text file
STEP 3:
Now we want to
put our decisions about any file more than our
size limits and also about any file in a formats
rather than image format:
if
($size>100000)
{
echo "ERROR <br> the image size is too big";
}
if (!ereg("image", $type) )
{
echo "ERROR <br> the file is not image<br>";
}
STEP 4:
Create The Library First we want to
retrieve the links of images from the text file and
so that we will use the
file() functionthe
file( ) retrieves all data from a text
file and store them in an array NOTE
the
explode( )
function create an array from an exploded string
.and we will explode each line into two parts, the
first one is the "\n" and the second is the
directory and then we will store the second part
(directory) in a variable called
$link
STEP 4: Why we used
that session?!!if you upload
an image and then refresh the upload.php page
you will find that the uploaded image is
uploaded again and stored in the library.So what we will
do if a user upload an image and then refreshed
the upload.php page 100 times?!! you will find
that this image was uploaded 100 times and 100
image were add to your libraryfor that reason
i used the sessionin the
uploadform.php page which is the first
page that user will see I created a session and
in it I create a variable named
"firsttimeupload" and stored the value "ok" in
this variable .in the
upload.php which is the second page we will
check the value of the "firsttimeupload"
whether it is "ok" or not. And if it was "ok" we will
move the image to the permanent folder and write
its path in the text file and then Destroy the
session using
session_destroy ( );
$uploaded =
$_SESSION ['firsttimeupload'];
if ($uploaded == "ok")
{
if ($size <100000 && ereg("image", $type ))
{
/////put the code responsible for moving
file and writing its path in the text.
session_destroy();
}}
Consequently
when the user refresh the page , the if
statement will search for the value of "firsttimeupload"
and certainly the if statement will not find the session and
then it will not move the image, or write it in
the text file. and it will print a message for
the user tells him that he cant upload the same
image more than one times and he must go back
*(why?? >>> to trigger the destroyed session
again)* to select another or the same
image again. When the user
return to the uploadform.php page the session
will created again with the value "ok";