下面的代码逐个上传图片。我想上传多张图片!
<?php
//config
require("./configuration.php");
$id = $_GET['id'];
$q = mysql_query("SELECT * FROM books WHERE id='$id'") or die(mysql_error());
$row = mysql_fetch_array($q);
$name = $row['name'];
echo '<div class="center-top"> NAME: <strong style="font-size:22px;">'.$name.'</strong> </div>';
//begin upload
if(isset($_POST['submit']))
{
$title = htmlspecialchars($_POST['title']);
$path = "./upload/images/";
$name_pic = $_FILES['file']['name'];
$ext = strtolower(substr(strrchr($name_pic, "."), 1));
$allow = array("jpg", "jpeg", "JPG", "JPEG", "png", "gif");
$uptype = ($_FILES['file']['tmp_name']);
if (in_array($ext, $allow))
{
$rand = rand(0,10000);
$md5 = md5($rand);
$new_file_name = "{$md5}.{$ext}";
$move_file = move_uploaded_file($_FILES['file']['tmp_name'], $path.$new_file_name);
if($move_file) {
mysql_query("INSERT INTO images (id, link, title, bid)
VALUES (NULL, '$new_file_name', '$title', '$id')") or die (mysql_error());
echo "<div class='yes'>succesfully added!</div>";
}}}
//form
echo '
<form method="post" action="images.php?id='.$id.'" enctype="multipart/form-data">
<br />title: <br /><input type="text" name="title" value="'.$name.'"/><br />
<br /><br/><input name="file" type="file" />
<br /><input type="submit" name="submit" value="UPLOAD" /><br /><br />
</form>
';
?>
使用此代码删除图像:
<?php
$id = $_GET['id'];
$result = mysql_query("DELETE FROM imagesWHERE `id`='$id'");
echo "<div class='yes'>successfully deleted picture!</div>";
?>
答案 0 :(得分:0)
首先,您需要将<input name="file" type="file"/>
声明为<input name="file[]" type="file" multiple/>
然后,找到所选图像的总数。将其放入for loop
并开始存储在 upload-&gt; images 文件夹中,并将图片路径保存在images
列中。
<?php
//config
require("./configuration.php");
$id = $_GET['id'];
$q = mysql_query("SELECT * FROM books WHERE id='$id'") or die(mysql_error());
$row = mysql_fetch_array($q);
$name = $row['name'];
echo '<div class="center-top"> NAME: <strong style="font-size:22px;">'.$name.'</strong> </div>';
if(isset($_POST['submit']))
{
$title = htmlspecialchars($_POST['title']);
$path = "./upload/images/";
$TotalImage = count($_FILES['file']['name']);
for($i=0;$i>$TotalImage;$i++)
{
$image_name = $_FILES['file']['name'][$i];
$new_file_name = $path.$image_name;
if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$path.$image_name))
{
mysql_query("INSERT INTO images (id, link, title, bid) VALUES (NULL, '$new_file_name', '$title', '$id')") or die (mysql_error());
echo "<div class='yes'>succesfully added!</div>";
}
}
}
//form
echo '
<form method="post" action="images.php?id='.$id.'" enctype="multipart/form-data">
<br />title: <br /><input type="text" name="title" value="'.$name.'"/><br />
<br /><br/><input name="file[]" type="file" multiple/>
<br /><input type="submit" name="submit" value="UPLOAD" /><br /><br />
</form>
';
?>