当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP实现ftp上传文件示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-08 10:13:41 浏览: 评论:0 

这篇文章主要介绍了PHP实现ftp上传文件的方法,是非常实用的技巧,需要的朋友可以参考下。

FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例。希望对大家的PHP学习能带来一定的帮助。

主要代码如下:

  1. function make_directory($ftp_stream, $dir){ 
  2.   // if directory already exists or can be immediately created return true 
  3.   if ($this->ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true; 
  4.   // otherwise recursively try to make the directory 
  5.   if (!$this->make_directory($ftp_stream, dirname($dir))) return false; 
  6.   // final step to create the directory 
  7.   return ftp_mkdir($ftp_stream, $dir); 
  8. } 
  9.    
  10. function ftp_is_dir($ftp_stream, $dir){ 
  11.   // get current directory 
  12.   $original_directory = ftp_pwd($ftp_stream); 
  13.   // test if you can change directory to $dir 
  14.   // suppress errors in case $dir is not a file or not a directory 
  15.   if ( @ftp_chdir( $ftp_stream, $dir ) ) { 
  16.     // If it is a directory, then change the directory back to the original directory 
  17.     ftp_chdir( $ftp_stream, $original_directory ); 
  18.     return true; 
  19.   } else { 
  20.     return false; 
  21.   } 
  22. } 
  23.  
  24. $conn = ftp_connect("ftp地址") or die("Could not connect"); 
  25. ftp_login($conn,"ftpname","password"); 
  26. //利用ftp创建目录 
  27. make_directory($conn,$path); 
  28. //利用ftp选择进入目录 
  29. ftp_chdir($conn,$path); 
  30. //开始上传 
  31. if(ftp_put($conn,$info[0]['savename'],getcwd().$upload->savePath.$info[0]['savename'],FTP_BINARY)){ 
  32.  unlink(getcwd().$upload->savePath.$info[0]['savename']); 
  33. } 
  34. ftp_close($conn); 
  35. //注意上传端的ftp权限设置 

感兴趣的朋友可以测试运行或改写本文所述代码,加深理解的同时可以让代码功能更加完善。

Tags: PHP上传文件

分享到: