简体   繁体   中英

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. 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 (eg 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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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