简体   繁体   English

通过Web服务器使用PHP代码将应用程序中的本地文件上传到FTP服务器

[英]Upload a local file from application via web server with PHP code to FTP server

I would like some help concerning how to upload a local file from an application to FTP using PHP code. 我想要一些有关如何使用PHP代码将本地文件从应用程序上传到FTP的帮助。 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). 我正在开发一个简单的Windows应用程序(通过可视化编程)。 The app will upload a local file to my FTP server through a web site (eg http://www.mysite/folder/ftp.php ). 该应用程序将通过网站(例如http://www.mysite/folder/ftp.php )将本地文件上传到我的FTP服务器。 I am doing this in order to avoid including my FTP login details in the app's code. 我这样做是为了避免在应用程序的代码中包含我的FTP登录详细信息。

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 (eg C:\\User\\Desktop\\myfile.txt ). 我发现一个PHP代码似乎有效,但是我不明白如何在代码中包含本地文件的路径(例如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']; 最后,将在链接中指定本地路径(例如http://www.mysite/folder/ftp.php?localpath = ....。 ),所以我认为我应该使用$file=$_GET['file']; instead of $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); 
?>

Your PHP code on your webserver absolutely cannot access your local files. 您在网络服务器上的PHP代码绝对无法访问您的本地文件。 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 ). 您的应用程序必须使用HTTP文件上传( Content-Type: multipart/form-data )将本地文件(其内容,不仅仅是文件名)主动上传到Web服务器。

Then, your PHP code can refer to the uploaded file using $_FILES variable. 然后,您的PHP代码可以使用$_FILES变量引用上载的文件。
See POST method uploads in PHP. 请参阅PHP中的POST方法上传

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. 对于客户端,请使用简单的HTTP / HTML文件上传表单来测试服务器端部分。 And once you have that up-and-running, replace the HTML upload form with a code in your application. 一旦运行起来,就用应用程序中的代码替换HTML上传表单。

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="..."/> ). 换句话说,如果要使用PHP内置支持来上传文件,则在提交带有文件的HTML <form>时,需要使您的应用程序执行与Web浏览器相同的HTTP请求( <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). 如果发现在(客户端)开发环境(我们对此一无所知)中实现HTTP文件上传很困难,则可以使用一些更简单的机制,例如带有文件内容的简单HTTP POST请求(无分段,只有二进制数据)的文件)。 Though I believe that most desktop app most frameworks support HTTP file upload these days natively. 尽管我相信大多数台式机应用程序如今大多数框架本身都支持HTTP文件上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM