Blog

Headers, Downloading and File Uploading in PHP

HEADERS
Headers are the information which are exchanged between client and server for every request and response.

Headers are of two types.

1. Request Headers.
2. Response Headers.

1) REQUEST HEADERS:-

These are send 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()

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()

HTTP METHODS:-

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

GET, POST, PUT, DELETE, HEAD

Headers can be send from 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 were send to the client or not.

3.header(“<name>:<value>”):-header function should be used in the page before any output is send 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).

header function is used for downloading of the files.

Ex: -hrds.php

DOWNLOADING
Download of a file is done by sending few headers followed with reading of 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 tells the browser to open the file or to ask the user about the file.

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

—–>Inline
—–>attachement

Optionally we can sent 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


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.php


RELATED COURSES

Get Updates on Tech posts, Interview & Certification questions and training schedules