簡體   English   中英

如何從其他服務器獲取文件並在PHP中重命名

[英]How to get a file from another server and rename it in PHP

我在尋找一種在PHP中執行許多任務的方法

  1. 從另一台服務器獲取文件
  2. 更改文件名和擴展名
  3. 將新文件下載到最終用戶

我更喜歡充當代理服務器類型的東西的方法,但是下載文件就可以了

提前致謝

嘗試這個

<?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/zh/ref.curl.php

參見http://www.php.net/manual/en/function.curl-init.php中的示例

這將獲取數據並將其直接輸出到瀏覽器,標題和所有內容。

如果將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);

我用這樣的東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM