Friday, 7 June 2013

how to upload many images at once in php

This tutorial tell you how to upload many images at once  in php.

 Just copy the code bellow and save it as "image.php" then open it in your server e.g. localhost.

<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 $uploaded_images = count($_FILES['file']['name']);

 for ($i = 0; $i < $uploaded_images; $i++)
 {
 if($_FILES["file"]["error"][$i] > 0)
 {
  // if there is error in file uploading
   echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br />";
 }
 else
 {

   // check if file already exit in "images" folder.
   if (file_exists("images/" . $_FILES["file"]["name"][$i]))
   {
    echo $_FILES["file"]["name"][$i] . " already exists. "."<br />";
   }
   else
   { 
    //move_uploaded_file function will upload your image.  if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
    if(move_uploaded_file($_FILES["file"]["tmp_name"][$i], "images/" . $_FILES["file"]["name"][$i]))
    {
     //If you want to stor its name in data base code will go here.
       //file has uploaded successfully, If you want to stor its name in data base. see this link http://b2atutorials.blogspot.com/2012/05/upload-image-into-folder-and-save-its.html
 
     echo $_FILES["file"]["name"][$i] . ' -image uploaded.' .'<br />';
    }
   }
 }
 }
 }
 ?>
<html>
<body>
    <form method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" multiple/>
    <input type="submit" value="submit">
    </form>
</body>
</html>
    

    

0 comments:

Post a Comment