0% found this document useful (0 votes)
315 views

Upload and Download Using PHP

1) PHP can be used to upload and download files to and from a server. Files can be uploaded via an HTML form and then processed by a PHP script to validate and move the file to a target location. 2) The PHP readfile() function allows easy downloading of files by reading the file and writing it to the output buffer. 3) A complete PHP file upload script would validate the file type, size, name, and move the file to the target location if valid. For downloading, readfile() is used to directly output the file.

Uploaded by

asia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
315 views

Upload and Download Using PHP

1) PHP can be used to upload and download files to and from a server. Files can be uploaded via an HTML form and then processed by a PHP script to validate and move the file to a target location. 2) The PHP readfile() function allows easy downloading of files by reading the file and writing it to the output buffer. 3) A complete PHP file upload script would validate the file type, size, name, and move the file to the target location if valid. For downloading, readfile() is used to directly output the file.

Uploaded by

asia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Topic:

Upload and Download Files Using


Php

H a m z a S h a r i f Aw a n
18 ARID 5148
OUTLINE

• Upload File Using PHP

• Upload File Using PHP


Introduction:
• A PHP script can be used with a HTML form to
allow users to upload files to the server. Initially
files are uploaded into a temporary directory and
then relocated to a target destination by a PHP
script.

• PHP enables you to download file easily using


built-in readfile() function. The readfile()
function reads a file and writes it to the output
buffer.
PHP File upload :
With PHP, it is easy to upload files to the
server.
 Configure The "php.ini" File
 First, ensure that PHP is
configured to allow file uploads.
 In your "php.ini" file, search for
the file_uploads directive, and
set it to On:
Create an HTML Form
Create an HTML form that allow users to choose the image file they want to
upload:
CODE
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
Some rules to follow for the HTML form above:
• Make sure that the form uses method="post"
The form also needs the following attribute:
enctype="multipart/form-data". It specifies which content-
type to use when submitting the form
• 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.
CREATE THE UPLOAD FILE PHP SCRIPT
THE "UPLOAD.PHP" FILE CONTAINS THE CODE FOR UPLOADING A FILE:

CODE:
<?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;
  }
}
?>
PHP SCRIPT EXPLAINED:

• $target_dir = "uploads/" - specifies the directory


where the file is going to be placed

• $target_file specifies the path of the file to be


uploaded

• $uploadOk=1 is not used yet (will be used later)

• $imageFileType holds the file extension of the file (in


lower case)
CHECK IF FILE ALREADY EXISTS
• Now we can add some restrictions.

• 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:
CODE

// Check if file already exists


if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
LIMIT FILE SIZE
• The file input field in our HTML form above is named
"fileToUpload".

• 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:
CODE
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
Complete Upload File PHP Script
CODE
<?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;
}
CONT…
// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size


if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats


if($imageFileType != "jpg" && $imageFileType != "png" &&
$imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
} CONT…
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). "
has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
PHP File Download:
PHP enables you to download file easily
using built-in readfile() function. The
readfile() function reads a file and writes
it to the output buffer.
PHP READFILE() FUNCTION
Syntax:
int readfile ( string $filename [, bool
$use_include_path = false [, resource $context ]] )
$filename: represents the file name

$use_include_path:
it is the optional parameter. It is by default false. You
can set it to true to the search the file in the
included_path.
$context: represents the context stream resource.

int: it returns the number of bytes read from the file.


COMPLETE DOWNLOAD FILE PHP SCRIPT
//html view to download file without using Php
//First view then download
<!DOCTYPE html>
<html>
<head>
<title>Download PDF using PHP from HTML Link</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<center>
<h1 >Welcome to this website </h1>
<p><b>Click below to download PDF </b> </p>
<p1><br> The pdf will be open in new tab you can Read and download form there </br> </p1>
<a href="assignment.pdf" target=" ">Download PDF Now</a>
</center>
</body>
</html>
//html view to download file with using Php
//direct or instant download
CODE

<!DOCTYPE html>
<html>
<head>
<title>Download PDF using PHP from HTML
Link</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<center>
<h1 >Welcome to this website </h1>
<p><b>Click below to download PDF </b> </p>
<p1><br> The pdf will imideatly download onclick
</br> </p1>
<a href="newpath.php?file=assignment" target="
">Download PDF Now</a>
</center>
</body>
</html>
CODE:

//Php code
<?php
$file = $_GET['file'] .".pdf";
header("content-disposition: attachment;
filename=". urlencode($file));
$fb = fopen($file, "r"); while(!feof($fb)){ echo
fread($fb, 8295);
flush();
}
fclose($fb);
?>
XAMPP PATH

 C:\xampp\php
CONFIGURATION SETTING

 Just open php.ini.


 Use shortcut key CTRL+F and find “uploads”.
 Make sure file_uploads=On
 Open xampp control panel.
 Start Apache and MySQL.

XAMPP CONTROL PANEL


C:\xampp\htdocs
1. Index.php
2. Upload.php
3. Create Folder “Uploads”

CREATING FILES
INDEX.PHP
UPLOADS.PHP
UPLOAD FILE
UPLOADING FILE
DOWNLOAD CODE
FILE DOWNLOADED

You might also like