简体   繁体   中英

PHP script for uploading multiple images to server

I have used the following code http://mayanklangalia.blogspot.co.ke/2014/04/how-to-upload-multiple-images-on-php.html in my application and it is working fine on the android studio side, i am however stranded on how to write the php script to upload these multiple selected images to the server. I already know how to write the script for uploading a single image using this code

     <?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {



 $image = $_POST['image'];
 require_once('DB_Connect.php');







 $sql ="SELECT id FROM images ORDER BY id ASC";
 $now = DateTime::createFromFormat('U.u', microtime(true));
 $id = $now->format('ymdhisu');
  $path = "Upload/$id.jpeg";
 $actualpath = "http://myurl/$path";
 $sql = "INSERT INTO volleyupload (photo) VALUES ('$actualpath')";




   if( file_put_contents($path,base64_decode($image))!=false){
    echo "Successfully Uploaded";
    exit;
}  
    } else {
echo "Error";
   }
    ?>

but i am not sure how to go about writing the script for multiple images.

An example script for multiple image upload up to 100KB, if the upload file size exceeds 100KB, then program won't allow user to upload the image.

if (isset($_POST['submit'])) {
$j = 0; //Variable for indexing uploaded image 

$target_path = "uploads/"; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) { //loop to get individual element from the array

    $validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed
    $ext = explode('.', basename($_FILES['file']['name'][$i])); //explode file name from dot(.) 
    $file_extension = end($ext); //store extensions in the variable

    $target_path = $target_path.md5(uniqid()).
    ".".$ext[count($ext) - 1]; //set the target path with a new name of image
    $j = $j + 1; //increment the number of uploaded images according to the files in array       

    if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
        && in_array($file_extension, $validextensions)) {
        if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { //if file moved to uploads folder
            echo $j.
            ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
        } else { //if file was not moved.
            echo $j.
            ').<span id="error">please try again!.</span><br/><br/>';
        }
    } else { //if file size and file type was incorrect.
        echo $j.
        ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
    }
}
}

I came up with the following script and it worked in regards to the question i posted above.

      <?php

      require_once('DB_Connect.php');


      $name = $_POST["name"];
      $image = $_POST["IMAGE"];
      $decodedImage = base64_decode("$image");


      $actualpath = "http://aerialssnip/public_html/$path";
      $sql = "INSERT INTO volleyupload (photo) VALUES ('$actualpath')";

      file_put_contents($path,$decodeImage);



    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM