![](/img/trans.png)
[英]I want to upload some picture files to my server and record the file name in mysql database but somehow there exists a problem
[英]How do I edit form and script to upload two files instead of one -file to server, file name to mysql
此处包含的表单和PHP代码已用于成功将文件名上载到数据库,并将文件本身上载到服务器上的文件夹。 我需要编辑表单和代码,以允许同时上传两个文件,而不只是一个文件。 文件名将转到数据库,而文件本身将转到服务器上的文件夹。 因此,在mysql数据库字段中,每个文件名都必须以静态字符串开头。 “图像/”和“闪光灯/”。 用于图像的是脚本中已经存在的方式,我只需要修改脚本以接受第二个文件上传(上传的文件将是jpeg和flash而不是现在的jpeg。在第二个文件的形式上字段可以命名为Flash。我已经编辑了mysql数据库,因此它将有一个名为“ flash”的附加字段。
简而言之,我需要它能够像现在一样准确地工作,仅上传2个文件,而不是一个。 我该怎么做呢?
<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>
首先需要更改表格:
Photo 1: <input type="file" name="photo[]"><br>
Photo 2: <input type="file" name="photo[]"><br>
第二:
您将:在数据库中需要另一个字段来存储第二张照片文件名
然后只需使用一个foreach()
循环来运行$_Files
数组
如果要将照片与Flash文件分开,则可以这样更改表格:
Photo: <input type="file" name="photo"><br />
Flash file: <input type="file" name="flash"><br />
然后添加:
$pic=($_FILES['photo']['name']);
$flash=($_FILES['flash']['name']);
最后,相应地添加到您的INSERT查询中。 就像sfmoe所说,如果要允许多张照片和多个Flash文件,只需添加更多字段,将photo
和flash
变成photo[]
和flash[]
(在输入标签中),然后使用foreach循环即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.