Hurry! 20% Off Ends SoonRegister Now
  • Home
  • Blog
  • PHP
  • Headers, Downloading and File Uploading in PHP

Headers, Downloading and File Uploading in PHP

Headers are the information that is exchanged between client and server for every request and response.

HEADERS in PHP

Headers are of two types.

1. Request Headers.
2. Response Headers.

1) REQUEST HEADERS:-

These are sent from client to server.

HTTP Request         : GET/php7/info.php HTTP/1.1
Host: localhost
User_Agent              : Mozilla/5.0
Accept                     : text/HTML, application/html
Accept_encoding     : gzip,deflate
Connection              : keep-alive
keep-alive                : 115
Accept_char             : ISO-88591-1 utf-8;q=0.7

Ex:-apache_request_headers()

 MindMajix YouTube Channel

2) RESPONSE HEADERS:-

These are send from server to client.

X-powered-by              : php/5.3.1
keep_Alive                    : timeout=5,max=100
connection                   : Keep_Alive
Transfer_Encoding        : chunted
content_type                : text/html

Ex:-apache_response_headers()

[Related Article: PHP Interview Questions for Freshers]

HTTP METHODS:-

Every request from the client to the server can be made through various HTTP methods like

GET, POST, PUT, DELETE, HEAD

Headers can be sent from the PHP program to the server.

1. headers_list():- Returns an array to the list of the headers which can be shared from server to client.

2. headers_sent ():-Returns the boolean value above whether the headers sent to the client or not.

3. header(“:”):-header function should be used on the page before any output is sent to the page.

header functions are used for redirection. header function is used for sending the output of the

PHP content in different formats(content-type like HTML,XHTML,images,pdf,flash,video etc).

The header function is used for downloading the files.

Ex: -hrds.php

hrds.php

[Related Article: Cookies and Session in PHP]

Uploading and Downloading Files in PHP 

DOWNLOADING

The download of a file is done by sending few headers followed by a reading of the source file from the server sending a content-type header

header(“Content-Type:”);
pdf -> application/pdf.
html -> text/html.
jpg/png/gif -> image/(jpg || png || gif).

Step 2: Sending a content Disposition header which will tell the browser to open the file or to ask the user about the file.

header(“Content-Disposition:;filename=”);

—–>Inline
—–>attachement

Optionally we can send a content-length header with the size of the file.

Step 3:

header(“Content-Length:”);

Reading the Original file: –

readfile()– -> Reads the file Content & prints to the browser..

Ex: -download.php

readfile()

FILE UPLOADING

upload_tmp_dir() -> default is set to c:xampptmp The temporary location to which the file can be uploaded to the server

upload_max_filesize : (<128>) The max size for a single file which can be uploaded to the server -> max_file_uploads : 20..

Ex:-testupload1.phpFILE UPLOADING2testupload1.php

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read less