如何从另一台服务器获取文件并在PHP中重命名

如何从另一台服务器获取文件并在PHP中重命名,php,file-upload,download,Php,File Upload,Download,我正在寻找一种在PHP中执行许多任务的方法 从另一台服务器获取文件 更改文件名和扩展名 将新文件下载到最终用户 我更喜欢代理服务器类型的方法,但文件下载就可以了 提前感谢试试这个 <?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, C

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

  • 从另一台服务器获取文件
  • 更改文件名和扩展名
  • 将新文件下载到最终用户
  • 我更喜欢代理服务器类型的方法,但文件下载就可以了

    提前感谢

    试试这个

    <?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);
    ?>
    
    
    
    保存后,请使用所需的名称重命名文件

    参考此

    参见


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

    如果已将allow\u url\u fopen设置为true:

     $url = 'http://example.com/image.php';
     $img = '/my/folder/flower.gif';
     file_put_contents($img, file_get_contents($url));
    
    否则请使用卷曲:

     $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);
    

    我用这样的方式:

    <?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);
        }
    }
    ?>
    

    谢谢,这正是我所需要的,有什么方法可以向最终用户显示某个描述的进度条吗