[英]How to upload/store multiple images in mysql database in php
在這里,我正在制作一個表單,用戶可以在其中上傳多張圖片
下面的 HTML 代碼
<input type='file' name='attachPhoto1' multiple />
下面的 PHP 代碼
$tmp_file = $_FILES['attachPhoto1']['tmp_name'];
$target_file = basename($_FILES['attachPhoto1']['name']);
$upload_dir = "userUploads";
move_uploaded_file($tmp_file,$upload_dir."/".$target_file);
但是當我上傳多張圖片時,只有一張圖片會上傳到userUploads
文件夾中。 所以請幫我解決這個問題。
當您上傳和發布多個圖像文件時,您的輸入應該是這樣的
<input type='file' name='attachPhoto1[]' multiple />
你的表格應該是這樣的
<form action="" method="post" name="" id="" enctype="multipart/form-data">
以下是我使用的非常多的工作代碼,您可以根據需要進行調整;
if(!empty($_FILES['attachPhoto1']['name'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$error_uploads = 0;
$total_uploads = array();
$upload_path = 'upload/';
foreach($_FILES['attachPhoto1']['name'] as $key => $value) {
$temp = explode(".", $_FILES['attachPhoto1']['name'][$key]);
$extension = end($temp);
if ($_FILES["files"]["type"][$key] != "image/gif"
&& $_FILES["files"]["type"][$key] != "image/jpeg"
&& $_FILES["files"]["type"][$key] != "image/jpg"
&& $_FILES["files"]["type"][$key] != "image/pjpeg"
&& $_FILES["files"]["type"][$key] != "image/x-png"
&& $_FILES["files"]["type"][$key] != "image/png"
&& !in_array($extension, $allowedExts)) {
$error_uploads++;
continue;
}
$file_name = time().rand(1,5).rand(6,10).'_'.str_replace(' ', '_', $_FILES["attachPhoto1"]['name'][$key]);
if(move_uploaded_file($_FILES["attachPhoto1"]['tmp_name'][$key], $upload_path.$file_name)) {
$total_uploads[] = $file_name;
} else {
$error_uploads++;
}
}
if(sizeof($total_uploads)) {
//Do what ever you like after file uploads, you can run query here to save it in database or set redirection after success upload
}
}
}
使用<input type='file' name='attachPhoto1[]' multiple />
。 注意方括號(突出顯示它們對我不起作用)。 在 PHP 中,您應該遍歷 $attachPhoto1[] 數組的成員。
請參閱此代碼以在指定文件夾中上傳多個圖像
<?php
if (isset($_POST['submit'])) {
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/"; // Declaring Path for uploaded images.
// --- Loop to get individual element from the array
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
$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.
// --- Approx. 100kb files can be uploaded.
if (($_FILES["file"]["size"][$i] < 100000) && 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/>';
}
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.