Mindmajix

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

<?php

ob_start();

print_r(apache_request_headers()); echo ‘<hr>’;

var_dump(headers_sent());

header(“Name:Rajesh”);

header(“location:info.php”);

print_r(headers_list());

echo ‘<hr>’;

print_r(apache_response_headers());

?>

info.php

<?php

echo phpinfo();

?>

 

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:<mine 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:<mode>;filename=<filename>”);

—–>Inline

—–>attachement

Optionally we can sent content length header with the size of the file.

Step 3: header(“Content-Length:<filesize(finename)>”);

Reading the Original file: –

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

Ex: -download.php

<?php

$file=”01.jpg”;

$type=”image/jpg”;

$mode=”inline”;

header(“Content-Type:”.$type);

header(“Content-Disposition:”.$mode.”;filename=”.$file);

header(“Content-Length:”.filesize($file));

readfile($file);

?>

down.php

mage down <a href=”download.php”/>View Please</a>

 

FILE UPLOADING

upload_tmp_dir(<path>) -> default is set to c:\xampp\tmp 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..

Functions in upload: –

copy of source destination :- copy the file from source location to destination folder

 is_upload_file(source path): – checks wheather file upload or not and return boolean..

move_upload_file (<source path, destination>): -moves the file from source location to destination location

Predefined variables: – $_FILES is predefined super global file which will be loaded with the

Properties of uploaded file.

Properties in file upload:-

1. $_FILES[<file_field_name>][‘name’]:-It returns the original name of the uploaded file.

2. $_FILES[<file_field_name>][‘tmp_name’]:-It returns the temporary path to the uploaded file.

(It acts as the source path of the uploaded file in the program).

3. $_FILES[<file_field_name>][‘size’]:-It returns the size of the uploaded file in bytes.

4. $_FILES[<file_field_name>][‘type’]:-It gives the MIME(Multipurpose Internet Main Extension)

type for the uploaded file.

5. $_FILES[<file_field_name>][‘error’]:-It returns the error code associated with the file upload.

If its value is configured to zero indicates no error

Ex :-testupload1.php

<?php

if(isset($_POST[‘submit’])){

if(isset($_FILES[‘photo’])){   $src = $_FILES[‘photo’][‘tmp_name’];

$desc = “pics/”.getRandString(12).$_FILES[‘photo’][‘name’];

if(move_uploaded_file($src,$desc)){ echo ‘Image saved successfully<br>’;}          else {echo ‘Image not saved<br>’;           }

}}

 

echo ‘<hr>’;

function getRandString($size){

$a = str_shuffle(‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890’);

return addslashes(substr($a,0,$size));

}

Ex: -testupload2.php

<?php

if(isset($_POST[‘submit’])){

if(isset($_FILES[‘photo’])){

$src = $_FILES[‘photo’][‘tmp_name’];

$desc = “pic/”.getRandString(40).’ – ‘.$_FILES[‘photo’][‘name’];

if(move_uploaded_file($src,$desc)){echo ‘Image saved successfully<br>’;}          else {   echo ‘Image not saved<br>’; }        }  }

echo ‘<hr>’;

function getRandString($size){

$a = str_shuffle(‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890’);

return crypt(substr($a,0,$size));  }

test.php

<form action=’testupload.php’ method=’POST’ enctype=’multipart/form-data’/>

Upload Photo : <input type=’file’ name=’photo’/><input type=’submit’ name=’submit’ value=’Upload’/>

</form>

0 Responses on Headers, Downloading and File Uploading in PHP"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.