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