开发者

how to download file from site (php)

开发者 https://www.devze.com 2023-01-06 15:42 出处:网络
withought ftp. from site to my hosting fo开发者_JAVA百科lder. how i can do that?You can download from a foreign server and write to the filesystem (if you have permissions for the current directory,

withought ftp.

from site to my hosting fo开发者_JAVA百科lder. how i can do that?


You can download from a foreign server and write to the filesystem (if you have permissions for the current directory, otherwise you could write to /tmp or wherever you have permissions).

$file = 'test.jpg';
file_put_contents($file, 
    file_get_contents("http://example.com/" . $file)
 );


You can also use cURL for this, which might be useful if the PHP setting allow_url_fopen is not enabled:

<?php

function fetch_url($url, $output_file) {
    $stream = fopen($output_file, 'w');
    if ($stream === false) {
        throw new Exception('Cannot write to file');
    }

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FILE, $stream);
    $result = curl_exec($curl);

    curl_close($curl);
    fclose($stream);

    if ($result === false) {
        throw new Exception('cURL Error: ' . curl_error($curl));
    }
}

fetch_url('http://www.example.com', '/some/file');


You can use file_get_contents, php is supposed to support network streams; but your folder must be accessible from the outside.

0

精彩评论

暂无评论...
验证码 换一张
取 消