gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:32:05 25 4
gpt4 key购买 nike

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

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

消息 -> messageID(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();
?>

最佳答案

试试这个

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

关于php - 将多个文件上传到 FTP 并将文件的 HTTP 链接上传到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com