[英]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.