从本地服务器获取文件并使用 php 将其上传到远程服务器

发布于 2025-01-03 23:28:53 字数 2041 浏览 0 评论 0原文

我知道有很多关于这个问题的帖子,但是我在这里找不到一个答案来准确描述我正在尝试做的事情,并且我已经搜索了几个小时......所以希望你们都能帮助我并引导我走向正确的方向方向,我不知道如何做的主要事情是获取源文件并将其传递给我的更新功能,我将在其中将该文件 ftp 到远程服务器......

基本上我想做的是以下内容:

1)从我的本地主机获取文件(例如/home/user/public_html/file.php) 2) 将该文件上传到我的一个客户的远程服务器(我将他们所有的 FTP 信息存储在我的数据库中,并且可以很好地连接到他们的服务器) 3) 如果远程服务器上存在该文件,则用该文件覆盖该文件。

现在,我已经尝试 ftp_get 来获取文件,但这不是我想要做的,因为我不需要重写或将相同的文件存储到本地服务器...

这是我到目前为止所拥有的...

非常感谢任何帮助,如果我在其他地方没有看到这个问题的答案,我深表歉意! :)

作为对此的更新,我只是想到了一些事情...

为什么我必须通过 ftp 获取本地文件?难道我不应该能够以其他方式获取本地文件并将其传递给我的更新函数,在那里它将将该文件上传到远程服务器吗?

update.php

$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
$file = "relative/path/to/my/file";
$source = $updater->getFile($file);
$updater->update($source,$file);

我的更新类

class updater {
var $ftp_user = "";
var $ftp_pass = "";
var $ftp_host = "";

function __construct($user,$pass,$host) {
$this->ftp_user = $user;
$this->ftp_pass = $pass;
$this->ftp_host = $host;    
}



 function getFile($file) {
$curl = curl_init();
$file = fopen($file, 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://domain.com/".$file); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "ftpuser:ftppass");
$returnFile = curl_exec($curl);
return $returnFile;
}

function update($source,$file) {
    echo("Source: ".$source."<BR>");
    echo("File: ".$file."<BR>");
$connection = ftp_connect($this->ftp_host);

$login = ftp_login($connection, $this->ftp_user, $this->ftp_pass);

if (!$connection || !$login) { die('Connection attempt failed!'); }

$upload = ftp_put($connection, "/home/".$this->ftp_host."/public_html/".$file, $source, FTP_BINARY);

if (!$upload) { 
echo 'FTP upload failed!'; 
} else {
echo("UPDATED FILE ".$source." SUCCESSFULLY!<BR>"); 
}

ftp_close($connection);     
}

I know there are many posts about this issue however I cannot find one single answer on here that describes EXACTLY what I am trying to do and I have been searching for HOURS...so hopefully you all can help me and steer me in the right direction, the main thing I cannot figure out how to do is get the source file and pass that to my update function where I will ftp that file to the remote server...

Basically what I am trying to do is the following:

1) Get a file from my local host (ex. /home/user/public_html/file.php)
2) Upload that file to a remote server of one of my clients (I have all of their FTP information stored in my database and can connect to their server just fine)
3) If the file exists on the remote server, have it over write with the one.

Now, I have tried ftp_get to get the file but that is not what I want to do because I don't need to re-write or store the same file to the local server...

Here's what I have so far...

Any help is much appreciated and I do apologize if this is answered some where else I just didn't see it! :)

AS AN UPDATE TO THIS I JUST THOUGHT OF SOMETHING...

Why do I even have to get the local file via ftp? Shouldn't I just be able to get the local file some other way and pass that to my update function where it will upload that file to the remote server?

update.php

$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
$file = "relative/path/to/my/file";
$source = $updater->getFile($file);
$updater->update($source,$file);

my update class

class updater {
var $ftp_user = "";
var $ftp_pass = "";
var $ftp_host = "";

function __construct($user,$pass,$host) {
$this->ftp_user = $user;
$this->ftp_pass = $pass;
$this->ftp_host = $host;    
}



 function getFile($file) {
$curl = curl_init();
$file = fopen($file, 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://domain.com/".$file); #input
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_FILE, $file); #output
curl_setopt($curl, CURLOPT_USERPWD, "ftpuser:ftppass");
$returnFile = curl_exec($curl);
return $returnFile;
}

function update($source,$file) {
    echo("Source: ".$source."<BR>");
    echo("File: ".$file."<BR>");
$connection = ftp_connect($this->ftp_host);

$login = ftp_login($connection, $this->ftp_user, $this->ftp_pass);

if (!$connection || !$login) { die('Connection attempt failed!'); }

$upload = ftp_put($connection, "/home/".$this->ftp_host."/public_html/".$file, $source, FTP_BINARY);

if (!$upload) { 
echo 'FTP upload failed!'; 
} else {
echo("UPDATED FILE ".$source." SUCCESSFULLY!<BR>"); 
}

ftp_close($connection);     
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2025-01-10 23:28:53

试试这个:

    # Declare Files
    $local_file = "/home/user/public_html/file.php";
    $remote_file = "public_html/remote_file.php";

    # FTP
    $server = "ftp://ftp.yourhost.com/".$remote_file;

    # FTP Credentials
    $ftp_user = "ftp_username";
    $ftp_password = "password";

    # Upload File
    $ch = curl_init();
    $ftp_file = fopen($local_file, 'r');
    curl_setopt($ch, CURLOPT_URL, $server);
    curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
    $result = curl_exec($ch);

如果这符合您的要求,您可以轻松地将其放入函数中。

Try this:

    # Declare Files
    $local_file = "/home/user/public_html/file.php";
    $remote_file = "public_html/remote_file.php";

    # FTP
    $server = "ftp://ftp.yourhost.com/".$remote_file;

    # FTP Credentials
    $ftp_user = "ftp_username";
    $ftp_password = "password";

    # Upload File
    $ch = curl_init();
    $ftp_file = fopen($local_file, 'r');
    curl_setopt($ch, CURLOPT_URL, $server);
    curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
    $result = curl_exec($ch);

You can easily put it in a function if this does what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文