我问如何将多个文件(使用jquery.Multifile)上传到FTP并将文件链接到MySQL数据库?我已经有了一个HTML表单,可以使用PHP将多个文件上传到我的FTP。
假设我有两个表(我在这些表中也有其他字段但是将它们添加到我的问题中无关紧要):
讯息 - > messageID(pk),标题,日期,消息
文件 - > fileID(pk),messageID(fk),fileLink
当用户编写消息时,他可以选择许多文件作为jquery.Multifile的附件。然后,当用户发送消息时,文件被发送到FTP,并且文件的http链接被发送到数据库。每个文件都应该有自己的ID(fileID),这两个表使用字段messageID作为消息表中的主键和文件表中的外键链接在一起。
这就是我现在所拥有的,但我不知道如何处理数据库部分:
HTML表单:
<form name="uploader" action="file_upload2.php" method="post" enctype="multipart/form-data">
<fieldset>
<label for="file">Upload file:</label>
<br />
<input type="file" class="multi, remove" name="file[]" value="Upload file" />
<input type="submit" value="submit"/>
</fieldset>
这是PHP部分($ filepath是我试图在FTP中建立文件的链接):
<?php
include('db.php');
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
mysql_query("SET names 'UTF8'");
$heading=$_POST['heading'];
$date=$_POST['date'];
$message=$_POST['message'];
$conn = ftp_connect("mysite.org") or die("Cannot connect");
ftp_login($conn,"username","password");
$uploaddir = 'httpdocs/folder1/folder2/files/';
foreach ($_FILES["file"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $_FILES["file"]["tmp_name"][$key];
$name = $_FILES["file"]["name"][$key];
$uploadfile = $uploaddir . basename($name);
ftp_put($conn,$uploadfile,$tmp_name,FTP_BINARY);
if (move_uploaded_file($tmp_name, $name))
{
echo "Success: File ".$name." uploaded to server.<br/>";
}
else
{
echo "Error: File ".$name." can not be uploaded to server.<br/>";
}
}
}
ftp_close($conn);
$filepath = "http://mysite.org/folder1/folder2/files/".$name;
$sql="INSERT INTO message(
heading,
date,
message)
VALUES
(
'$heading',
'$date',
'$message')";
$result=mysql_query($sql);
if($result){
header("Location: ../index.php");
}
else {
echo $sql . ("Adding message failed <br />" . mysql_error());
}
mysql_close();
?>
答案 0 :(得分:0)
试试这个
<?php
include('db.php');
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
mysql_query("SET names 'UTF8'");
$heading=$_POST['heading'];
$date=$_POST['date'];
$message=$_POST['message'];
$conn = ftp_connect("mysite.org") or die("Cannot connect");
ftp_login($conn,"username","password");
$uploaddir = 'httpdocs/folder1/folder2/files/';
$sql="INSERT INTO message(messageid,heading, date, message) VALUES('Null' , '$heading', '$date', '$message')";
$result=mysql_query($sql);
$lastinsertid = mysql_insert_id($sql);
foreach ($_FILES["file"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$tmp_name = $_FILES["file"]["tmp_name"][$key];
$name = $_FILES["file"]["name"][$key];
$uploadfile = $uploaddir . basename($name);
ftp_put($conn,$uploadfile,$tmp_name,FTP_BINARY);
if (move_uploaded_file($tmp_name, $name))
{
echo "Success: File ".$name." uploaded to server.<br/>";
$filepath = "http://mysite.org/folder1/folder2/files/".$name;
$sql="INSERT INTO File (fileid, messageid, link) VALUES( Null, '$lastinsertid', '$filepath')";
$result=mysql_query($sql);
}
else
{
echo "Error: File ".$name." can not be uploaded to server.<br/>";
}
}
}
ftp_close($conn);
if($result){
header("Location: ../index.php");
}
else {
echo $sql . ("Adding message failed <br />" . mysql_error());
}
mysql_close();
?>