Jim
Jim

Reputation: 1315

ftp zip files from one server to another

I am attempting to transfer a zip file via ftp from one server to mine, so that I can use the data in the file to update my database table. Here is my ftp.php file:

    <?php
header('Content-type: text/html; charset=utf-8');
$date = "2013-05-21-11-19-40";
$ftp_server="ftp.server.com";
$ftp_user_name="BookCellar";
$ftp_user_pass="*****";
$file = "/reports/other/compreport_abebooks_" .$date. ".zip";//tobe uploaded
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip"; 

?>

and my ftpUpload.php file is:

    <?php
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Content-Type: application/zip');
require_once('ftp.php');

// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);//ftp_connect

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;

} else {
echo "Connected for user $ftp_user_name" ;
}

ftp_chdir($conn_id, '/home/bookcell/bookcellaronline.com/html/testbcos/accounting/');

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
echo $php_errormsg;
// close the connection
ftp_close($conn_id);
?>

When I run these I get the error messge:

    [<a href='function.ftp-put'>function.ftp-put</a>]: failed to open stream: No such file or directory in /chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php on line 25

Line 25 is:

    if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {

I have researched a bunch of other posts on SO and have not found a solution. My connection works, but I can't get the file to transfer. How do I (if even possible) get this to transfer to my server?

EDIT: Am I missing the fact that I need to connect not only to their server($file) AND my server ($remote_file)????

Upvotes: 1

Views: 4061

Answers (3)

Nikunj Kathrotiya
Nikunj Kathrotiya

Reputation: 963

set_time_limit(0);
exec("wget --continue http://example.com/site.zip");
exit;

Upvotes: 2

moskito-x
moskito-x

Reputation: 11958

You can't put a path to the destination file

$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";

e.g. - this doesn't work

ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);

you have to put

<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY ); 

A FTP server hides his absolute path /home/bookcell/bookcellaronline.com/html/

All folders must be relative to the root

ftp_chdir($conn_id, '/testbcos/accounting/');

test the result of ftp_chdir ! Are your in the right directory ?

echo ftp_pwd($conn_id);

Try to connect to the FTP server via a browser.

ftp://[email protected]

What you get is / the root. Folders and files that you see in the browser, are below the root directory.

Update : A working out of the box example.

the $password = line must be replaced with Download pass

ftp.php

<?php
    $password = "????";
    $resource = ftp_connect('ftp.strato.com');
    $login = ftp_login($resource, '[email protected]', $password);
    $list = ftp_rawlist($resource, '/');
    print_r($list);
?>

You will get with print_r

Array ( [0] => drwxr-xr-x 2 ftp ftp 4096 May 23 20:15 aFolder [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt )

we can see there is a folder aFolder and a file tutorial.txt.
We are interest what files are in the folder aFolder ?
So replace the $list = with

$list = ftp_rawlist($resource, '/aFolder');

and run the php script again. The output :

Array ( [0] => drwxr-xr-x 3 ftp ftp 4096 May 23 19:24 .. [1] => -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt [2] => -rw-r--r-- 1 ftp ftp 271 May 23 21:16 tutorial.zip )

Now we want to download aFolder/tutorial.txt .
Add following below print_r($list); .

     echo "<br />\n";
     $local_file = "tmp.txt" ;
     $file = ftp_get($resource, $local_file, '/aFolder/tutorial.txt',FTP_ASCII);
      if ($file) {
         echo "$local_file has been successfully written\n";
     } else {
         echo "An error has occurred\n";
     }

The Output :

enter image description here

The folder where the php script is has changed.

enter image description here

now there is a new file tmp.txt !

If you bring this little script to run. We can go further.

From our chat :
Your server does not allow a ftp call to a url.

look allow_url_fopen = ON

echo ini_get('allow_url_fopen');

if (!ini_get('allow_url_fopen')) {
    ini_set('allow_url_fopen', 1);
}

echo ini_get('allow_url_fopen'); 

Then try it again.

Upvotes: 2

Orangepill
Orangepill

Reputation: 24645

In the first excerpt change the remote tile to be

`$remote_file = "compreport_abebooks_" .$date. ".zip";`

prior to the put you are changing into the directory.

Also note that the directory that you reference in the ftp_chdir call differs from the one in the $remote_file referenced at the top.

 /home/bookcell/bookcellaronline.com/html/testbcos/accounting/

vs

 /chroot/home/bookcellaronline.com/html/testbcos/accounting/

Upvotes: 0

Related Questions