PHP Upload/Download

How to upload file on the server ?
First create html form for file upload on the server.
Code

<form action="" method="post" enctype="multipart/form-data"> Filename:<input type="file" name="userfile" /><br /> <input type="submit" name="submit" value="Submit" /> </form>

Note:- enctype=”multipart/form-data” attribute is necessary.

File upload on server side
When we submit the form. Then $_FILES superglobal variable is created. Suppose we submitting file : “c:\files\sunset.jpeg”. Then following variable is available.

  • $_FILES[‘userfile’][‘name’]:- The name of the file like c:\files\sunset.jpeg.
  • $_FILES[‘userfile’][‘tmp_name’]:- The path of the file in the temporary directory.
  • $_FILES[‘userfile’][‘size’]:- The size of the file in bytes like 30000.
  • $_FILES[‘userfile’][‘type’]:- The type of the file like image/jpeg for jpeg file.
  • $_FILES[‘userfile’][‘error’]:- if file contain any error then this variable generate error code.

PHP Function To Upload file on server.
To upload file on the server use move_uploaded_file() function
Syntax:- bool move_uploaded_file(string sourcefilename,string destinationfilename);
Example 1: To upload file on the server

<?php 
if(isset($_POST['submit'])) 
 { 
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]); 
 }
 ?>
<html>
 <body>
 <form action="" method="post" enctype="multipart/form-data">
 Filename:<input type="file" name="file" id="file" /><br /> 
<input type="submit" name="submit" value="Submit" /> 
 </form> 
 </body> 
</html>

Example 3:- By this example file size less then equal to 5MB.

<?php
    if(isset($_POST['submit']))
      { 
      if ($_FILES["userfile"]["size"] <= 1024*1024*5) move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]); else echo "File size less then equal to 5MB"; }
  ?> 
<html>
  <body>
    <form action="" method="post" enctype="multipart/form-data"> Filename:<input type="file" name="file" id="file" /><br />
 <input type="submit" name="submit" value="Submit" />
 </form>
  </body> 
</html>

How to download data from then server
For download file from server use header() function and the header function must be with Content-type and Content-Disposition.
1- header(“Content-type:filetype”); write Content-type like For jpg file Content-type is image/jpeg For pdf file Content-type is application/pdf etc

2- header(“Content-Disposition:attachment;filename=filename”); write filename with path
Example 4:- PHP script to download pdf

<?php 
 header("Content-type:application/pdf");
 header("Content-Disposition:attachment;filename=COMPANY_PROFILE.pdf");
 ?>

Example 5:- PHP script to download image

<?php
   header("Content-type:image/jpeg"); 
   header("Content-Disposition:attachment;filename=images/img1.jpg");
 ?>
free black porn
6 tips to younger looking skin in the new year
xvideos While its use in modern Myanmar Burma has wittled

15 on 15th at the Wortham
xhamster It told the world

Importance of Monitoring Secondary Sales
porno This is why it is this

Rob Kardashian Foxtrots for Dad
youjizz there may be several people in the fashion editorial staff

Mart swimming pools and accessories on rollback until Sept
porno disassembled towards fashion sense

Flaunt your body with sexy lingerie
jogos de vestir We can classify colors into either being warm or cool

Karrine ‘Superhead’ Steffans publicly separates from husband
jogos da barbie has a significantly higher market share than Spin Master

Stylish Collection of Footwear at ActionEstore
youporn Project Runway Season 2
This entry was posted in   PHP.
Bookmark the   permalink.

Admin has written 171 articles