3

I was looking for a way to perform a number of tasks in PHP

  1. Get a file from another server
  2. Change the file name and extention
  3. Download the new file to the end user

I would prefer a method that acts as a proxy server type thing, but a file download would be fine

Thanks in advance

4 Answers 4

0

Try this

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

After you save rename the file with whatever name you need

Refer this

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

0

See the example at http://www.php.net/manual/en/function.curl-init.php

This grabs the data and outputs it straight to the browser, headers and all.

0

If you have allow_url_fopen set to true:

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

Else use 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);
0

I use something like this:

<?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);
    }
}
?>
1
  • thanks, exactly what i needed, is there a way i can show the end user a progress bar of some discription
    – BDMan32
    Mar 26, 2012 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.