I need to upload same file to 2 different place in same FTP. Is there a way to copy the file on the FTP to the other place instead of upload it again? Thanks.
There's no standard way to duplicate a remote file over the FTP protocol. Some FTP servers support proprietary or non-standard extensions for this though.
Some FTP clients do support the remote file duplication. Either using the extensions or via a temporary local copy of the remote file.
For example WinSCP FTP client does support the duplication using both drag&drop and menu/keyboard command:
It supports the SITE CPFR/CPTO FTP extension (supported for example by the ProFTPD mod_copy module)
It falls back to an automatic duplication via a local temporary copy, if the above extension is not available.
(I'm the author of WinSCP)
Another workaround is to open a second connection to the FTP server and make the server upload the file to itself by piping a passive mode data connection to an active mode data connection. This solution is shown in the answer by #SaadAchemlal. This is basically use of FXP protocol, but for one server. Though many FTP servers will reject this, as they wont allow data connection to/from an address different to the client's.
Side note: people often confuse move with copy. In case you actually want to move, then that's a completely different question. Moving file on FTP is widely supported.
I don't think there's a way to copy files without downloading and re-uploading, at least I found nothing like this in the List of FTP commands and no client I have seen so far supported something like this.
Yes, the FTP protocol itself can support this in theory. The FTP RFC 959 discusses this in section 5.2 (see the paragraph starting with "When data is to be transferred between two servers, A and B..."). However, I don't know of any client that offers this sort of dual server control operation.
Note that this method could transfer the file from the FTP server to itself using its own network, which won't be as fast as a local file copy but would almost certainly be faster than downloading and then reuploading the file.
I can copy files between remote folders in Linux based systems.
In my particular case, I'm using very common file manager PCManFM:
Menu "Go" --> "Connect to server"
FTP Login info, etc
Open new tab in PCManFM
Connect to same server
Copy from tab to tab...
It's a bit slow, so I guess that it could be downloading and uploading back the files, but it's done automatically and very user-friendly.
The code below makes the FTP server to upload the file to itself (using loopback connection). It needs the FTP server to allow both passive and active connection mode.
If you want to understand the ftp commands here is a list of them : List of ftp commands
function copyFile($filePath, $newFilePath)
{
$ftp1 = ftp_connect('192.168.1.1');
$ftp2 = ftp_connect('192.168.1.1');
ftp_raw($ftp1, "USER ftpUsername");
ftp_raw($ftp1, "PASS mypassword");
ftp_raw($ftp2, "USER ftpUsername");
ftp_raw($ftp2, "PASS mypassword");
$res = ftp_raw($ftp2, "PASV");
$addressAndPort = substr($res[0], strpos($res[0], '(') + 1);
$addressAndPort = substr($addressAndPort, 0, strpos($addressAndPort, ')'));
ftp_raw($ftp1, "CWD ." . dirname($newFilePath));
ftp_raw($ftp2, "CWD ." . dirname($filePath));
ftp_raw($ftp1, "PORT ".$addressAndPort);
ftp_raw($ftp1, "STOR " . basename($newFilePath));
ftp_raw($ftp2, "RETR " . basename($filePath));
ftp_raw($ftp1, "QUIT");
ftp_raw($ftp2, "QUIT");
}
I managed to do this by using WebDrive to mount the ftp as a local folder, then "download" the files using filezilla directly to the folder. It was a bit slower than download normally is, but you dont need to have the space on your hdd.
Here's another workaround using PHP cUrl to execute a copy request on the server by feeding parameters from the local machine and reporting the outcome:
Local code:
In this simple test routine, I want to copy the leaning tower photo to the correct folder, Pisa:
$ch = curl_init();
$data = array ('pic' => 'leaningtower', 'folder' => 'Pisa');
curl_setopt($ch, CURLOPT_URL,"http://travelphotos.com/copypic.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Server code (copypic.php):
On the remote server, I have simple error checking. On this server I had to mess with the path designation, i.e., I had to use "./" for an acceptable path reference, so you may have to tinker with it a bit.
$pic = $_POST["pic"];
$folder = $_POST["folder"];
if (!$pic || !$folder) exit();
$sourcePath = "./unsortedpics/".$pic.".jpg";
$destPath = "./sortedpics/".$folder."/".$pic.".jpg";
if (!file_exists($sourcePath )) exit("Source file not found");
if (!is_dir("./sortedpics/".$folder)) exit("Invalid destination folder");
if (!copy($sourcePath , $destPath)) exit("Copy not successful");
echo "File copied";
You can do this from C-Panel.
Log into your C-Panel.
Go into file manager.
Find the file or folder you want to duplicate.
Right-click and chose Copy.
Type in the new director you want to copy to.
Done!
You can rename the file to be copied into the full path of your wanted result.
For example:
If you want to move the file "file.txt" into the folder "NewFolder" you can write it as
ftp> rename file.txt NewFolder/file.txt
This worked for me.
I am working on a CMS that will be installed for many clients, but as I keep on improving it I make modifications to a few files. I want these updates to be automatically applied to all the projects using the same files.
I thought of executing a check file every time the CMS is opened. This file would compare the version of the local file with the remote file, for this I can keep a log or something for the versions, no big deal, but that's not the problem, here is some sample code I thought of:
$url = 'http://www.example.com/myfile.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
The problem is getting the content of myfile.php since its a PHP file the server will execute it and return the output, but I want the actual content of the file. I understand that this is not possible as it would be a security problem, anybody would be able to get the php code of other sites, but is there any way to get the contents of a remote php file maybe by giving special permissions to a remote connection?
Thanks.
You should create a download script on your remote server which will return the original php code by using readfile().
<?php
$file = $_SERVER['DOCUMENT_ROOT'] . $_GET['file'];
// #TODO: Add security check if file is of type php and below document root. Use realpath() to check this.
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
?>
Get file contents by fethcing http://example.com/download.php?file=fileName
I have a problem where my hosting company won't let me run a cron job in this format from my control panel:
/usr/bin/php /home/sites/MYDOMAIN.com/index.php?option=com_community&task=cron
Or:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
Now if i run the second job in a browser i.e.:
www.MYDOMAINNAME.com/index.php?option=com_community&task=cron
this works fine in a browser
My support says I have to create a file to run the URL. The only problem is I don’t know how to run a URL in PHP. I have asked a few sites. But nothing. My file is called bump.php and has the following code:
lynx -dump http://www.MYDOMAIN.com/index.php?option=com_community&task=cron
this is what i have in the file
<?php
echo file_get_contents('DOMAIN.com/index.php?option=com_community&task=cron');
?>
You have to access the file in question via your webserver, not directly by file access. If you access it by file-access, it will just return the php code and not execute it.
There are several options on how to access files via webserver. One is your shown method with file_get_contents. You will need to add http:// in front of the url to tell PHP that you want it accessed remotly and not as a local file.
file_get_contents is not allways configured to allow remotely downloading files. In these cases, it will not work. You can check this link to see the configuration setting for remote accessing files:
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Another solution is to use the curl extension (if available)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
http://www.php.net/manual/en/function.curl-exec.php
There are other extensions if curl is also not available...
I have two server's server A and server B
I have a registration page which have username,*email*,password and an image.
I am using a simple php script to upload the image sent from an ios device.
I am able to register the new user to server A. Image upload works well in the following cases
Case 1: Uploading via HTML (Success)
Case 2: Uploading Via app (Success)
But when trying to upload the image to server B (LINUX server) image uploading failed.
Case 1: Uploading via HTML (Success)
Case : Uploading via app (FAILED)
I have made sure file permissions are 777. But still i am unable to upload the image to server. It is returning 500 Internal error.
Actually, The uploading to Server B used to work earlier. But all of a sudden it stopped working.
Can any one suggest me what more conditions should i check to make sure image will upload?
Try using CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#/path/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');
curl_exec($ch);
curl_close($ch);
Setting the permission on file alone won't solve the purpose.Make sure you have set directory for upload_tmp_dirin php.ini to writable by whatever user php is running.
Check with sys_get_temp_dir() what it returns,make sure returned path is writable.
If you can ssh to your linux server you check the permission on temp directory by following command
$ls -ld temp_dir_path_here
Of course there could be other problems causing the failure.You can get more information by checking you server error logs or set errors on by copying following snippet at top of your script
error_reporting(E_ALL);
ini_set('display_errors', 'on');
Remove above snippet when done.
I am a realtor/web designer. I am trying to write a script to download a zip file to my server from a ftp server. My website is all in php/mysql. The problem that I am having is that I cannot actually log in to the ftp server. The file is provided through a link that is available to me, but access to the actual server is not available. Here is the link i have access to.
ftp://1034733:ze3Kt699vy14#idx.living.net/idx_fl_ftp_down/idx_ftmyersbeach_dn/ftmyersbeach_data.zip
php's normal functions for accomplishing this give me a connection error. Php does not have permission to access this server... Any solutions to this problem would be a life saver for me. I am looking to use a cron job to run this script every day so i don't have to physically download this file and upload it to my (godaddy) server, which is my current solution (not a good one, i know!).
Also, I can figure out how to unzip the file myself, as I have done some work with php's zip extension, but any tips for an efficient way to do this would be appreciated as well. I am looking to access a text file inside of the zip archive called "ftmyers_data.txt"
Do you have shell access to the server on which your PHP application runs? If so, you might be able to automate the retrieval using a shell script and the ftp shell command.
Use php_curl
$curl = curl_init();
$fh = fopen("file.zip", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://1034733:ze3Kt699vy14#idx.living.net/idx_fl_ftp_down/idx_ftmyersbeach_dn/ftmyersbeach_data.zip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
fwrite($fh, $result);
fclose($fh);
curl_close($curl);
Easy as pi. :)