簡體   English   中英

用於將多個圖像上傳到服務器的PHP腳本

[英]PHP script for uploading multiple images to server

我在應用程序中使用了以下代碼http://mayanklangalia.blogspot.co.ke/2014/04/how-to-upload-multiple-images-on-php.html ,它在android studio端工作正常,但是我在如何編寫php腳本以將這些多個選定的圖像上載到服務器上感到困惑。 我已經知道如何編寫使用此代碼上傳單個圖像的腳本

     <?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";
   }
    ?>

但是我不確定如何為多個圖像編寫腳本。

一個示例文件,用於上傳最多100KB的多張圖片,如果上傳文件的大小超過100KB,則程序將不允許用戶上傳圖片。

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/>';
    }
}
}

我想出了以下腳本,它可以解決我上面發布的問題。

      <?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);



    ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM