用于将多个图像上载到服务器的PHP脚本

时间:2016-05-29 13:18:50

标签: php android image-uploading

我在我的应用程序中使用了以下代码http://mayanklangalia.blogspot.co.ke/2014/04/how-to-upload-multiple-images-on-php.html,它在android工作室方面运行良好,但我却陷入了如何编写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";
   }
    ?>

但我不确定如何编写多个图像的脚本。

2 个答案:

答案 0 :(得分:0)

多张图片上传的示例脚本,最高可达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/>';
    }
}
}

答案 1 :(得分:0)

我提出了以下脚本,它对我上面发布的问题有用。

data