ftp.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * 远程操作常用函数
  4. *
  5. *
  6. *
  7. */
  8. defined('InShopNC') or exit('Access Invalid!');
  9. /**
  10. * 通过FTP同步图片到远程服务器
  11. *
  12. * @param string $path 图片路径(upload/store/goods/4)
  13. * @param string $file 图片名称(含年月日2012/06/12/03f625d923bfb1ca84355007487ed68b.jpg)
  14. * @param boolean $ifdel 是否删除本地图片,目前淘宝导入的图片上传到远程时,不会删除本地图片
  15. * @return string 远程图片路径部分
  16. */
  17. function remote_ftp($path, $file, $ifdel = true)
  18. {
  19. ftpcmd('upload', $path.'/'.$file);
  20. $img_ext = explode(',', GOODS_IMAGES_EXT);
  21. foreach ($img_ext as $val) {
  22. if(!ftpcmd('error')) ftpcmd('upload', $path.'/'.str_ireplace('.', $val . '.', $file));
  23. }
  24. if(!ftpcmd('error')) {
  25. if ($ifdel){
  26. @unlink(BASE_PATH.'/'.$path.'/'.$file);
  27. foreach ($img_ext as $val) {
  28. @unlink(BASE_PATH.'/'.$path.'/'.str_ireplace('.', $val . '.', $file));
  29. }
  30. }
  31. return C('ftp_access_url').'/'.$path;
  32. }
  33. return false;
  34. }
  35. function ftpcmd($cmd, $arg1 = '')
  36. {
  37. import('libraries.ftp');
  38. static $ftp;
  39. $ftpon = C('ftp_open');
  40. if(!$ftpon) {
  41. return $cmd == 'error' ? -101 : 0;
  42. } elseif($ftp == null) {
  43. $ftp = & NcFtp::instance();
  44. }
  45. if(!$ftp->enabled) {
  46. return $ftp->error();
  47. } elseif($ftp->enabled && !$ftp->connectid) {
  48. $ftp->connect();
  49. }
  50. switch ($cmd) {
  51. case 'upload' : return $ftp->upload(BASE_PATH.'/'.$arg1, $arg1); break;
  52. case 'delete' : return $ftp->ftp_delete($arg1); break;
  53. case 'close' : return $ftp->ftp_close(); break;
  54. case 'error' : return $ftp->error(); break;
  55. case 'object' : return $ftp; break;
  56. default : return false;
  57. }
  58. }
  59. function getremotefile($file) {
  60. @set_time_limit(0);
  61. $file = $file.'?'.time().rand(1000, 9999);
  62. $str = @implode('', @file($file));
  63. if(!$str) {
  64. $str = dfsockopen($file);
  65. }
  66. return $str;
  67. }
  68. function dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE) {
  69. return _dfsockopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype, $allowcurl);
  70. }
  71. function _dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE) {
  72. $return = '';
  73. $matches = parse_url($url);
  74. $scheme = $matches['scheme'];
  75. $host = $matches['host'];
  76. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  77. $port = !empty($matches['port']) ? $matches['port'] : 80;
  78. if(function_exists('curl_init') && function_exists('curl_exec') && $allowcurl) {
  79. $ch = curl_init();
  80. $ip && curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: ".$host));
  81. curl_setopt($ch, CURLOPT_URL, $scheme.'://'.($ip ? $ip : $host).':'.$port.$path);
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  83. if($post) {
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. if($encodetype == 'URLENCODE') {
  86. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  87. } else {
  88. parse_str($post, $postarray);
  89. curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray);
  90. }
  91. }
  92. if($cookie) {
  93. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  94. }
  95. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  96. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  97. $data = curl_exec($ch);
  98. $status = curl_getinfo($ch);
  99. $errno = curl_errno($ch);
  100. curl_close($ch);
  101. if($errno || $status['http_code'] != 200) {
  102. return;
  103. } else {
  104. return !$limit ? $data : substr($data, 0, $limit);
  105. }
  106. }
  107. if($post) {
  108. $out = "POST $path HTTP/1.0\r\n";
  109. $header = "Accept: */*\r\n";
  110. $header .= "Accept-Language: zh-cn\r\n";
  111. $boundary = $encodetype == 'URLENCODE' ? '' : '; boundary='.trim(substr(trim($post), 2, strpos(trim($post), "\n") - 2));
  112. $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";
  113. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  114. $header .= "Host: $host:$port\r\n";
  115. $header .= 'Content-Length: '.strlen($post)."\r\n";
  116. $header .= "Connection: Close\r\n";
  117. $header .= "Cache-Control: no-cache\r\n";
  118. $header .= "Cookie: $cookie\r\n\r\n";
  119. $out .= $header.$post;
  120. } else {
  121. $out = "GET $path HTTP/1.0\r\n";
  122. $header = "Accept: */*\r\n";
  123. $header .= "Accept-Language: zh-cn\r\n";
  124. $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  125. $header .= "Host: $host:$port\r\n";
  126. $header .= "Connection: Close\r\n";
  127. $header .= "Cookie: $cookie\r\n\r\n";
  128. $out .= $header;
  129. }
  130. $fpflag = 0;
  131. if(!$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) {
  132. $context = array(
  133. 'http' => array(
  134. 'method' => $post ? 'POST' : 'GET',
  135. 'header' => $header,
  136. 'content' => $post,
  137. 'timeout' => $timeout,
  138. ),
  139. );
  140. $context = stream_context_create($context);
  141. $fp = @fopen($scheme.'://'.($ip ? $ip : $host).':'.$port.$path, 'b', false, $context);
  142. $fpflag = 1;
  143. }
  144. if(!$fp) {
  145. return '';
  146. } else {
  147. stream_set_blocking($fp, $block);
  148. stream_set_timeout($fp, $timeout);
  149. @fwrite($fp, $out);
  150. $status = stream_get_meta_data($fp);
  151. if(!$status['timed_out']) {
  152. while (!feof($fp) && !$fpflag) {
  153. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  154. break;
  155. }
  156. }
  157. $stop = false;
  158. while(!feof($fp) && !$stop) {
  159. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  160. $return .= $data;
  161. if($limit) {
  162. $limit -= strlen($data);
  163. $stop = $limit <= 0;
  164. }
  165. }
  166. }
  167. @fclose($fp);
  168. return $return;
  169. }
  170. }
  171. function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
  172. $fp = '';
  173. if(function_exists('fsockopen')) {
  174. $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
  175. } elseif(function_exists('pfsockopen')) {
  176. $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
  177. } elseif(function_exists('stream_socket_client')) {
  178. $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
  179. }
  180. return $fp;
  181. }
  182. ?>