hi all....
please help me..... i want to download the files from server using php code.

any help tnx in advnc........

Dani AI

Generated

If you mean letting a user download a file from your server to their computer, you do not need cURL. Serve the file through a PHP endpoint that sends the right headers and streams the file. This lets you enforce access control, hide real paths, and prevent path traversal.

Here is a minimal, safe download.php that streams a file from a known directory. It sets Content-Type, Content-Disposition (forces download), and Content-Length, then streams with low memory usage:

<?php
// download.php?f=report.pdf
$baseDir = __DIR__ . '/files/';
$requested = basename($_GET['f'] ?? '');
$path = realpath($baseDir . $requested);
$baseReal = realpath($baseDir);

if (!$requested || !$path || strpos($path, $baseReal) !== 0 || !is_file($path)) {
    http_response_code(404);
    exit('File not found');
}

$mime = 'application/octet-stream';
if (function_exists('finfo_open')) {
    $fi = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($fi, $path) ?: $mime;
    finfo_close($fi);
}

if (ob_get_level()) { ob_end_clean(); }
header('Content-Type: ' . $mime);
header('Content-Length: ' . filesize($path));
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: private, no-store, max-age=0');
flush();

$fp = fopen($path, 'rb');
fpassthru($fp);
exit;

Tips:

  • Keep downloadable files outside the web root and gate access with auth checks before sending headers.
  • For very large files, offload to the web server using X-Sendfile (Apache) or X-Accel-Redirect (Nginx).
  • See PHP docs for header, fpassthru, and the Fileinfo extension for MIME detection at finfo_file. For offloading, check mod_xsendfile and Nginx X-Accel-Redirect.

Recommended Answers

All 6 Replies

Hi,

Do you mean from other server to your server?, or from server to your computer?

You can use Curl to download file from web using php

function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
  }

pass url to this function and download contents. alternatively you can use file reader/writer

 private function downloadFile ($url, $path) {

      $newfname = $path;
      $file = fopen ($url, "rb");
      if ($file) {
        $newf = fopen ($newfname, "wb");

        if ($newf)
        while(!feof($file)) {
          fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
        }
      }

      if ($file) {
        fclose($file);
      }

      if ($newf) {
        fclose($newf);
      }
     } 

I hope so that it was helped solve your problem.

tnx 4 ur reply veedeoo...
i mean from the server to my computer...

You can get around allow_url_fopen restrictions by using fsockopen. Here's a (rudimentary) implementation:

function fsock_get_contents($url) {
    $fp = fsockopen($url, 80, $errno, $errstr, 20);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
        return false;
    } else {
        $out = "GET / HTTP/1.1\r\n";
        $out .= "Host: " . parse_url($url, PHP_URL_HOST) . "\r\n";
        $out .= "Connection: Close\r\n\r\n";

        $contents = '';
        fwrite($fp, $out);
        while (!feof($fp)) {
            $contents .= fgets($fp, 128);
        } fclose($fp);
        return $contents;
    }
}

echo fsock_get_contents('www.google.com');

Hope this helps you!!

tnx.... veedeoo & allenhill99 for helping me....
finally i got a simple trick for that.. and it works for me....
here given is what i got....
<a href="<?php echo stripslashes($row['resume']); ?>"> Download Resume </a>

like this it is easy to download the document files.... once again tnx 2 all...

thanks for all. it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.