百科狗-知识改变命运!
--

ftp_put() - ftp函数

乐乐1年前 (2023-11-21)阅读数 24#技术干货
文章标签模式

ftp_put()

(PHP 4, PHP 5, PHP 7)

上传文件到 FTP 服务器

说明

ftp_put(resource $ftp_stream,string $remote_file,string $local_file,int $mode[,int $startpos]): bool

ftp_put()函数用来上传由$local_file参数指定的文件到 FTP 服务器,上传后的位置由$remote_file指定。传输模式参数

参数

$ftp_stream

FTP 连接资源。

$remote_file

远程文件路径。

$local_file

ftp_put() - ftp函数

本地文件路径。

$mode

传送模式,只能为FTP_ASCII(文本模式)或FTP_BINARY(二进制模式)。

$startpos

返回值

成功时返回TRUE,或者在失败时返回FALSE

范例

ftp_put()实例

更新日志

版本说明
4.3.0增加$startpos参数。

参见

  • ftp_pasv()返回当前 FTP 被动模式是否打开
  • ftp_fput()上传一个已经打开的文件到 FTP 服务器
  • ftp_nb_fput()将文件存储到 FTP 服务器(非阻塞)
  • ftp_nb_put()存储一个文件至 FTP 服务器(non-blocking)
ftp_put() overwrites existing files. 
Again: trivial but not mentioned here.
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:

Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D
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();
}
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
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();
}
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();
}
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.
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 
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.
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
Here is the Code I am using for the same function with more flexibility in static code: 





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!
Took me forever to work out but this will work if you get the paths right!

Login to your site using an FTP client to retrieve the $ftp_root
Use something like getcwd() to get the $site root
-----FTP PUT contents (php 7.0) ---
$tmpFile = tmpfile();
fwrite($tmpFile, $contents);
rewind($tmpFile);
$tmpMetaData = stream_get_meta_data($tmpFile);
if (ftp_put($ftpObj, $remoteFile, $tmpMetaData['uri'], FTP_ASCII)) {
   echo "success";
}else {
    echo "fail"
}
fclose($tmpFile);
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.
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.
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. 
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:   Image: 
      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: 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...

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)