UPLOADING and Downloading A FILE
UPLOADING and Downloading A FILE
In your "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Without the requirements above, the file upload will not work.
Other things to notice:
The type="file" attribute of the <input> tag shows the input field as a file-
select control, with a "Browse" button next to the input control
The form above sends data to a file called "upload.php", which we will create
next.
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]
["name"]);
$uploadOk = 1;
$imageFileType
= strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
First, we will check if the file already exists in the "uploads" folder. If it does, an
error message is displayed, and $uploadOk is set to 0:
Now, we want to check the size of the file. If the file is larger than 500KB, an
error message is displayed, and $uploadOk is set to 0:
<html>
<head>
<title>Download Files</title>
</head>
<body>
<p><a href="abc.txt">Download TEXT file</a></p>
<p><a href="horizon.zip">Download ZIP file</a></p>
<p><a href="lecture.pdf">Download PDF file</a></p>
<p><a href="rose.jpg">Download JPG file</a></p>
</body>
</html>
Output
The following dialog box will appear to download the file after clicking the zip
file link. The user can then download the file or open the file in the archive
manager.
If you click on the image file, the image will be opened automatically in the
browser, as shown in the following output. You must save the file to make a
copy of the image file in the local drive. In the same way, when you click on
PDF and TEXT file links, the content of the file will be opened in the browser
without downloading the file. The solution to this problem is to download the
file forcibly using the built-in PHP readfile() function.
if(isset($_GET['path']))
{
//Read the filename
$filename = $_GET['path'];
//Check the file exists or not
if(file_exists($filename)) {
download3.html
<html>
<head>
<title>Download Files</title>
</head>
<body>
<p><a href="download.php?path=downloads/lecture.pdf">Download PDF file</a></p>
<p><a href="download2.php?path=downloads/rose.jpg">Download JPG file</a></p>
</body>
</html>
We will create a PHP file with the following code to download a file from the
file path. The PHP code in the previous example will be slightly modified to
download the file from the given path. The clearstatecache() function is used
to clear the cache that was previously stored. Two arguments are used in
the readfile() function.
download2.php
<?php
if(isset($_GET['path']))
{
//Read the url
$url = $_GET['path'];
?>
Output
After the download link of the PDF file is clicked, the following output will
appear.