Website Scripts And Cloud Tutorials

How to upload and download file using PHP

Byadmin

Mar 2, 2009 ,
call php page using ajax

Last updated on March 15th, 2022 at 04:25 am

Click to rate this tutorial!
[Total: 19 Average: 4.4]

The script below can be use to upload a file to the server. You can make of this script to provide your visitors an option to upload to you web application. This script allows any files to be uploaded(Caution: Make sure that you are strictly checking contents uploaded by your visitors since some extenstions can be malicious. )

Modify your max upload file size limit in PHP.INI according to your requirement.

<?php
session_start();
if(!isset($_POST['upload'])) {
echo '<form name="upload" enctype="multipart/form-data" method="POST" action="'.$_SERVER['REQUEST_URI'].'">
<input type="file" name="file" size="13" value=""/>
<br /><input type="submit" name="upload" value="Upload"/>';
} else {
$yourdomain = 'http://localhost/';
$uploaddir = 'uploads/';
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmpname_file = $_FILES['file']['tmp_name'];
    
if($filesize[0] > '1000000') {
echo "Way too big!!";
} else {
move_uploaded_file($tmpname_file, "$uploaddir$filename");
echo "Successful.<br /><b>URL: </b>".$yourdomain.$uploaddir.$filename."";
}
  }
?>

Modify the below variables, filesize is verified in bytes. So watch out for the size of file you allow visitors to upload.

$yourdomain = 'http://localhost/';
$uploaddir = 'uploads/';
$filesize[0] > '1000000'

Once the file is uploaded you can easily get the URL to download from this part of the code.

echo "Successful.<br /><b>URL: </b>".$yourdomain.$uploaddir.$filename."";

You may also be interested in taking a look at Drag and Drop upload script using PHP /HTMl5 (HTML5 Drag And Drop Multiple Files And Upload Using PHP)

Click to rate this tutorial!
[Total: 19 Average: 4.4]
2 thoughts on “How to upload and download file using PHP”

Leave a Reply

Your email address will not be published.