Scope of ftp_put function in PHP cant it make transfers between two different location on the same server

Is the FTP_PUT function in PHP not designed that it can move files between two locations on the same server?

<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';

// 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";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
?>

The above is an example from the php.net = php.net/manual/en/function.ftp-put.php#89986

None of the files and remote_file has a path included in it?

because while I tried it doesnt worked, and generated errors:

$source = '/app.trafficopedia.com/ftp/message.txt';
$destination = '/app.trafficopedia.com/ftp/ftpsend/message.txt';
$upload = ftp_put($newconn->conn,$destination,$source, FTP_BINARY);

Warning : ftp_put(/app.trafficopedia.com/ftp/message.txt): failed to open stream: No such file or directory in

Either this is not designed to work in such a way or some understanding and implementation gap or there is some hack to be used to make this work.

N.B. The path that I have taken is the path that was available in the FileZilla not what getcwd offers. getcwd doesn’t offers top-level directory.

I am finding a way around because I have some purpose in some future WP project.

Live Link

As far as I know ftp_put is just for uploading a file from one server to another.

Have you tried ftp_rename? I think that should do what you’re after.

2 Likes

Thanks for replying. What I understand is that ftp_put is used to transfer files from the local server(windows/mac) using some upload button feature to the remote server. My conclusion may be flawed as this is What I interpreted based on what was written on php.net

ftp_rename is not useful. I have the intention of building a WP plugin a few months from now where files backed up can be transferred to a remote server. so I was giving it a shot on my live server.

If moderators allow then I can share a program, which is a small one build with the help of people here on Sitepoint. It has FTP login credentials. The FTP is pointing only to a directory, and I am trying everything within that directory(that has no sensitive data) so there is no security threat in sharing such details.

With everything handy people may give a try else this seems to be boring, and not too much interest can be aroused.

FTP is for transferring data between different locations, so you don’t want that for moving a file within the same server.

1 Like

I think it is written for transfer between client machine(Desktops) to remote locations.

ftp_put is for transferring a file from whatever machine (ie. a web server) is running the PHP script to a remote FTP server.

ftp_rename is either for renaming a file on a remote FTP server or moving a file from one location on a remote FTP server to another location on the same server.

I don’t fully understand what it is that you’re trying to achieve, if it’s not either of the above?

Currently,

I am trying to transfer the message.txt file from this location in the FTP server:
/app.trafficopedia.com/ftp

to

/app.trafficopedia.com/ftp/ftpsend/

In some real projects in the future, the “to” will be replaced by another live server.(Transaction between two live servers).

OK, let me get this straight:

You have a PHP script running on Server A.

There is a file on Server B which you can access via FTP.

You want to copy/move that file to Server C, which you also have FTP access to.

Is that correct?

Yes,

No File is also on server A.

/app.trafficopedia.com/ftp

No file is also on server A, and FTP is also of server A.

/app.trafficopedia.com/ftp/ftpsend/

This is testing, but in future real situations they will be server A and B.

FTP doesn’t care whether the client and server are on the same machine. The FTP client connects to the FTP server, and does operations on that server to the extent that its permissions allow it. As noted above and in the documentation, ftp_put() is designed to move a file from the client to the server, not from one location on the server to another location on the server.

Also remember that, like Apache, FTP servers work on virtual directories which may or may not have any relation to the physical directory structure underneath it. When your FTP account is created, you’ll have a virtual root directory. So while your local file might be in the same physical folder, when you sign in via ftp you may well already be one or more levels “down” the directory structure. You can easily see this using an FTP client.

Also when sending files from the local machine to the FTP server, in many cases you cannot “put” directly into a directory, you must first “ftp_chdir()” into the destination folder and then upload the file. This varies from server to server.

1 Like

so what I am trying cant be accomplished in this case?

Well, you could sign into the FTP server, download the file (I don’t know anything about PHP FTP operations, I’d guess it’s ftp_get() though), chdir into the location you want it to end up, and ftp_put() it again. That would copy it from the first location to the second.

Have you done much experimenting with a normal FTP client, preferably a command-line one? That would give you an idea of what can and can’t be done. In fact even if you use something like the Filezilla client, that will show you which FTP commands are being executed for each operation.

1 Like

What is the meaning of this error:
Warning : ftp_put(/app.trafficopedia.com/ftp/message.txt): failed to open stream

Does the file exist? Is that really the name of the source or destination file? If it’s the destination file, did you consider the comments I made about perhaps having to chdir into the destination directory before uploading the file?

1 Like

Let me give it a try that way as you suggested. I will update you. thank you for coming into the discussion.

By the way this is the current code:

$source = '/app.trafficopedia.com/ftp/message.txt';
$destination = '/app.trafficopedia.com/ftp/ftpsend/message.txt';
// ftp_chdir($newconn->conn,$destination);
$upload = ftp_put($newconn->conn,$destination,$source, FTP_BINARY);

How can I rearrange this code?

Directory Path in Filezilla →

I’m pretty sure it can. I think the issue is this:

The error message is telling you what the problem is - it can’t find the file you’re trying to upload. Is that definitely the right location of the file on your server, bearing in mind that if the path starts with a / then it’s an absolute path meaning there is a folder called app.trafficopedia.com at the top level?

As a simple test I would start with a file in the same folder as the PHP script and just use the filename with no path. See if you can get that working and go from there.

1 Like

True, and correct too.

That is the case here.
same location as php script:

Additionally, FTP is pointing to this location:
/home1/toolculator/public_html/app.trafficopedia.com/ftp

Oh I see I got the miskate.

$source = '/app.trafficopedia.com/ftp/message.txt';
$destination = '/app.trafficopedia.com/ftp/ftpsend/message.txt';

should be

$source = 'message.txt';
$destination = 'ftpsend/message.txt';

It is working now which means ftp_put can transfer between the same server too.

Great. It’s worth getting a better understanding of relative and absolute paths though.

In your previous message you said that app.trafficopedia.com is at the top level but it’s not, as shown by your screenshot from File Manager. If you’re using absolute paths it needs to be from the very top of the file system, so in this case it would be /home1/toolculator/public_html/app.trafficopedia.com/ftp/message.txt

But you don’t really want to hardcode the full path like that because if you move this to a different server the path will be different. So you’d want to either use a relative path (relative the the location of the PHP script that’s running) or get the full absolute path dynamically.

It’s something that can be quite tricky and trips me up from time to time too.

1 Like

True,
I lately un hardcoded it.