一次输入多个图像上载PHP表单
我一直在努力使这项工作已经有相当一段时间了。但我似乎不能让它工作。我想有一个多图像上传表单,只使用一个输入 这是我的upload.php一次输入多个图像上载PHP表单,php,image,file-upload,image-uploading,multiple-file-upload,Php,Image,File Upload,Image Uploading,Multiple File Upload,我一直在努力使这项工作已经有相当一段时间了。但我似乎不能让它工作。我想有一个多图像上传表单,只使用一个输入 这是我的upload.php <?php include("../include/session.php"); session_start(); $allowedExts = array("jpeg", "jpg", "png", "gif"); $extension = end(explode(".", $_FILES["upload"]["name"])); if(isset
<?php
include("../include/session.php");
session_start();
$allowedExts = array("jpeg", "jpg", "png", "gif");
$extension = end(explode(".", $_FILES["upload"]["name"]));
if(isset($_FILES['upload']['tmp_name']))
{
for($i=0; $i < count($_FILES['upload']['tmp_name']);$i++)
{
if (($_FILES["upload"]["name"] < 90000000000000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["upload"]["error"] > 0)
{
header('location: '.$error); die;
}
else
{
if (file_exists("../icons/".$_SESSION["username"] ."/" . $_FILES["upload"]["name"]))
{
echo "error";
}
else
{
if(!is_dir("../icons/". $_SESSION["username"] ."/")) {
mkdir("../icons/". $_SESSION["username"] ."/");
}
$temp = explode(".",$_FILES["upload"]["name"]);
$file = rand(1,999999999999) . '.' .end($temp);
move_uploaded_file($_FILES["upload"]["tmp_name"], "../icons/". $_SESSION["username"] ."/". $file);
}
}
}
} else {
echo "yep error";
}
}
}
?>
如果我把绳子拿出来
if(isset($_FILES['upload']['tmp_name']))
{
for($i=0; $i < count($_FILES['upload']['tmp_name']);$i++)
{
与相应的关闭支架,它似乎工作良好。图片上传得很完美。但问题是,它只允许我上传一个
拜托,我真的需要你的专业知识。多谢各位
extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {
$file_name=$_FILES["files"]["name"][$key];
$file_tmp=$_FILES["files"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(in_array($ext,$extension)) {
if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name)) {
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
}
else {
$filename=basename($file_name,$ext);
$newFileName=$filename.time().".".$ext;
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
}
}
else {
array_push($error,"$file_name, ");
}
}
你必须检查你的HTML代码
<form action="create_photo_gallery.php" method="post" enctype="multipart/form-data">
<table width="100%">
<tr>
<td>Select Photo (one or multiple):</td>
<td><input type="file" name="files[]" multiple/></td>
</tr>
<tr>
<td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
</tr>
</table>
</form>
多个图像上传使用php完整的源代码和预览可在下面的链接。 示例代码:
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代码
Jquery代码
这里$file\u name\u all和$filepath两次获得1个上传文件名?多路径图像与其他表上传 $sql1=插入eventtitle值“$title”
$result1 = mysqli_query($connection,$sql1) or die(mysqli_error($connection));
$lastid= $connection->insert_id;
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK ){
$name = $lastid.$_FILES['file']['name'][$key];
$target_dir = "photo/";
$sql2 = "INSERT INTO photos(image,eventid) VALUES ('".$target_dir.$name."','".$lastid."')";
$result2 = mysqli_query($connection,$sql2) or die(mysqli_error($connection));
move_uploaded_file($_FILES['file']['tmp_name'][$key],$target_dir.$name);
}
}
如何取回
$query = "SELECT * FROM event ";
$result = mysqli_query($connection,$query) or die(mysqli_error());
if($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)){
$eventid= $r['id'];
$sqli="select id,image from photos where eventid='".$eventid."'";
$resulti=mysqli_query($connection,$sqli);
$image_json_array = array();
while($row = mysqli_fetch_assoc($resulti)){
$image_id = $row['id'];
$image_name = $row['image'];
$image_json_array[] = array("id"=>$image_id,"name"=>$image_name);
}
$msg1[] = array ("imagelist" => $image_json_array);
}
在ajax中
$document.readyfunction{$'addCAT'。验证{规则:{name:required:true}提交者:functionform{var formurl=$form.attr'action';$.ajax{url:formurl,type:POST,data:new FormDataform,cache:false,processData:false,contentType:false,success:functiondata{window.location.href={url'admin/listcategory'};};
}
}}注意此链接以及如何将这些图像保存到数据库?@AsifMehmood将图像作为blob文件保存到数据库中确实不是一个好主意,除非您正在处理一个小型且不寻求SEO的项目。@AsifMehmood请看这里:为什么要使用extract$\u POST?我删除了它,代码运行良好。而$\u POST上的摘录似乎是糟糕的编码实践。您可以在php.net manual@PreciousTom中找到“请勿对不受信任的数据使用提取”的警告,如用户输入,即$\u GET、$\u文件等。那么,还有什么办法不将图像保存到数据库?这是问题的答案吗?如果是这样,请明确说明它解决问题的程度。如果这本身就是一个问题,请在此处删除它并询问一个新问题。我只想添加到这个问题中,初始$target_path变量需要位于for循环中,否则通过md5散列的文件名将被连接。此外,我删除了散列文件名方法,因为它不适合我的需要。
$total = count($_FILES['txt_gallery']['name']);
$filename_arr = [];
$filename_arr1 = [];
for( $i=0 ; $i < $total ; $i++ ) {
$tmpFilePath = $_FILES['txt_gallery']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = "../uploaded/" .date('Ymdhis').$i.$_FILES['txt_gallery']['name'][$i];
$newFilePath1 = date('Ymdhis').$i.$_FILES['txt_gallery']['name'][$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
$filename_arr[] = $newFilePath;
$filename_arr1[] = $newFilePath1;
}
}
}
$file_names = implode(',', $filename_arr1);
var_dump($file_names); exit;
$result1 = mysqli_query($connection,$sql1) or die(mysqli_error($connection));
$lastid= $connection->insert_id;
foreach ($_FILES["file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK ){
$name = $lastid.$_FILES['file']['name'][$key];
$target_dir = "photo/";
$sql2 = "INSERT INTO photos(image,eventid) VALUES ('".$target_dir.$name."','".$lastid."')";
$result2 = mysqli_query($connection,$sql2) or die(mysqli_error($connection));
move_uploaded_file($_FILES['file']['tmp_name'][$key],$target_dir.$name);
}
}
$query = "SELECT * FROM event ";
$result = mysqli_query($connection,$query) or die(mysqli_error());
if($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)){
$eventid= $r['id'];
$sqli="select id,image from photos where eventid='".$eventid."'";
$resulti=mysqli_query($connection,$sqli);
$image_json_array = array();
while($row = mysqli_fetch_assoc($resulti)){
$image_id = $row['id'];
$image_name = $row['image'];
$image_json_array[] = array("id"=>$image_id,"name"=>$image_name);
}
$msg1[] = array ("imagelist" => $image_json_array);
}
$total = count($_FILES['txt_gallery']['name']);
$filename_arr = [];
$filename_arr1 = [];
for( $i=0 ; $i < $total ; $i++ ) {
$tmpFilePath = $_FILES['txt_gallery']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = "../uploaded/" .date('Ymdhis').$i.$_FILES['txt_gallery']['name'][$i];
$newFilePath1 = date('Ymdhis').$i.$_FILES['txt_gallery']['name'][$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
$filename_arr[] = $newFilePath;
$filename_arr1[] = $newFilePath1;
}
}
}
$file_names = implode(',', $filename_arr1);
var_dump($file_names); exit;