通过Web服务器使用PHP代码将应用程序中的本地文件上传到FTP服务器 [英] Upload a local file from application via web server with PHP code to FTP server

查看:85
本文介绍了通过Web服务器使用PHP代码将应用程序中的本地文件上传到FTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些有关如何使用PHP代码将本地文件从应用程序上传到FTP的帮助. 我是一个业余程序员,可以阅读非常基本的代码结构.因此,请避免使用编程术语!

我正在开发一个简单的Windows应用程序(通过可视化编程).该应用程序将通过网站将本地文件上传到我的FTP服务器(例如 http://www.mysite /folder/ftp.php ).我这样做是为了避免在应用程序的代码中包含我的FTP登录详细信息.

我发现一个PHP代码似乎有效,但是我不明白如何在代码中包含本地文件的路径(例如C:\User\Desktop\myfile.txt).我希望使用以下代码,我发现它很容易理解.我只是不知道如何指定本地文件的路径以及该文件将被上传到的路径.最后,将在链接中指定本地路径(例如 http://www.mysite/folder/ftp.php?localpath = .... .),所以我认为我应该使用$file=$_GET['file'];而不是$file = ".....";.

<?php 
$ftp_server=""; 
$ftp_user_name=""; 
$ftp_user_pass=""; 
$file = "";//tobe uploaded 
$remote_file = ""; 

// set up basic connection 
$conn_id = ftp_connect($ftp_server); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// upload a file 
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { 
    echo "successfully uploaded $file\n"; 
    exit; 
} else { 
    echo "There was a problem while uploading $file\n"; 
    exit; 
    } 
// close the connection 
ftp_close($conn_id); 
?>

解决方案

您在网络服务器上的PHP代码绝对无法访问您的本地文件.那将是一场安全噩梦.


您的应用程序必须使用HTTP文件上传(Content-Type: multipart/form-data)主动将本地文件(其内容,而不仅仅是文件名)上传到Web服务器.

然后,您的PHP代码可以使用$_FILES变量引用上载的文件.
请参见PHP中的 POST方法上传. /p>

由于您似乎难以理解客户端和服务器端代码,因此您可能应该首先仅实现(和调试)服务器端.对于客户端,请使用简单的HTTP/HTML文件上传表单来测试服务器端部分.并且,一旦启动并运行,就用应用程序中的代码替换HTML上载表单.

换句话说,如果要使用PHP内置的支持来上传文件,则在提交带有文件的HTML <form>时,需要使您的应用程序执行与Web浏览器相同的HTTP请求( <input type="file" name="..."/>).


如果发现在(客户端)开发环境(我们对此一无所知)中难以实现HTTP文件上传,则可以使用一些更简单的机制,例如带有文件内容的简单HTTP POST请求(不多部分,只是文件的二进制数据).尽管我相信大多数台式机应用程序如今大多数框架本身都支持HTTP文件上传.

I would like some help concerning how to upload a local file from an application to FTP using PHP code. I am an amateur programmer that can read very basic code structure. So please avoid using programming terms!

I am developing a simple Windows application (through visual programming). The app will upload a local file to my FTP server through a web site (e.g. http://www.mysite/folder/ftp.php). I am doing this in order to avoid including my FTP login details in the app's code.

I have found a PHP code that seems to work, but I do not understand how to include my local file's path in the code (e.g. C:\User\Desktop\myfile.txt). I wish to use the following code, which I found pretty easy to understand. I just don't know how to specify the path of the local file as well as the path where this file will be uploaded. Finally, the local path will be specified in the link (eg http://www.mysite/folder/ftp.php?localpath=.....), so I think that I should use a $file=$_GET['file']; instead of $file = ".....";.

<?php 
$ftp_server=""; 
$ftp_user_name=""; 
$ftp_user_pass=""; 
$file = "";//tobe uploaded 
$remote_file = ""; 

// set up basic connection 
$conn_id = ftp_connect($ftp_server); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// upload a file 
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { 
    echo "successfully uploaded $file\n"; 
    exit; 
} else { 
    echo "There was a problem while uploading $file\n"; 
    exit; 
    } 
// close the connection 
ftp_close($conn_id); 
?>

解决方案

Your PHP code on your webserver absolutely cannot access your local files. That would be a security nightmare.


Your application has to actively upload your local file (its contents, not just file name) to the web server using an HTTP file upload (Content-Type: multipart/form-data).

Then, your PHP code can refer to the uploaded file using $_FILES variable.
See POST method uploads in PHP.

As you seem to have troubles understanding both client- and server-side code, maybe you should start by first implementing (and debugging) the server-side only. For client-side, use a simple HTTP/HTML file upload form for testing the server-side part. And once you have that up-and-running, replace the HTML upload form with a code in your application.

In other words, if you want to use a PHP built-in support for uploading files, you need to make your application do the same HTTP request as a web browser does, when submitting an HTML <form> with a file (<input type="file" name="..."/>).


If you find implementing an HTTP file upload difficult in your (client) development environment (which we do not know anything about), you can use some simpler mechanism, like a simple HTTP POST request with a file contents (no multipart, just binary data of the file). Though I believe that most desktop app most frameworks support HTTP file upload these days natively.

这篇关于通过Web服务器使用PHP代码将应用程序中的本地文件上传到FTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆