dtrj74376 2012-04-09 01:25
浏览 95

将多个文件上传到FTP和HTTP链接到MySQL数据库的文件

I am asking that how is it possible to upload multiple files (using jquery.Multifile) to FTP and links of the files to MySQL database? I already have an HTML form which can upload multiple files to my FTP using PHP.

Assuming I have two tables (I have other fields also in these tables but it is not relevant to add them to my question):

Message -> messageID(pk), heading, date, message

File -> fileID(pk), messageID(fk), fileLink

When user writes a message he can choose many files as attachment with jquery.Multifile. Then when user sends the message, the files are sent to FTP and http links to the files are sent to database. Each file should have their own ID (fileID) and these two tables are linked together using field messageID as a primary key in message table and foreign key in file table.

This is what I have now, but I don't know how to do the database part:

HTML form:

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

And this is the PHP part ($filepath is something I tried to do to make a link to the file in 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();
?>
  • 写回答

1条回答 默认 最新

  • dongle2627 2012-06-04 06:08
    关注

    try this

    <?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();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示