Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP FTP function

Status
Not open for further replies.

lazydays

Technical User
Sep 29, 2003
93
GB
I have written a script that uploads .rm files to my server - it all works fine.

However, my host has allowed me the use of their streaming media server, and I need to upload to that server using a script on my normal server and use the ftp function.

Every example I have found, uses only local paths for ftp-ing to the same server the script is run on.
Is there a way that I can use the ftp function to upload a file to another server location?
 
I am not following your logic.
Every example I have found, uses only local paths for ftp-ing to the same server the script is run on.

FTP is a protocol to exchange files between servers. FTP'ing to the same server the script is run on makes absolutely no sense.

There is a basic example for a PHP based FTP script on the first page of the PHP FTP documentation:
Have a look at that and you should be all set.
If it is still unclear, please let us know so we can clear up the confusion.
 
lazydays,

I'm not really sure what your problem is. The only difference should be that the host, login information, and destination paths are on the other server, while the source paths are on your server. Is that not working?

DRJ478,

The "FTP to localhost" trick is actually quite handy for storing information in files (as opposed to a database). In fact, I use it in my personal weblog/CMS software. It's particularly useful for shared hosting environments or servers where you don't have root access.

Basically, using the native filesystem access functions completely messes up file permissions and ownership, whereas FTP keeps everything sane. Since PHP typically writes files as 'apache' or some other such system user, that user must have write permissions to the target file/directory, which often means making it world-writable. In addition, if PHP creates new files, then those are owned by the system user, so you can't move or delete them from an FTP or SSH session (at least, not without it being a big pain).

With FTP, however, you can login and perform operations as whichever account you choose, e.g. the account the hosting company gives you. You can even use ftp_fput() to create new files which are owned by your account, thus eliminating worries about file ownership and permissions. It also has the handy effect of allowing you to bypass the file ownership restrictions when safe mode is enabled, since you can create files as the same user who owns the scripts.
 
AdaHacker
I'm spoiled by not having to use a host. The safe-mode thing is an excellent example. Thanks for the nudge.
 
OK, I have used this as the base of the my ftp script.

<?php
// 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);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream
ftp_close($conn_id);
?>

The connection works fine, but i am unsure of what to put in the destination and source files - should they be file paths or what?
 
OK, now it's just the source file I'm having problems with...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top