如何从 FTP 远程服务器上的数据库上传文件?
How to upload files from database on FTP remote server?
我使用 PHP 将图像从主机上传到 FTP 远程服务器。
我已经把需要上传到FTP远程服务器的文件名存储在数据库中了。
连接到数据库并查询图像的 select 信息后,我将带有 $_SESSION 的变量传递给另一个 PHP 文件,以便将它们上传到 FTP 远程服务器,如下所示:
conn_auth.php 对于 select 目标图像:
<?php
if (file_exists(__DIR__.'/conn_auth/db_conn.php')){
include __DIR__.'/conn_auth/db_conn.php';
}else{
die('Connection is Failed ...!');
}
$conn = OpenConn();
$sql = "SELECT * FROM users where status = 'Active'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id = $row["id"];
//search for user_id in userAthentication Table in DB
$sql = "SELECT * FROM userauthentication where user_id = $user_id";
$result = $conn->query($sql);
$user_auth = $result->fetch_assoc();
$user = array(
'id'=>$row['id'],
'name'=>$row['name'],
'family'=>$row['family'],
'auth_image'=>'',
'file_name'=>$user_auth["image"],
'phone'=>$row["phone"]
);
$url = "/home/root/app_name_1/uploads/".$user["file_name"];
if(!file_exists($url)){
$url = "/home/root/app_name_2/uploads/".$user["file_name"];
}
$user['auth_image'] = $url;
session_start();
$_SESSION["userInfo"] = $user;
$_SESSION["userAuth"] = $user_auth;
if (file_exists(__DIR__.'/conn_auth/get_auth.php')){
include __DIR__.'/conn_auth/get_auth.php';
}else{
die("I can't Connect to the Server");
}
}
} else {
echo "0 results";
}
CloseConn($conn);
在这个文件上,我尝试连接到 FTP 远程服务器并使用 ftp_put() 方法上传图像,但它不起作用:
<?php
include_once __DIR__.'../conn_auth.php';
session_start();
$ftp_server="*.***.***.**";
$ftp_user_name="****";
$ftp_user_pass="**********";
$file = $_SESSION["userInfo"]['auth_image']; //to be uploaded
$folder_name = $_SESSION["userInfo"]['phone'];
$file_name = $_SESSION["userInfo"]["file_name"];
$remote_file = "/HDD/pics/$folder_name";
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_mkdir($conn_id,'auth_pics').ftp_chdir($conn_id,'auth_pics');
if (ftp_mkdir($conn_id,"$folder_name")){
ftp_chdir($conn_id,$folder_name);
}else{
echo "I Can't Create Folder For : ".$folder_name;
die();
}
// upload a file
$upload = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);
if ($upload) {
echo "successfully uploaded : $file\n";
} else {
echo "There was a problem while uploading...\n";
}
// close the connection
ftp_close($conn_id);
?>
我使用 PHP 将图像从主机上传到 FTP 远程服务器。
我已经把需要上传到FTP远程服务器的文件名存储在数据库中了。
连接到数据库并查询图像的 select 信息后,我将带有 $_SESSION 的变量传递给另一个 PHP 文件,以便将它们上传到 FTP 远程服务器,如下所示:
conn_auth.php 对于 select 目标图像:
<?php
if (file_exists(__DIR__.'/conn_auth/db_conn.php')){
include __DIR__.'/conn_auth/db_conn.php';
}else{
die('Connection is Failed ...!');
}
$conn = OpenConn();
$sql = "SELECT * FROM users where status = 'Active'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id = $row["id"];
//search for user_id in userAthentication Table in DB
$sql = "SELECT * FROM userauthentication where user_id = $user_id";
$result = $conn->query($sql);
$user_auth = $result->fetch_assoc();
$user = array(
'id'=>$row['id'],
'name'=>$row['name'],
'family'=>$row['family'],
'auth_image'=>'',
'file_name'=>$user_auth["image"],
'phone'=>$row["phone"]
);
$url = "/home/root/app_name_1/uploads/".$user["file_name"];
if(!file_exists($url)){
$url = "/home/root/app_name_2/uploads/".$user["file_name"];
}
$user['auth_image'] = $url;
session_start();
$_SESSION["userInfo"] = $user;
$_SESSION["userAuth"] = $user_auth;
if (file_exists(__DIR__.'/conn_auth/get_auth.php')){
include __DIR__.'/conn_auth/get_auth.php';
}else{
die("I can't Connect to the Server");
}
}
} else {
echo "0 results";
}
CloseConn($conn);
在这个文件上,我尝试连接到 FTP 远程服务器并使用 ftp_put() 方法上传图像,但它不起作用:
<?php
include_once __DIR__.'../conn_auth.php';
session_start();
$ftp_server="*.***.***.**";
$ftp_user_name="****";
$ftp_user_pass="**********";
$file = $_SESSION["userInfo"]['auth_image']; //to be uploaded
$folder_name = $_SESSION["userInfo"]['phone'];
$file_name = $_SESSION["userInfo"]["file_name"];
$remote_file = "/HDD/pics/$folder_name";
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_mkdir($conn_id,'auth_pics').ftp_chdir($conn_id,'auth_pics');
if (ftp_mkdir($conn_id,"$folder_name")){
ftp_chdir($conn_id,$folder_name);
}else{
echo "I Can't Create Folder For : ".$folder_name;
die();
}
// upload a file
$upload = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);
if ($upload) {
echo "successfully uploaded : $file\n";
} else {
echo "There was a problem while uploading...\n";
}
// close the connection
ftp_close($conn_id);
?>