Fileupload in php

 File uploading in PHP

 

e-trainings-file-upload-in-PHP-1

 

Create the file with code given below and run it on the server. Choose any image or text or PDF file from the folder. And click upload. When the file is uploaded, all the details(like name, type, size, tmp_name etc.) of the file are stored in a super global array variable called $_FILE and the actual file is stored in servers tmp folder in a raw format.  Next the function move_uploaded_file() gets called, it moves raw file contents to its filename and saves it in a given path. Once this task is finished. It displays a "File upload Success" message.

Then the file is uploaded to 'uploads' folder. The given example code will also check for the folder 'uploads' is created,  if the folder is not created, it creates and uploads the file into that. 

Note: Make sure Form tag must have the attribute enctype:

Ex:  enctype="multipart/form-data"
 

The above attribute encrypted the file while  transmitting the file from 
client to server.

file-upload.php


<html>
<head>
 	
<title>
 e-Trainings Demo on PHP-Fileupload
</title>
 
</head>
<body>

<h1> FileUpload Form </h1>
 


<?php
if(isset($_POST["submit"]))

{
$filename=$_FILES['file']['name'];
$filetype=$_FILES['file']['type'];
$filesize=$_FILES['file']['size'];
$filetmp=$_FILES['file']['tmp_name'];

 

echo "Uploaded File : ".$filename . "<br>";
 
 

if (!is_dir("uploads")) {
mkdir("users ");
}
 

move_uploaded_file($filetmp, "uploads/".$filename);
 

echo "<br>File Upload Success"; 
}

?> 

 

<form name="f1" method="post" action="" enctype="multipart/form-data"> 
 
Choose File:
 

<input type="file" name="file" id="file" ><br> 
  
<input type="submit" name="submit" value="Upload">

</form>
 
</body>

</html>

 Result:

 

coding-zon-file-upload

 

 

 

 Next Using Sessions in PHP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP