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);
?>
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_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?
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.
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.
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?
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.
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.