【问题标题】:PHP script for uploading multiple images to server用于将多个图像上传到服务器的 PHP 脚本
【发布时间】:2016-05-29 13:18:49
【问题描述】:

我在我的应用程序中使用了以下代码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";
   }
    ?>

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

【问题讨论】:

  • 我建议通过这样做查看发送到 php 的内容。回声'
    '; print_r($_POST);这将允许您查看所有发布数据,然后您可以查看 $_POST['image']
    中是否有图像数组

标签: php android image-uploading


【解决方案1】:

多张图片上传最大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/>';
    }
}
}

【讨论】:

  • 为什么会这样? OP做错了什么?这将暂时解决他们的问题,但不会在未来帮助他们或帮助未来的观众。
  • Llias 我已经尝试了上面的代码,稍作修改,现在它向我发送了图片上传成功但图片没有出现在我的服务器上的响应。我可能做错了什么?
  • Llias 我已经尝试了上面的代码,稍作修改,现在它向我发送了图片上传成功但图片没有出现在我的服务器上的响应。我可能做错了什么?
【解决方案2】:

我想出了以下脚本,它适用于我上面发布的问题。

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



    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多