【发布时间】:2010-01-13 04:10:10
【问题描述】:
此处包含的表单和 PHP 代码已用于成功地将文件名上传到数据库,并将文件本身上传到服务器上的文件夹。我需要编辑表单和代码以允许同时上传两个文件而不是一个文件。文件名将进入数据库,而文件本身将进入服务器上的文件夹。因此,在 mysql 数据库字段中,每个文件名都必须以静态字符串开头; “图像/”和“闪光灯/”。图片的方式已经在脚本中,我只需要修改脚本以接受第二个文件上传(上传的文件将是 jpeg 和 flash 而不是现在的 jpeg。在表单上第二个文件字段可以带有名称 Flash。我已经编辑了 mysql 数据库,因此它将有一个名为“flash”的附加字段。
简而言之,我需要它与它现在完全一样工作,只上传 2 个文件而不是 1 个。我该怎么做?
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
STK: <input type="text" name = "STK"><br>
<input type="submit" value="Add">
</form>
</body>
</html>
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$ID=$_POST['ID'];
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$STK=$_POST['STK'];
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("employees") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$ID','$name', '$email', '$phone', 'images/$pic','$STK')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
</body>
【问题讨论】: