图片上传多张最多四张图片 [php]
Image upload multiple maximum of four images only [php]
这是我上传图片的代码。它正在工作,但只有一张图片。我想更改它,以便我的用户最多可以上传 4 张图片。有人可以指导我吗?
if(isset($_FILES['image'])){
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png","gif","");
if(in_array($file_ext,$extensions) === false){
$msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>';
}
if($file_size > 10000000){
$msg = '<div class="error">File size must not be larger than 10MB</div>';
}
if(empty($msg)==true){
$newname= (rand(1, 99999999999).".".$file_ext);
$dir_separator = DIRECTORY_SEPARATOR;
$folder = "uploads";
$uploaddate = date("M-d-Y");
$destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;
if(!is_dir($destination_path)) mkdir($destination_path);
$target_path = $destination_path.$newname;
move_uploaded_file($file_tmp, $target_path);
if(empty($file_name) == false){
$file_location = "/".$uploaddate."/".$newname;
}
else
{
$file_location = "";
}
//SQL Insert code here
echo "Upload complete";
}
}
这是我的表格
<form id="support-form" action="" name="support-form" method="POST" enctype="multipart/form-data">
<div class="subject-message-wrap"> <br/>
<div class="message-text" style="text-align:center;">
<textarea name="message" id="message" rows="5" style="width:98%; max-width: 98%;" placeholder="Write your reply here..." required></textarea>
</div>
</div>
<!---UPLOAD FILE--->
<div class="wrap-upload">
<div>
<label style="color:#009fdb;"> Attach an image: </label>
<input type="file" name="image" id="image" multiple/>
</div>
<br/>
<div class="submit-button-wrap">
<input type="submit" value="Reply" name="submit" id="submit"> <input type="submit" value="Cancel" name="cancel" id="cancel">
</div>
</div>
<!---Code End UPLOAD FILE--->
</form>
编辑 #1:
我的数据库 table 上的问题。我的 table 看起来像这样。
我的想法是为附加图像位置添加新的 3 个字段 (url)。还有比我更好的好主意吗?
以下是您的操作方法:
注意: 如果您想上传多张照片,则需要有多个 file
输入:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="submit" value="Send files" >
</form>
然后您可以在提交按钮上获取所有图像:
$images = $_POST['images'];
for($i=0;$i<count($images);$i++)
{
// Here you need to repeat your upload whole PHP Code for each photo
// to upload all of them
}
参考URL:
你问题的另一部分:
将所有 4 个图像存储在您的数据库中:
解决方案: 你不需要有多个字段来保存这些值。!您可以像这样简单地将图像名称保存在一个字段中:
<?php
$images = array();
$images[0]['images'] = "image1,image2,image3,image4";
$images[1]['images'] = "image1,image2,image3,image4";
$images[2]['images'] = "image1,image2,image3,image4";
//After fetching your data from your database
//You can simply explode the image column data in order to separate each image value
for($i=0;$i<count($images);$i++) {
$image = explode(",",$images[$i]['images']);
echo "Image 1 :".$image[0]."</br>";
echo "Image 2 :".$image[1]."</br>";
echo "Image 3 :".$image[2]."</br>";
echo "Image 4 :".$image[3]."</br></br></br></br></br>";
}
?>
代码输出:
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
更改输入名称:
<input type="file" name="image[]" id="image" multiple />
更改 PHP 代码以处理多个文件上传。这里有很好的例子:
http://php.net/manual/en/features.file-upload.multiple.php
限制客户端 (JavaScript) 和服务器端 (PHP) 的图像数量。
试试这个
if(isset($_FILES['image'])){
$num_files = count($_FILES['image']['tmp_name']); // file count
for($i=0; $i < $num_files;$i++)
{
if($i<4)
{
//here checking only four file
$file_name = $_FILES['image']['name'][$i];
$file_size =$_FILES['image']['size'][$i];
$file_tmp =$_FILES['image']['tmp_name'][$i];
$file_type=$_FILES['image']['type'][$i];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'][$i])));
$extensions= array("jpeg","jpg","png","gif","");
if(in_array($file_ext,$extensions) === false){
$msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>';
}
if($file_size > 10000000){
$msg = '<div class="error">File size must not be larger than 10MB</div>';
}
if(empty($msg)==true){
$newname= (rand(1, 99999999999).".".$file_ext);
$dir_separator = DIRECTORY_SEPARATOR;
$folder = "uploads";
$uploaddate = date("M-d-Y");
$destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;
if(!is_dir($destination_path)) mkdir($destination_path);
$target_path = $destination_path.$newname;
move_uploaded_file($file_tmp, $target_path);
if(empty($file_name) == false){
$file_location = "/".$uploaddate."/".$newname;
}
else
{
$file_location = "";
}
//SQL Insert code here
echo "Upload complete";
}
}
}
}
这是我上传图片的代码。它正在工作,但只有一张图片。我想更改它,以便我的用户最多可以上传 4 张图片。有人可以指导我吗?
if(isset($_FILES['image'])){
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png","gif","");
if(in_array($file_ext,$extensions) === false){
$msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>';
}
if($file_size > 10000000){
$msg = '<div class="error">File size must not be larger than 10MB</div>';
}
if(empty($msg)==true){
$newname= (rand(1, 99999999999).".".$file_ext);
$dir_separator = DIRECTORY_SEPARATOR;
$folder = "uploads";
$uploaddate = date("M-d-Y");
$destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;
if(!is_dir($destination_path)) mkdir($destination_path);
$target_path = $destination_path.$newname;
move_uploaded_file($file_tmp, $target_path);
if(empty($file_name) == false){
$file_location = "/".$uploaddate."/".$newname;
}
else
{
$file_location = "";
}
//SQL Insert code here
echo "Upload complete";
}
}
这是我的表格
<form id="support-form" action="" name="support-form" method="POST" enctype="multipart/form-data">
<div class="subject-message-wrap"> <br/>
<div class="message-text" style="text-align:center;">
<textarea name="message" id="message" rows="5" style="width:98%; max-width: 98%;" placeholder="Write your reply here..." required></textarea>
</div>
</div>
<!---UPLOAD FILE--->
<div class="wrap-upload">
<div>
<label style="color:#009fdb;"> Attach an image: </label>
<input type="file" name="image" id="image" multiple/>
</div>
<br/>
<div class="submit-button-wrap">
<input type="submit" value="Reply" name="submit" id="submit"> <input type="submit" value="Cancel" name="cancel" id="cancel">
</div>
</div>
<!---Code End UPLOAD FILE--->
</form>
编辑 #1: 我的数据库 table 上的问题。我的 table 看起来像这样。
我的想法是为附加图像位置添加新的 3 个字段 (url)。还有比我更好的好主意吗?
以下是您的操作方法:
注意: 如果您想上传多张照片,则需要有多个 file
输入:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="file" name="images[]" id="image" >
<input type="submit" value="Send files" >
</form>
然后您可以在提交按钮上获取所有图像:
$images = $_POST['images'];
for($i=0;$i<count($images);$i++)
{
// Here you need to repeat your upload whole PHP Code for each photo
// to upload all of them
}
参考URL:
你问题的另一部分:
将所有 4 个图像存储在您的数据库中:
解决方案: 你不需要有多个字段来保存这些值。!您可以像这样简单地将图像名称保存在一个字段中:
<?php
$images = array();
$images[0]['images'] = "image1,image2,image3,image4";
$images[1]['images'] = "image1,image2,image3,image4";
$images[2]['images'] = "image1,image2,image3,image4";
//After fetching your data from your database
//You can simply explode the image column data in order to separate each image value
for($i=0;$i<count($images);$i++) {
$image = explode(",",$images[$i]['images']);
echo "Image 1 :".$image[0]."</br>";
echo "Image 2 :".$image[1]."</br>";
echo "Image 3 :".$image[2]."</br>";
echo "Image 4 :".$image[3]."</br></br></br></br></br>";
}
?>
代码输出:
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
Image 1 :image1
Image 2 :image2
Image 3 :image3
Image 4 :image4
更改输入名称:
<input type="file" name="image[]" id="image" multiple />
更改 PHP 代码以处理多个文件上传。这里有很好的例子:
http://php.net/manual/en/features.file-upload.multiple.php
限制客户端 (JavaScript) 和服务器端 (PHP) 的图像数量。
试试这个
if(isset($_FILES['image'])){
$num_files = count($_FILES['image']['tmp_name']); // file count
for($i=0; $i < $num_files;$i++)
{
if($i<4)
{
//here checking only four file
$file_name = $_FILES['image']['name'][$i];
$file_size =$_FILES['image']['size'][$i];
$file_tmp =$_FILES['image']['tmp_name'][$i];
$file_type=$_FILES['image']['type'][$i];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'][$i])));
$extensions= array("jpeg","jpg","png","gif","");
if(in_array($file_ext,$extensions) === false){
$msg = '<div class="error">Extension not allowed, please choose a JPEG or PNG file.</div>';
}
if($file_size > 10000000){
$msg = '<div class="error">File size must not be larger than 10MB</div>';
}
if(empty($msg)==true){
$newname= (rand(1, 99999999999).".".$file_ext);
$dir_separator = DIRECTORY_SEPARATOR;
$folder = "uploads";
$uploaddate = date("M-d-Y");
$destination_path = dirname(__FILE__).$dir_separator.$folder.$dir_separator.$uploaddate.$dir_separator;
if(!is_dir($destination_path)) mkdir($destination_path);
$target_path = $destination_path.$newname;
move_uploaded_file($file_tmp, $target_path);
if(empty($file_name) == false){
$file_location = "/".$uploaddate."/".$newname;
}
else
{
$file_location = "";
}
//SQL Insert code here
echo "Upload complete";
}
}
}
}