©
本文档使用
php.cn手册 发布
(PHP 4, PHP 5, PHP 7)
ftp_put — 上传文件到 FTP 服务器
$ftp_stream
, string $remote_file
, string $local_file
, int $mode
[, int $startpos
] ) ftp_put() 函数用来上传由 local_file
参数指定的文件到 FTP 服务器,上传后的位置由
remote_file
指定。传输模式参数
ftp_stream
FTP 连接资源。
remote_file
远程文件路径。
local_file
本地文件路径。
mode
传送模式,只能为 FTP_ASCII
(文本模式)或 FTP_BINARY
(二进制模式)。
startpos
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 ftp_put() 实例
<?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 );
?>
版本 | 说明 |
---|---|
4.3.0 |
增加 startpos 参数。
|
[#1] fabiogoncalves at live dot co dot uk [2015-02-14 12:14:11]
I was trying to upload WAV files using FTP_ASCII and the file was full of noise after uploading.
So for WAV files use FTP_BINARY.
Hope it helps.
[#2] donjajo at 2netlodge dot com [2013-09-15 09:08:27]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple FTP Upload Script using PHP by 2netlodge</title>
</head>
<body>
<?php
if(isset($_POST['ftp']))
{
$ftp = $_POST['ftp'];
$username = $_POST['username'];
$pwd = $_POST['pwd'];
$filename = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];
$d = $_POST['des'];
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$d.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="ftp" placeholder="FTP link"/><br/>
<input type="text" name="username" placeholder="Username"/><br/>
<input type="password" name="pwd" placeholder="Password"/><br/>
<input type="file" name="file" /><br/>
<input type="text" name="des" placeholder="Destination" /><br/>
<br/><input type="submit" value="Upload"/></form>
</body>
</html>
[#3] Anonymous [2012-08-24 12:59:21]
If you are getting the very helpful warnings...
"Opening BINARY mode data connection" or
"Opening ASCII mode data connection"
... and have tried ftp_pasv, ftp_alloc, switching between FTP_ASCII or FTP_BINARY and blaming the FTP provider (like I did), try using ftp_close then ftp_connect to reset your FTP connection. Worked for me!
[#4] Anonymous [2012-03-13 10:28:31]
Hi !
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...
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.
[#5] lourspolair at banquiz dot net [2010-06-07 03:05:53]
Hi,
I try to upload one text file represente CSV with tab separator thru FTP connection.
i'm using :
ftp_put($connect_id, $fileFullName, $localFile, FTP_ASCII);
everything goes fine, but txt file in destination server get white line between dataline...
i try :
ftp_put($connect_id, $fileFullName, $localFile, FTP_BINARY);
and everything goes fine.
so, lets do it in binary mode.
[#6] lousrpolair at banquiz dot net [2010-06-07 03:05:44]
Hi,
I try to upload one text file represente CSV with tab separator thru FTP connection.
i'm using :
ftp_put($connect_id, $fileFullName, $localFile, FTP_ASCII);
everything goes fine, but txt file in destination server get white line between dataline...
i try :
ftp_put($connect_id, $fileFullName, $localFile, FTP_BINARY);
and everything goes fine.
so, lets do it in binary mode.
[#7] JDD [2010-03-12 18:22:08]
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.
[#8] Svinto [2010-03-01 04:58:25]
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.
[#9] bintjes at gmail dot com [2009-07-02 01:51:47]
I've seen two notes about a "ftp_copy" function but i think there's a misinterpretation about what an "ftp_copy" function should do. For me , it should be something like an ftp_rename that would keep the orginal file and clone it somewhere else on the same ftp server, as for them they consider its purpose is to copy a local file to a distant ftp ..well .. as in FTP protocol there's no such thing as an FTP COPY command anyway, i think you're free to interpret it as you want.
So here's my solution using ftp_put and ftp_get ..
<?php
// bool ftp_copy ( resource $ftp_stream , string $initialpath, string $newpath, string $imagename )
function ftp_copy($conn_distant , $pathftp , $pathftpimg ,$img){
// on recupere l'image puis on la repose dans le nouveau folder
if(ftp_get($conn_distant, TEMPFOLDER.$img, $pathftp.'/'.$img ,FTP_BINARY)){
if(ftp_put($conn_distant, $pathftpimg.'/'.$img ,TEMPFOLDER.$img , FTP_BINARY)){
unlink(TEMPFOLDER.$img) ;
} else{
return false;
}
}else{
return false ;
}
return true ;
}
?>
[#10] Anonymous [2009-04-26 07:51:01]
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:
<?php
ftp_pasv($resource, true);
?>
Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D
[#11] Kev [2009-03-31 20:36:14]
Got this cryptic error
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);
you have to put
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
[#12] emailfire at gmail dot com [2008-05-20 03:45:28]
Took me forever to work out but this will work if you get the paths right!
<?php
function ftp_copy($file) {
global $conn_id;
$ftp_root = '/public_html/';
$site_root = '/home/usr/public_html/';
return ftp_put($conn_id, $ftp_root . $file, $site_root . $file, FTP_BINARY);
}
?>
Login to your site using an FTP client to retrieve the $ftp_root
Use something like getcwd() to get the $site root
[#13] florian dot ember at gmail dot com [2008-05-17 08:08:10]
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).
<?php
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;
}
?>
[#14] Aditya P Bhatt (adityabhai at gmail dot com) [2008-03-26 03:18:47]
Here is the Code I am using for the same function with more flexibility in static code:
<?php
$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("<span style='color:#FF0000'><h2>Couldn't connect to $ftp_server</h2></span>"); // set up basic connection
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("<span style='color:#FF0000'><h2>You do not have access to this ftp server!</h2></span>"); // 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 "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}
$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY); // upload the file
if (!$upload) { // check upload status
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo "<span style='color:#339900'><h2>Uploading $name Completed Successfully!</h2></span><br /><br />";
}
ftp_close($conn_id); // close the FTP stream
?>
[#15] dherran at gmail dot com [2007-12-17 12:01:29]
If you are moving files from one folder to another inside the same server, the "local file" field has to be indicated in a relative path according to the location of the script running the ftp_put() function.
For example, your function is running on: /public_html/do_ftp.php and you want to move /public_html/products.php to /public_html/backup/products.php
The correct way to build the function would be:
ftp_put($ftp_id, '/public_html/backup/products.php', 'products.php', FTP_ASCII);
After having headaches for 2 days trying to make this function work using absolute paths in both fields, I finally found the right way to use it. I hope it helps someone. Excuse my english, it isn't my native language.
[#16] Ari [2007-07-16 23:30:59]
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.
[#17] todd [2007-05-11 23:26:41]
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.
<?php
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.
[#18] koen dot depoorter at gmail dot com [2007-02-11 16:24:39]
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();
koen
[#19] tom pittlik [2005-12-20 02:25:04]
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.
[#20] ben at over dot bomb [2005-08-25 05:28:58]
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).
[#21] Icy_Tiger_2000 at yahoo dot com dot au [2005-07-16 01:35:36]
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:
<?PHP
$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...
<?PHP
$destination_path = "src/bin/";
//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.
$file = $myFile['tmp_name'];
//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 ^_^
[#22] webmaster at sweetphp dot com [2005-07-08 17:54:00]
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:
function ftp_putAll($conn_id, $src_dir, $dst_dir) {
$d = dir($src_dir);
while($file = $d->read()) { // do this for each file in the directory
if ($file != "." && $file != "..") { // to prevent an infinite loop
if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
if (!@ftp_nlist($conn_id, $dst_dir."/".$file)) {
ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
}
ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
} else {
$upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
}
}
}
$d->close();
}
[#23] jrisken at mn dot rr dot com [2005-06-29 06:32:23]
This solution to a common problem is implied elsewhere, but I thought it might be useful to put it all in one place (since I spent hours piecing it together!)
Sometimes a web host will open PHP sessions with a user of 'nobody'. Files created by this user may not have the correct permissions to allow management of those files by the actual owner of the site. The following script allows the actual owner to open access to a directory so that 'nobody' can create a file using fopen(). Then using the handle created by 'nobody', the ftp_fput() command saves the file with the correct owner. The file 'nobody' created is discarded.
<?php
$connection = ftp_connect($ftpServer);
ftp_login($connection, $ftpUser, $ftpPass);
ftp_chdir($connection, $ftpDir);
// open the directory so that 'nobody' can create a temporary file
ftp_site($cn, "CHMOD 777 $ftpDir");
$new="tempFile";
@unlink($new); // just in case
$handle=fopen($new,"x");
chmod($new,0777);
fputs($handle,"a bunch of stuff...");
fclose($handle); // have to rewind
$handle=fopen("tempFile","r");
ftp_fput($connection, "finalFile", $handle, FTP_ASCII);
fclose($handle);
unlink($new);
// remove open access to the directory
ftp_site($connection, "CHMOD 755 $ftpDir")
ftp_close($connection);
?>
[#24] herok at atwatcher dot com [2004-06-17 10:59:06]
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.
[#25] Saeven [2003-09-23 21:43:29]
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
[#26] lucas at rufy dot com [2003-08-04 01:45:00]
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
function ftp_putAll($conn_id, $src_dir, $dst_dir) {
$d = dir($src_dir);
while($file = $d->read()) { // do this for each file in the directory
if ($file != "." && $file != "..") { // to prevent an infinite loop
if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
}
ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
} else {
$upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
}
}
}
$d->close();
}
[#27] darian lassan at yahoo de [2003-02-05 04:06:34]
ftp_put() overwrites existing files.
Again: trivial but not mentioned here.
[#28] oheil at ecc-gmbh dot de [2002-09-25 05:43:16]
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.
Example:
----------------------------------------------------------------
$conn_id = ftp_connect("server_adress");
...
$src_dir = "/from";
$dst_dir = "/to";
ftp_copy($src_dir, $dst_dir);
...
ftp_close($conn_id)
Function: ftp_copy()
----------------------------------------------------------------
function ftp_copy($src_dir, $dst_dir) {
global $conn_id;
$d = dir($src_dir);
while($file = $d->read()) {
if ($file != "." && $file != "..") {
if (is_dir($src_dir."/".$file)) {
if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
ftp_mkdir($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);
}
}
}
$d->close();
}
[#29] kiwo1 at yahoo dot com [2002-08-28 10:05:45]
Friends,
If you wanna upload files from your harddisk by a form to a specified ftp this sample can help you...
First of all create the form:
<html>
<body marginwidth=4 marginheight=4 topmargin=4 leftmargin=4 bgcolor=white vlink="#0000ff" link="#0000ff">
<form name="Attachments" method=POST action="sendimage.php" enctype="multipart/form-data">
<input type=hidden name=box value="">
<tr>
<td nowrap width="1%"> <b>Image:</b></td>
<td colspan=2>
<input type=file name=source_file size=20> <br>
</td>
</tr>
<input type=submit name=btnSubmit value=Submit size=20 style="border: 1px solid #0000FF"></form>
</body>
</html>
The critical point in this form is the usage of enctype="multipart/form-data"
If you don't use this part your upload operations won't work.
Then u must create sendimage.php as follows:
<?php
$ftp_server='190.148.20.201';//serverip
$conn_id = ftp_connect($ftp_server);
// login with username and password
$user="username";
$passwd="*****";
$login_result = ftp_login($conn_id, $user, $passwd);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
die;
} else {
echo "<br>Connected to $ftp_server, for user $user<br>";
}
//directorylike /www.velibaba.com/images
ftp_chdir($conn_id, "www.velibab.com");
ftp_chdir($conn_id, "compimages");
//$destination_file=ftp_pwd($conn_id);
$destination_file="x.jpg";
echo ("<br>");
print $destination_file;
echo ("<br>");
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
In this example code $source_file is the path of the file in your disk, and destination file is the name of the uploaded file in ftpserver.
In this code I use ftp_chdir to give the path of the
uploaded file within ftpserver..
For your questions about all categories of PHP my email:kiwo1@yahoo.com
c u...
[#30] clambert at whitecrown dot net [2001-06-29 19:24:15]
When using versions of PHP below 4.04b4, the ftp_put command doesn't work on NT/IIS4. Most of the other functions will work, but there was a bug while trying to send upstream data to an IIS webserver.