【问题标题】:How to get a file from another server and rename it in PHP如何从另一台服务器获取文件并在 PHP 中重命名
【发布时间】:2012-03-26 04:51:22
【问题描述】:

我正在寻找一种在 PHP 中执行许多任务的方法

  1. 从其他服务器获取文件
  2. 更改文件名和扩展名
  3. 将新文件下载给最终用户

我更喜欢充当代理服务器类型的方法,但文件下载就可以了

提前致谢

【问题讨论】:

    标签: php file-upload download


    【解决方案1】:

    试试这个

    <?php
        $url  = 'http://www.example.com/a-large-file.zip';
        $path = '/path-to-file/a-large-file.zip';
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $data = curl_exec($ch);
    
        curl_close($ch);
    
        file_put_contents($path, $data);
    ?>
    

    保存后用你需要的任何名称重命名文件

    参考这个

    http://www.php.net/manual/en/ref.curl.php

    【讨论】:

      【解决方案2】:

      查看http://www.php.net/manual/en/function.curl-init.php的示例

      这会抓取数据并将其直接输出到浏览器、标题和所有内容。

      【讨论】:

        【解决方案3】:

        如果您将 allow_url_fopen 设置为 true:

         $url = 'http://example.com/image.php';
         $img = '/my/folder/flower.gif';
         file_put_contents($img, file_get_contents($url));
        

        其他使用 cURL:

         $ch = curl_init('http://example.com/image.php');
         $fp = fopen('/my/folder/flower.gif', 'wb');
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_exec($ch);
         curl_close($ch);
         fclose($fp);
        

        【讨论】:

          【解决方案4】:

          我使用这样的东西:

          <?php
          $url  = 'http://www.some_url.com/some_file.zip';
          $path = '/path-to-your-file/your_filename.your_ext';
          
          function get_some_file($url, $path){
              if(!file_exists ( $path )){
                  $fp = fopen($path, 'w+');
                  fwrite($fp, file_get_contents($url));
                  fclose($fp);
              }
          }
          ?>
          

          【讨论】:

          • 谢谢,正是我需要的,有没有办法可以向最终用户显示一些描述的进度条
          猜你喜欢
          • 2011-12-12
          • 1970-01-01
          • 2018-06-27
          • 1970-01-01
          • 1970-01-01
          • 2021-06-22
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          相关资源
          最近更新 更多