Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't <em>have</em> to use curl, here is how you could do it using straight PHP.</p> <p>Note: It is possible to set headers, credentials etc. while using fopen() to fetch URL's using the <a href="http://www.php.net/manual/en/context.http.php" rel="nofollow">HTTP context options</a>.</p> <pre><code>&lt;?php class DownloadWithExtension { // array for caching mimetypes public $mimetypes = array(); // create mimetype cache when instantiated public function __construct() { // iterate through /etc/mime.types and create a hash with // mime-types as keys and the first extension as the value. foreach(preg_grep('/(^#)|(^\s+$)/', file('/etc/mime.types'), PREG_GREP_INVERT) as $line) { $minfo = preg_split('/\t+/', $line); if (count($minfo) &gt; 1) { $this-&gt;mimetypes[$minfo[0]] = trim(array_shift(explode(' ', $minfo[1]))); } } } // download $url and save as $prefix while automatically // determining appropriate extension. // @param $url - URL to download // @param $prefix - Filename to save to (without extension) // @return filename used public function get($url, $prefix) { $mimetype = NULL; $filename = NULL; $src = fopen($url, 'r'); if (! $src) { throw new Exception('Failed to open: ' . $url); } $meta = stream_get_meta_data($src); foreach($meta['wrapper_data'] as $header){ if (preg_match('/^content-type: ([^\s]+)/i', $header, &amp;$matches)) { $mimetype = $matches[1]; break; } } $extension = @$this-&gt;mimetypes[$mimetype]; // default to .bin if the mime-type could not be determined $filename = sprintf('%s.%s', $prefix, $extension ? $extension : 'bin'); $dst = fopen($filename, 'w'); if (! ($dst &amp;&amp; stream_copy_to_stream($src, $dst) &amp;&amp; fclose($src) &amp;&amp; fclose($dst))) { throw new Exception('An error occurred while saving the file!'); } return $filename; } } $d = new DownloadWithExtension(); $url = 'http://example.com?file=3838438'; $filename = $d-&gt;get($url, '/tmp/myfile'); print(sprintf("Saved %s as %s\n", $url, $filename)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload