File Uploading in PHP





File uploading is easy and well supported by PHP.

There are certain built in functions used to upload files.

$_FILES is an Associative array created by PHP that stores file name, type, size etc


Settings to be done in php.ini configuration file.

Open php.ini file in notepad editor.

1) search for post_max_size and make it as follows
post_max_size = 10M

Maximum size of POST data that PHP will accept.
i.e At the max 10Mb data can be sent from Client to Server via post method.

2) search for file_uploads and make it as follows

file_uploads = On

    It determines if HTTP file uploads allowed or not.

3)search for upload_tmp_dir and make it as follows

upload_tmp_dir = "c:/wamp/tmp"  

     Its Temporary storage for uploaded files.



 /* $loc is the location to move uploaded file "  ./images/  " means images folder is present in current directory where upload.php file is kept*/

//$loc='./images/'.basename($_FILES['f1']['name']);


selectFile.html

<html>
 <body><form action="upload.php" method="post" enctype="multipart/form-data">
Select File to Upload <input type=file name="f1" /><input type=submit value="Upload" />
 </form>
</body>
</html>


upload.php
<?php

try
  {
   $loc='./images/'.$_FILES['f1']['name'];  
   $res=move_uploaded_file($_FILES['f1']['tmp_name'],$loc);
    if($res)
      echo $_FILES['f1']['name'].' File Uploaded successfuly';
    else
      echo $_FILES['f1']['name'].' File Not Uploaded. Try again';
  }
catch(Exception $e)
 {
  echo 'OOPS!'.$e->getMessage();
 }

?>


1 comment:

  1. sir need help how can file uplode and downlode option max file side 5 mb
    s_jid@yahoo.co.in

    ReplyDelete

Thanks for visiting my Blog!

Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog