我是php新手。我想知道如何将单张图片php更改为多张图片。
以下是我的代码:-
<?php
require_once("header.php");
require_once("nav.php");
// Count total files
$countfiles = count($_FILES['files']['name']);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frm_add_channel")) {
if(isset($_FILES['image']['tmp_name']))
{
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES['image']['tmp_name'],"images/album" . $_FILES['image']['name']);
$albumid = $_POST['albumid'];
$name = str_replace("'","''",$name);
$place = $_POST['place'];
$place = str_replace("'","''",$place);
$owner = $_POST['owner'];
$type = $_POST['type'];
$path = $_FILES['image']['name'];
$sql = "INSERT INTO images (name,image,sys_album_id)
VALUES ('$path','$name','$albumid')";
$result = mysql_query($sql);
//$int_id = mysql_num_rows($result);
$int_id = mysql_num_rows();
echo '<script type="text/javascript">alert("Successfully Add New Channel!");location.href="gallery_upload.php";</script>';
}
}
?>
<br><br><br><br><br><br><br>
<?php echo $_GET['id'];?>
<form name="frm_add_channel" method="post" action='' enctype='multipart/form-data'>
<h4>Image (Less Than 2MB)</h4>
<img src="upload/qm.jpg" id="imgAvatar" alt="Course Image" />
<p><input type="file" name="image" id="image" onChange="showPreview(this)" accept="image/*" multiple /></p><br>
<input type="text" class="form" name="albumid" value"<?php echo $_GET['id'];?>" size="30" />
<p><input type="submit" value="Add Album" onclick="return checking()" /></p><br>
<input type="hidden" name="MM_insert" value="frm_add_channel">
</form>
答案 0 :(得分:1)
请尝试在下面更改的html和PHP文件中
$countfiles = count($_FILES['image']['name']);
for($i=0;$i<$countfiles;$i++)
{
$file = $_FILES['image']['tmp_name'][$i];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'][$i]));
$image_name = addslashes($_FILES['image']['name'][$i]);
move_uploaded_file($_FILES['image']['tmp_name'][$i],"images/album" . $_FILES['image']['name'][$i]);
......
....
}
<form name="frm_add_channel" method="post" action='' enctype='multipart/form-data'>
<h4>Image (Less Than 2MB)</h4>
<img src="upload/qm.jpg" id="imgAvatar" alt="Course Image" />
<p><input type="file" name="image[]" id="image" onChange="showPreview(this)" multiple /></p><br>
<input type="text" class="form" name="albumid" value"<?php echo $_GET['id'];?>" size="30" />
<p><input type="submit" value="Add Album" onclick="return checking()" /></p><br>
<input type="hidden" name="MM_insert" value="frm_add_channel">
</form>