How to Upload Single and Multiple File with Php




For single file upload


Step1- create a index.php file and upload folder

<?php
if(isset($_POST['submit'])){
echo "clicked";



$name= $_FILES['myfile']['name'];// FIRST STEP IS GIVE NAME OF UPLOAD FILE

$tmp_name=$_FILES['myfile']['tmp_name'];//SECOND STEP IS GIVE TEMPORARY NAME OF  UPLOAD FILE
$store="upload/".$name;//THIRD STEP IS CREATE A STORE LOCATION OF FILE





move_uploaded_file($tmp_name,$store);//FOTH STEP UPLOAD FILE

?>
<img src="<?php echo $store; ?>" width="100px" height="100px" />

<?php
}
?>


<html>
<head>




</head>




<body>


<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/>

<input type="submit" value="upload" name="submit"/>
</form>
</body>
</html>



For upload multiple files 
Step1-create  index2.php file and upload folder

<form action="" method="post"  enctype="multipart/form-data">

<input type="file" name="myfile[]" multiple/>

<input type="submit" name="submit" />

</form>






<?php

if(isset($_POST['submit'])){

echo "success";

$countfiles=count($_FILES['myfile']['name']);//count the files

// looping files for multiple upload

for($i=0;$i<$countfiles;$i++){
$name=$_FILES['myfile']['name'][$i];  // name of the upload file

$tmp_name=$_FILES['myfile']['tmp_name'][$i]; // tempoary name of upload file

$location="upload/".$name;// location where we upload files


move_uploaded_file($tmp_name, $location);
?>

<img src="<?php echo $location;  ?>" height="100px" width="100px">

<?php

}
}









?>

Previous
Next Post »
Thanks for your comment