ftp.php 6.1 KB

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