Upload/Download
How to upload file on the server ?

First create html form for file upload on the server.

Code

<form action="" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="userfile" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

Note:- enctype="multipart/form-data" attribute is necessary.

File upload on server side

When we submit the form. Then $_FILES superglobal variable is created.

Suppose we submitting file : "c:\files\sunset.jpeg". Then following variable is available.

$_FILES['userfile']['name']:- The name of the file like c:\files\sunset.jpeg.
$_FILES['userfile']['tmp_name']:- The path of the file in the temporary directory.
$_FILES['userfile']['size']:- The size of the file in bytes like 30000.
$_FILES['userfile']['type']:- The type of the file like image/jpeg for jpeg file.
$_FILES['userfile']['error']:- if file contain any error then this variable generate error code.

PHP Function To Upload file on server.

To upload file on the server use move_uploaded_file() function

Syntax:-
bool move_uploaded_file(string sourcefilename,string destinationfilename);

Example 1: To upload file on the server

<?php
if(isset($_POST['submit']))
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
}
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Example 2:-This example is upload only jpeg and gif file.

<?php
if(isset($_POST['submit']))
{
if ( ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") )
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
else
echo "You can upload only gif and jpeg file";
}
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Example 3:- By this example file size less then equal to 5MB.

<?php
if(isset($_POST['submit']))
{
if ($_FILES["userfile"]["size"] <= 1024*1024*5)
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
else
echo "File size less then equal to 5MB";
}
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

How to download data from then server

For download file from server use header() function and the header function must be with Content-type and Content-Disposition.

1- header("Content-type:filetype");
write Content-type like
For jpg file Content-type is image/jpeg
For pdf file Content-type is application/pdf
etc

2- header("Content-Disposition:attachment;filename=filename");
write filename with path

Example 4:- PHP script to download pdf

<?php
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=COMPANY_PROFILE.pdf");
?>

Example 5:- PHP script to download image

<?php
header("Content-type:image/jpeg");
header("Content-Disposition:attachment;filename=images/img1.jpg");
?>


Previous
Next