In general, no PHP script is needed to upload a file with exe and zip extensions. If the location of this file type is specified in the root attribute of the anchor element, the file is downloaded automatically when the user clicks on the download link. Some files, such as image files, PDF files, text files, CSV files, etc. are not downloaded automatically, but open in your browser when you click the download link. These files can be forcibly uploaded into PHP using the Readfile() function, which does not load automatically. In this lesson you will learn how to use a PHP script to force the downloading of any file.

Check download left

Earlier it was mentioned that zip and exe files are loaded automatically without using a PHP script. First create an HTML file with the following code. Here four anchor elements are defined to load four file types. These file types include TEXT, ZIP, PDF and JPG formats.

Download.html

 

Download the TEXT file

 

Download ZIP file

 

Download PDF file

 

Download JPG file

Output
After clicking on the link of the zip file, the following dialog box for downloading the file will appear, after which the user can download a file or open it in the archive manager.

When you click on an image file, the image is automatically opened in your browser, as shown in the following number. You need to save the file to make a copy of the image file on your local hard drive. When you click on the links to the PDF and TEXT files, the contents of the file will open in your browser without you having to download it. The solution to this problem is to force the file to load using PHP’s built-in readfile() function.

Loads the file with the Readfile() function

The readfile() function is used in PHP scripts to force downloading a file from the current location or a file with a file path. The syntax of this function is given below.

Syntax
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]]).

This function can accept three arguments. The first argument is mandatory, the other two are optional. The first argument, $filename, saves the filename with the path to load. The second default value of $use_include_path is false and is set to true if the first argument uses the filename of the path. The third argument, $context, is used to specify the contextual source. This function returns the number of bytes read of the file specified in the first argument. The use of this function is illustrated in the following two examples.

Example 1: Download the file with the filename

In this example we create an HTML file with the following code, passing the filename as a URL parameter with the pathname to a PHP file named download.php.

download2.html

html>
head>
title> file/title upload>
/head>
body>
p>a href=download.php?path=abc.txt> download TEXT file/a>/p>
p>a href=download.php?path=horizon.zip>download ZIP file/a>/p>
p>a href=download.php?path=referat.pdf>download PDF file/a>/p>
p>a href=download.php?path=rose.jpg>download JPG file/a>/p>
/body>
/html>.

We create a PHP file with the following code for forced loading Here the isset() function is used to check if $_GET[‘path’] is defined. When the variable is set, the function file_exists() is used to check the existence of the file on the server. In addition, the header() function is used to define the required header information before the readfile() function is used. The basic name() function is used to obtain the file name, while the file size() function reads the file size in bytes, which is displayed in the opening dialog for loading the file. The coil() function is used to flush the output buffer. The function readfile() is only used here with the filename.

download.php

{
// Read file name
$B9 File name = $_GET[‘path’];
// Check if file
exists or not (file_exist($filename)). {

//Define header information
-Chapter (Content description: File transfer);
-Chapter (Content type: Application/Octet stream);
-Chapter (Cache Control: no cache, needs to be validated again);
-Chapter (Expires: 0);
-Chapter (Content description: Attachment; File name=).base name ($filename)’) ;
header (‘Content length : ‘ . file size($filename)) ;
-Chapter (‘Pragma : public’) ;

// Erase system output buffer
coils() ;

// Read file size
readfile ($filename) ;

// Clarification of Script();}}{ the echo file does not exist;}, moreover, the name of the echo file is not defined. ?

Output
After clicking the link to download the image file, the following output appears. The size of the image file rose.jpg is 27.2 KB, as shown in the dialog box. You can download a file by selecting the Save file option and pressing OK.

Example 2: Download the file via file path

If the file exists there, the path to the file must be indicated in the URL. In this example, we create an HTML file with the following code, which contains the filename with the path to the :

download3.html

 

Download PDF file

 

Download JPG file

We create a PHP file with the following code to upload the file in the file path The PHP code in the previous example has been slightly modified to upload the file via the specified path. The clearstatecache() function is used to clear the previously saved cache. In the readfile() function, two arguments are used.

download2.php

if(isset($_GET[‘path’])).

// Read url
$url = $_GET [path] ;

// Clear cache
clearstatcache() ;

// Check if the path to the file exists or not
if(file_exists($url)) {

//
-Chapter (‘DeepL Description: File Transfer’);
-Chapter (‘Content Type: Application/Octetstream’);
-Chapter (‘Content Type: Attachment; File Name=’.base name($url)’);
-Chapter (‘Content Length: ‘ . fileize($url));
-Chapter (‘Pragma: public’) ;

// Erase system output buffer
coils() ;

// Read file size
readfile ($url,true) ;

// Script clearing
de();
}
else{
echopad to file does not exist;
}
}
echopad to file is not defined.

?>

Output
After clicking on the PDF download link, the following output appears.

Conclusion

This article provides an easy way to force the downloading of any file using a PHP script to help players add download options to their scripts.

1.Generally linux has drivers for everything known to man, but becuase of proprietary interference or hardware is not used anymore, you may still be able to …2.Message of the day if the screen you see when you log into the command line. Usually if s short and mundane, You may want to jazz it up a little bit with …3.After compiling a program that is a replacementment/upgrade version to existing software you might get a library conflict. That happened to me . Had a …,1.Generally linux has drivers for everything known to man, but becuase of proprietary interference or hardware is not used anymore, you may still be able to …,2.Message of the day if the screen you see when you log into the command line. Usually if s short and mundane, You may want to jazz it up a little bit with …,3.After compiling a program that is a replacementment/upgrade version to existing software you might get a library conflict. That happened to me . Had a …,linux commands cheat sheet 2020 pdf,cheatography linux,kali linux cheat sheet pdf,linux cheat sheet github,linux cheat sheet wallpaper,linux administration commands cheat sheet pdf

You May Also Like

What is the difference between an IntentService and a Service? –

Вопрос или проблема в разделе Развитие: Не могли бы Вы помочь мне…

GeForce RTX 3000: official announcement of new generation Nvidia graphics cards –

After months of speculation and rumours, Nvidia officially launched the first graphics…

Vue.js Components – Linux Hint

Vue.js is a progressive Javascript framework used to create user interfaces (UI)…

How to Install OpenLiteSpeed Web Server on CentOS 8/RHEL 8

When it comes to open source web servers, Apache and Nginx usually…