【问题标题】:How do I edit form and script to upload two files instead of one -file to server, file name to mysql如何编辑表单和脚本以将两个文件而不是一个文件上传到服务器,将文件名上传到 mysql
【发布时间】: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>

【问题讨论】:

    标签: php mysql html


    【解决方案1】:

    首先需要更改表单:

    Photo 1: <input type="file" name="photo[]"><br>
    Photo 2: <input type="file" name="photo[]"><br>
    

    秒:

    您会:需要在数据库中使用另一个字段来存储第二张照片文件名

    然后只需使用foreach() 循环遍历$_Files 数组

    【讨论】:

      【解决方案2】:

      如果您想将照片与 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文件,只需添加更多字段,将photoflash变成photo[]flash[](在输入标签中) ,并使用foreach 循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 2011-08-17
        • 2016-08-26
        • 2011-11-15
        • 1970-01-01
        • 2017-04-25
        • 2012-10-27
        相关资源
        最近更新 更多