开发者

How do I edit form and script to upload two files instead of one -file to server, file name to mysql

The form and PHP code included here has been used to successfully upload a file name to the database and the file itse开发者_如何学编程lf to a folder on the server. I need to edit the form and the code to allow for simultaneous upload of two files instead of just one. The filenames will go to the database while the files themselves will go to a folder on the server. In the mysql database fields each of the filenames have to be proceeded with static strings thusly; "images/" and "flash/". The one for images is that way already in the script, I just need to amend the script to accept a second file upload (the files uploaded will be jpeg and flash instead of just jpeg the way it is now. On the form the second file field could carry the name Flash. I have edited the mysql database so it will have an additional field named "flash".

In short, I need this to work exactly ad it does now, only uploading 2 files instead of one. How do i do this?

<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>


first of all the form needs to be changed:

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

second:

you would a: need another field in your database to store the second photo file name

then just use a foreach() loop to run through the $_Files array


If you want to separate the photos from the Flash files, you would change your form like so:

Photo: <input type="file" name="photo"><br />
Flash file: <input type="file" name="flash"><br />

and then add:

$pic=($_FILES['photo']['name']);
$flash=($_FILES['flash']['name']);

And finally add to your INSERT query accordingly. Like sfmoe said, if you want to allow multiple photos and multiple flash files, just add more fields, turn photo and flash into photo[] and flash[] (in the input tags), and use a foreach loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜