Php copy files ftp

ftp_put

ftp_put() stores a local file on the FTP server.

Parameters

The transfer mode. Must be either FTP_ASCII or FTP_BINARY .

The position in the remote file to start uploading to.

Return Values

Returns true on success or false on failure.

Changelog

Version Description
8.1.0 The ftp parameter expects an FTP\Connection instance now; previously, a resource was expected.
7.3.0 The mode parameter is now optional. Formerly it has been mandatory.

Examples

Example #1 ftp_put() example

$file = ‘somefile.txt’ ;
$remote_file = ‘readme.txt’ ;

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

// login with username and password
$login_result = ftp_login ( $ftp , $ftp_user_name , $ftp_user_pass );

// upload a file
if ( ftp_put ( $ftp , $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 ( $ftp );
?>

See Also

  • ftp_pasv() — Turns passive mode on or off
  • ftp_fput() — Uploads from an open file to the FTP server
  • ftp_nb_fput() — Stores a file from an open file to the FTP server (non-blocking)
  • ftp_nb_put() — Stores a file on the FTP server (non-blocking)

User Contributed Notes 33 notes

If when using ftp_put you get the one of the following errors:

Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection

Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection

and it creates the file in the correct location but is a 0kb file and all FTP commands thereafter fail. It is likely that the client is behind a firewall. To rectify this use:

ftp_pasv ( $resource , true );
?>

Before executing any put commands. Took me so long to figure this out I actually cheered when I did 😀

If you are having timeouts uploading a file, even very small files, you might have a look at ftp_pasv()

And don’t forget to do it after your ftp_login();

if you examine the first user submitted function, ftp_putAll, it will work only if you extract this line and its matching bracket.

if (!@ftp_chdir($conn_id, $dst_dir.»/».$file))

The function will have changed into that directory before having uploaded files to it. This alters your upload path and the system will try to upload into an essentially non-existent directory (duped at the end).

Hope this helps some of you.
Cheers.
Saeven

This is an extremely trivial thing but one that had me stumped forever (well, until I decided to check the error logs and see the error). I had a large file (mysql backup of a huge forum) that was only partially being uploaded to a remote backup server. Couldn’t figure out why, until I realized that the max execution time was being hit since it was taking longer than 30 seconds to upload this file.

set_time_limit ( 0 );
rest of the code here .
?>

That did the trick. It’s one of those dumb, trivial things, and if you’re having trouble like I, it may be something you overlooked.

Читайте также:  Java comparator reverse sort

The following is a fully tested function (based on a previous note) that recursively puts files from a source directory to a destination directory. See http://rufy.com/tech/archives/000026.html for more information.

NOTE: use full path name for the destination directory and the destination directory must already exist

If you want to copy a whole directory tree (with subdiretories),
this function (ftp_copy) might be usefull. Tested with
php 4.2.2 and a Linux OS.

ftp_copy($src_dir, $dst_dir);
.
ftp_close($conn_id)

if (!@ftp_chdir($conn_id, $dst_dir.»/».$file))

ftp_copy($src_dir.»/».$file, $dst_dir.»/».$file);
>
else

$upload = ftp_put($conn_id, $dst_dir.»/».$file, $src_dir.»/».$file, FTP_BINARY);
>
>
>

victor at nobel dot com dot br wrote that
the correct dirpath format excluded «/home/USER/» from the public path, but for my server, i had to use it in order to get my scripts to work.

it may be obvious to most but I’m positing that you cannot use the $_SERVER[‘DOCUMENT_ROOT’] path since FTP starts at your top-level and therefore bypasses (or just plain doesn’t recognize) most of the virtual server pathing.

ftp_put() can display confusing warning messages as it returns one line of the remote server’s response which may be multi lined.

If you’re transferring large amounts of files note that some file systems only support up to 2000 files per directory. This had me stumped for a while.

Here is the Code I am using for the same function with more flexibility in static code:
$name = «FILE NAME»;
$filename = «FILE NAME WITH FULL PATH»;

//— Code to Transfer File on Server Dt: 06-03-2008 by Aditya Bhatt —//
//— Connection Settings
$ftp_server = «IP ADDRESS»; // Address of FTP server.
$ftp_user_name = » SERVER USERNAME»; // Username
$ftp_user_pass = » SERVER PASSWORD»; // Password
$destination_file = » SERVER FILE PATH TO UPLOAD VIA FTP_PUT»; //where you want to throw the file on the webserver (relative to your login dir)

$conn_id = ftp_connect($ftp_server) or die(«

Couldn’t connect to $ftp_server

«); // set up basic connection

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die(«

You do not have access to this ftp server!

«); // login with username and password, or give invalid user message

if ((!$conn_id) || (!$login_result)) < // check connection
// wont ever hit this, b/c of the die call on ftp_login
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 = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY); // upload the file
if (!$upload) < // check upload status
echo «

FTP upload of $filename has failed!


«;
> else echo «

Uploading $name Completed Successfully!

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

Warning: ftp_put() [function.ftp-put]: ‘STOR’ not understood in
C:\wamp\www\kevtest\ftp_todays.php on line 48

Found the prob, you can’t put a path to the destination file
(even though I can do that in the dos ftp client. )

e.g. — this doesn’t work
ftp_put($conn, ‘/www/site/file.html’,’c:/wamp/www/site/file.html’,FTP_BINARY);

ftp_chdir ( $conn , ‘/www/site/’ );
ftp_put ( $conn , ‘file.html’ , ‘c:/wamp/www/site/file.html’ , FTP_BINARY );
?>

If you get this error when trying to send data to server :
Warning: ftp_put() [function.ftp-put]: Unable to build data connection: Connection timed out.

Читайте также:  Python check my ip address

Two solutions :
— Add the program httpd.exe in your exception list for external connexions of your firewall. Indeed, the FTP protocol open a new socket for data transfer. And this socket is opened from the server to the client (your computer). This program is located (for WAMP) in C:\wamp\bin\apache\Apache[version]\bin\
— Use the ftp_pasv() function to activate the passive mode. In this mode, it is the client who open the new socket to the server.

Currently, there is no function that lets you specifiy the file’s contents as a string. However, there is ftp_fput(), which operates on an open file. Using this function in conjunction with tmpfile() lets you emulate this kind of function. (You could also use php://memory, but this breaks BC).

function ftp_fputs ( $ftp_stream , $remote_file , $contents , $mode , $startpos = 0 )
$tmp = tmpfile ();
fwrite ( $tmp , $contents );
rewind ( $tmp );
$result = ftp_fput ( $ftp_stream , $remote_file , $tmp , $mode , $startpos );
fclose ( $tmp );
return $result ;
>
?>

I [had an error for which] ftp_pasv didnt solve the problem. Here’s why:

FTP uses 2 connections on different ports, one for connection/handshake and another for data transfer.
The problem was that the ftp-server (that php was connecting to) also used different IP-addresses for the different connections (say what!?).
Normally the firewall (csf) detects ftp-connections and allows them through but because of the different IP-adresses this didn’t work.

Solution:
1 angry mail to the ftp server owner.
Allowing the second IP in the firewall.

I’m copying fairly large backup files from server to server. ftp_put was running fine for awhile until it occasionally began reporting errors.
When I set TRUE as the value for the ftp_pasv () (after login), ftp_put started working again.

Here is a fix for the function from lucas at rufy dot com below that will recursively put files from a source directory to a destination directory. As written below, it won’t put a file in a directory that already exists, because the the destination is altered. So here is the corrected function that will allow it to work:

I spent some time debugging a silly problem:

In php >= 5, ftp_put() will apparently rewind to the start of the file regardless of the state you left it in before sending it to the $host.

I found this out because I wasn’t closing the file handle before using ftp_put(). Since I had just written to the file, the file pointer must have been located at the *bottom*.

I was sending a 0-byte file on php 4.2.2., but worked fine on php 5.

So, just a heads up, don’t forget to close those filehandles. Even though I was using the filename as the argument for ftp_put, it still needs to be closed.

I did not call rewind on the file handle, just fclose($file_h).

In case you aren’t aware. Some web hosting services do NOT allow outbound ftp unless you have a dedicated server account. A «shared» hosting account often doesn’t have this capability.

So if you can’t get your ftp, curl, or ssh remote file transfer functions to work, check with the host service and ask. You may have to upgrade your account.

Читайте также:  Css работа с графикой

I had a little trouble getting the ftp_put to work, because of that particular server. All variables and data parsed from the previous web form had to be retreived using $_POST, $_GET or $_FILES.

If you don’t know what you sent use phpinfo(); to display what the server thinks about your data.

so. when sending files using a form and PHP, make sure that all the data (text files etc. ) are retreived with $_POST, and files (smiley.png, compression.zip, etc. ) are retreived with $_FILES.

here’s what your start of a results.php file might look like:
$myName = $_POST [ ‘name’ ]; //This will copy the text into a variable
$myFile = $_FILES [ ‘file_name’ ]; // This will make an array out of the file information that was stored.
?>

Now when it comes to transmitting that information.

//where you want to throw the file on the webserver (relative to your login dir)

$destination_file = $destination_path . «img.jpg» ;

//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I’m dynamically creating new folders.

//Converts the array into a new string containing the path name on the server where your file is.

$upload = ftp_put ( $conn_id , $destination_file , $file , FTP_BINARY ); // upload the file
if (! $upload ) < // check upload status
echo «FTP upload of $destination_file has failed!» ;
> else echo «Uploaded $file to $conn_id as $destination_file » ;
>
?>

hope this is usefull ^_^

Источник

Php copy files ftp

For those who dont want to deal with handling the connection once created, here is a simple class that allows you to call any ftp function as if it were an extended method. It automatically puts the ftp connection into the first argument slot (as all ftp functions require).

public function __construct ( $url ) <
$this -> conn = ftp_connect ( $url );
>

public function __call ( $func , $a ) <
if( strstr ( $func , ‘ftp_’ ) !== false && function_exists ( $func )) <
array_unshift ( $a , $this -> conn );
return call_user_func_array ( $func , $a );
>else <
// replace with your own error handler.
die( » $func is not a valid FTP function» );
>
>
>

// Example
$ftp = new ftp ( ‘ftp.example.com’ );
$ftp -> ftp_login ( ‘username’ , ‘password’ );
var_dump ( $ftp -> ftp_nlist ());
?>

Upload file to server via ftp.

$ftp_server = «» ;
$ftp_user_name = «» ;
$ftp_user_pass = «» ;
$file = «» ; //tobe uploaded
$remote_file = «» ;

// 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» ;
exit;
> else <
echo «There was a problem while uploading $file \n» ;
exit;
>
// close the connection
ftp_close ( $conn_id );
?>

In example 2 above you may need to set the system to to use pasv to get a result ie:

$ftp = new ftp(‘ftp.example.com’);
$ftp->ftp_login(‘username’,’password’);
$ftp->ftp_pasv(TRUE);
var_dump($ftp->ftp_nlist());

syntax error in the above example, ftp_nlist requires a directory parameter:

$ftp->ftp_nlist(‘.’); // retrieve contents of current directory

Источник

Оцените статью