1

我在问如何将多个文件(使用 jquery.Multifile)上传到 FTP 并将文件链接到 MySQL 数据库?我已经有一个 HTML 表单,可以使用 PHP 将多个文件上传到我的 FTP。

假设我有两个表(这些表中还有其他字段,但将它们添加到我的问题中并不相关):

消息 -> 消息 ID(pk)、标题、日期、消息

文件 -> 文件 ID(pk)、消息 ID(fk)、文件链接

当用户写消息时,他可以选择许多文件作为 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();
?>
4

1 回答 1

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();
?>
于 2012-06-04T06:08:35.743 回答