当前位置:Gxlcms > PHP教程 > PHP简单实现用FTP类上传文件视频等的方法

PHP简单实现用FTP类上传文件视频等的方法

时间:2021-07-01 10:21:17 帮助过:25人阅读

本篇文章主要介绍PHP简单实现用FTP类上传文件视频等的方法,感兴趣的朋友参考下,希望对大家有所帮助。

文件1:ftp.php

  1. <?php
  2. /**
  3. * 仿写CodeIgniter的FTP类
  4. * FTP基本操作:
  5. * 1) 登陆; connect
  6. * 2) 当前目录文件列表; filelist
  7. * 3) 目录改变; chgdir
  8. * 4) 重命名/移动; rename
  9. * 5) 创建文件夹; mkdir
  10. * 6) 删除; delete_dir/delete_file
  11. * 7) 上传; upload
  12. * 8) 下载 download
  13. *
  14. * @author quanshuidingdang
  15. */
  16. class Ftp {
  17. private $hostname
  18. = '';
  19. private $username
  20. = '';
  21. private $password
  22. = '';
  23. private $port
  24. = 21;
  25. private $passive
  26. = TRUE;
  27. private $debug
  28. = TRUE;
  29. private $conn_id
  30. = FALSE;
  31. /**
  32. * 构造函数
  33. *
  34. * @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
  35. */
  36. public function __construct($config = array()) {
  37. if(count($config) > 0) {
  38. $this->_init($config);
  39. }
  40. }
  41. /**
  42. * FTP连接
  43. *
  44. * @access public
  45. * @param array 配置数组
  46. * @return boolean
  47. */
  48. public function connect($config = array()) {
  49. if(count($config) > 0) {
  50. $this->_init($config);
  51. }
  52. //判断是否打开了ftp连接
  53. if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
  54. if($this->debug === TRUE) {
  55. $this->_error("ftp_unable_to_connect");
  56. }
  57. return FALSE;
  58. }
  59. //判断是否登录成功
  60. if( ! $this->_login()) {
  61. if($this->debug === TRUE) {
  62. $this->_error("ftp_unable_to_login");
  63. }
  64. return FALSE;
  65. }
  66. //判断是否开启FTP被动模式
  67. if($this->passive === TRUE) {
  68. ftp_pasv($this->conn_id, TRUE);
  69. }
  70. return TRUE;
  71. }
  72. /**
  73. * 目录改变
  74. *
  75. * @access public
  76. * @param string 目录标识(ftp)
  77. * @param boolean
  78. * @return boolean
  79. */
  80. public function chgdir($path = '', $supress_debug = FALSE) {
  81. if($path == '' OR ! $this->_isconn()) {
  82. return FALSE;
  83. }
  84. $result = @ftp_chdir($this->conn_id, $path);
  85. if($result === FALSE) {
  86. if($this->debug === TRUE AND $supress_debug == FALSE) {
  87. $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
  88. }
  89. return FALSE;
  90. }
  91. return TRUE;
  92. }
  93. /**
  94. * 目录生成
  95. *
  96. * @access public
  97. * @param string 目录标识(ftp)
  98. * @param int 文件权限列表
  99. * @return boolean
  100. */
  101. public function mkdir($path = '', $permissions = NULL) {
  102. if($path == '' OR ! $this->_isconn()) {
  103. return FALSE;
  104. }
  105. $result = @ftp_mkdir($this->conn_id, $path);
  106. if($result === FALSE) {
  107. if($this->debug === TRUE) {
  108. $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
  109. }
  110. return FALSE;
  111. }
  112. if( ! is_null($permissions)) {
  113. $this->chmod($path,(int)$permissions);
  114. }
  115. return TRUE;
  116. }
  117. /**
  118. * 上传
  119. *
  120. * @access public
  121. * @param string 本地目录标识
  122. * @param string 远程目录标识(ftp)
  123. * @param string 上传模式 auto || ascii
  124. * @param int 上传后的文件权限列表
  125. * @return boolean
  126. */
  127. public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
  128. if( ! $this->_isconn()) {
  129. return FALSE;
  130. }
  131. //判断本地文件是否存在
  132. if( ! file_exists($localpath)) {
  133. if($this->debug === TRUE) {
  134. $this->_error("ftp_no_source_file:".$localpath);
  135. }
  136. return FALSE;
  137. }
  138. //判断上传模式
  139. if($mode == 'auto') {
  140. //获取文件后缀类型
  141. $ext = $this->_getext($localpath);
  142. //根据后缀类型决定上传模式是 FTP_ASCII(文本模式) 还是 FTP_BINARY(二进制模式);
  143. $mode = $this->_settype($ext);
  144. }
  145. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  146. //上传
  147. $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
  148. //判断上传是否成功
  149. if($result === FALSE) {
  150. if($this->debug === TRUE) {
  151. $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
  152. }
  153. return FALSE;
  154. }
  155. //判断是否需要改写文件权限
  156. if( ! is_null($permissions)) {
  157. $this->chmod($remotepath,(int)$permissions);
  158. }
  159. return TRUE;
  160. }
  161. /**
  162. * 下载
  163. *
  164. * @access public
  165. * @param string 远程目录标识(ftp)
  166. * @param string 本地目录标识
  167. * @param string 下载模式 auto || ascii
  168. * @return boolean
  169. */
  170. public function download($remotepath, $localpath, $mode = 'auto') {
  171. if( ! $this->_isconn()) {
  172. return FALSE;
  173. }
  174. if($mode == 'auto') {
  175. $ext = $this->_getext($remotepath);
  176. $mode = $this->_settype($ext);
  177. }
  178. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  179. $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
  180. if($result === FALSE) {
  181. if($this->debug === TRUE) {
  182. $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
  183. }
  184. return FALSE;
  185. }
  186. return TRUE;
  187. }
  188. /**
  189. * 重命名/移动
  190. *
  191. * @access public
  192. * @param string 远程目录标识(ftp)
  193. * @param string 新目录标识
  194. * @param boolean 判断是重命名(FALSE)还是移动(TRUE)
  195. * @return boolean
  196. */
  197. public function rename($oldname, $newname, $move = FALSE) {
  198. if( ! $this->_isconn()) {
  199. return FALSE;
  200. }
  201. $result = @ftp_rename($this->conn_id, $oldname, $newname);
  202. if($result === FALSE) {
  203. if($this->debug === TRUE) {
  204. $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
  205. $this->_error($msg);
  206. }
  207. return FALSE;
  208. }
  209. return TRUE;
  210. }
  211. /**
  212. * 删除文件
  213. *
  214. * @access public
  215. * @param string 文件标识(ftp)
  216. * @return boolean
  217. */
  218. public function delete_file($file) {
  219. if( ! $this->_isconn()) {
  220. return FALSE;
  221. }
  222. $result = @ftp_delete($this->conn_id, $file);
  223. if($result === FALSE) {
  224. if($this->debug === TRUE) {
  225. $this->_error("ftp_unable_to_delete_file:file[".$file."]");
  226. }
  227. return FALSE;
  228. }
  229. return TRUE;
  230. }
  231. /**
  232. * 删除文件夹
  233. *
  234. * @access public
  235. * @param string 目录标识(ftp)
  236. * @return boolean
  237. */
  238. public function delete_dir($path) {
  239. if( ! $this->_isconn()) {
  240. return FALSE;
  241. }
  242. //对目录宏的'/'字符添加反斜杠'\'
  243. $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
  244. //获取目录文件列表
  245. $filelist = $this->filelist($path);
  246. if($filelist !== FALSE AND count($filelist) > 0) {
  247. foreach($filelist as $item) {
  248. //如果我们无法删除,那么就可能是一个文件夹
  249. //所以我们递归调用delete_dir()
  250. if( ! @delete_file($item)) {
  251. $this->delete_dir($item);
  252. }
  253. }
  254. }
  255. //删除文件夹(空文件夹)
  256. $result = @ftp_rmdir($this->conn_id, $path);
  257. if($result === FALSE) {
  258. if($this->debug === TRUE) {
  259. $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
  260. }
  261. return FALSE;
  262. }
  263. return TRUE;
  264. }
  265. /**
  266. * 修改文件权限
  267. *
  268. * @access public
  269. * @param string 目录标识(ftp)
  270. * @return boolean
  271. */
  272. public function chmod($path, $perm) {
  273. if( ! $this->_isconn()) {
  274. return FALSE;
  275. }
  276. //只有在PHP5中才定义了修改权限的函数(ftp)
  277. if( ! function_exists('ftp_chmod')) {
  278. if($this->debug === TRUE) {
  279. $this->_error("ftp_unable_to_chmod(function)");
  280. }
  281. return FALSE;
  282. }
  283. $result = @ftp_chmod($this->conn_id, $perm, $path);
  284. if($result === FALSE) {
  285. if($this->debug === TRUE) {
  286. $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
  287. }
  288. return FALSE;
  289. }
  290. return TRUE;
  291. }
  292. /**
  293. * 获取目录文件列表
  294. *
  295. * @access public
  296. * @param string 目录标识(ftp)
  297. * @return array
  298. */
  299. public function filelist($path = '.') {
  300. if( ! $this->_isconn()) {
  301. return FALSE;
  302. }
  303. return ftp_nlist($this->conn_id, $path);
  304. }
  305. /**
  306. * 关闭FTP
  307. *
  308. * @access public
  309. * @return boolean
  310. */
  311. public function close() {
  312. if( ! $this->_isconn()) {
  313. return FALSE;
  314. }
  315. return @ftp_close($this->conn_id);
  316. }
  317. /**
  318. * FTP成员变量初始化
  319. *
  320. * @access private
  321. * @param array 配置数组
  322. * @return void
  323. */
  324. private function _init($config = array()) {
  325. foreach($config as $key => $val) {
  326. if(isset($this->$key)) {
  327. $this->$key = $val;
  328. }
  329. }
  330. //特殊字符过滤
  331. $this->hostname = preg_replace('|.+?://|','',$this->hostname);
  332. }
  333. /**
  334. * FTP登陆
  335. *
  336. * @access private
  337. * @return boolean
  338. */
  339. private function _login() {
  340. return @ftp_login($this->conn_id, $this->username, $this->password);
  341. }
  342. /**
  343. * 判断con_id
  344. *
  345. * @access private
  346. * @return boolean
  347. */
  348. private function _isconn() {
  349. if( ! is_resource($this->conn_id)) {
  350. if($this->debug === TRUE) {
  351. $this->_error("ftp_no_connection");
  352. }
  353. return FALSE;
  354. }
  355. return TRUE;
  356. }
  357. /**
  358. * 从文件名中获取后缀扩展
  359. *
  360. * @access private
  361. * @param string 目录标识
  362. * @return string
  363. */
  364. private function _getext($filename) {
  365. if(FALSE === strpos($filename, '.')) {
  366. return 'txt';
  367. }
  368. $extarr = explode('.', $filename);
  369. return end($extarr);
  370. }
  371. /**
  372. * 从后缀扩展定义FTP传输模式 ascii 或 binary
  373. *
  374. * @access private
  375. * @param string 后缀扩展
  376. * @return string
  377. */
  378. private function _settype($ext) {
  379. $text_type = array (
  380. 'txt',
  381. 'text',
  382. 'php',
  383. 'phps',
  384. 'php4',
  385. 'js',
  386. 'css',
  387. 'htm',
  388. 'html',
  389. 'phtml',
  390. 'shtml',
  391. 'log',
  392. 'xml'
  393. );
  394. return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
  395. }
  396. /**
  397. * 错误日志记录
  398. *
  399. * @access prvate
  400. * @return boolean
  401. */
  402. private function _error($msg) {
  403. return @file_put_contents('ftp_err.log', "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND);
  404. }
  405. }
  406. /*End of file ftp.php*/
  407. /*Location /Apache Group/htdocs/ftp.php*/

文件2:ftp_demo.php

  1. <?php
  2. require_once('ftp.php');
  3. $config = array(
  4. 'hostname' => '101.64.183.92', //服务器地址
  5. 'username' => 'ftpadminuser', //FTP登录账号
  6. 'password' => 'admin_user', //FTP登录密码
  7. 'port' => 2112 //端口号
  8. );
  9. $ftp = new Ftp();
  10. //连接
  11. $ftp->connect($config);
  12. //上传第一个参数是本地文件名,第二个参数是FTP文件名
  13. $rs=$ftp->upload('jsyh.flv','jsyh.flv');
  14. if($rs){
  15. echo 1;
  16. }
  17. // $ftp->download('ftp_upload.log','ftp_download.log');

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP查询大量数据内存耗尽问题的解决方法

php PATH_SEPARATOR判断当前服务器系统类型实例

php+mongodb判断坐标是否在指定多边形区域内的实例

以上就是PHP简单实现用FTP类上传文件视频等的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行