The following code works without errors on my local server but when I put it up on my hosted website, I get a "Failed - Network Error" message immediately.

The way this works is that a button is pressed on a download page to select what file to download. Here's a sample of that code:

<div class="part__head">Get the "<?php echo "{$srcname}";?>"</div> 
            <div class="part__head">Source file (size: <?php echo "{$srcsize}";?>)</div>
               <a href="download/mydloader.php?f=4"><center><img src="images/us_download.png" style="height:35px; width:144px; margin-bottom:.5rem;"  alt="download source code"></center> </a>

That code passes the id of the file in the database to mydloader.php. Here's the code of mydloader.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

function mydloader($filenumber=NULL){
    require '../../php/PDO_Connection_Select.php';  
    require '../../php/GetUserIpAddr.php';

    $ip = GetUserIpAddr();
    if (!$pdo = PDOConnect("foxclone_data"))
    {
        exit("unable to connect to database");
    }

   $test = $pdo->query("SELECT `filename` FROM files WHERE id = $filenumber");
   $filename = $test->fetchColumn();

  if (!file_exists($filename)) {
        exit("Requested file ($file) does not exist");
      }
  else {

        $file=basename($filename);
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        $size = filesize($filename);

    //Process file for download
        ob_clean();
        header('Content-Description: File Transfer');
        header('Content-Type: ' . mime_content_type($filename));
        header('Content-Disposition: attachment; filename='.$filename);
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($filename); // Download the file
        flush();
        exit;
    }
}
mydloader($_GET["f"]);      

mydloader.php is located in the same folder as the files to be downloaded, so there shouldn't be a path issue. Can somebody please explain why I'm getting an immediate "Failed- Network Error" or point to an issue in my code?

Dani AI

Generated

A concise expert summary and practical checklist based on the thread.

The original failure pattern — working locally but an immediate "Failed - Network Error" on the hosted site — strongly indicates a server-side layer (compression/proxy/security module) modifying or aborting the HTTP response rather than a simple PHP syntax bug. resolved the fatal error by changing server settings, and the remaining symptom (browsers not showing file size) is what you get when the server does not deliver a reliable Content-Length header.

What to check next

  • Inspect the actual response headers (Browser DevTools Network tab or curl -I) and look for Content-Length, Transfer-Encoding, and Content-Encoding. If Transfer-Encoding: chunked or a compression encoding is present, the browser will not show a size.
  • Check PHP error logs and ensure there is no PHP warning, fatal, or early output (including UTF-8 BOM or stray whitespace in included files) emitted before headers — any output will corrupt header delivery. Removing closing PHP tags from pure-PHP include files is a safe practice.
  • Make sure the byte count you advertise matches the bytes actually sent. If the server compresses or alters the output after PHP sets the header, the Content-Length value becomes invalid and transfers can be aborted. Disable script-level output compression for the download endpoint or let the webserver send the file itself.

Robust delivery tips

  • Where available, use the webserver’s optimized delivery (X-Sendfile / X-Accel-Redirect) so the server sets headers and streams files reliably.
  • For PHP streaming, clear all output buffers before headers, stream in controlled chunks for large files, and ensure the script won’t hit execution/memory limits.
  • If a host-level security module is terminating the response, capture server logs and ask the host for guidance.

Acknowledgements: suggested the compression angle, / flagged path/include checks, and verified the server-config fix. Use the header-inspection + log-check checklist above to find why Content-Length is still missing and restore consistent downloads.

Recommended Answers

All 7 Replies

What about require - files exists on defined path?

Yes, AndrisP, they are at the correct location.

This is just a shot in the dark, but what steps have you taken to troubleshoot the issue?

I guess I would start by making sure that (i) your connection to the database is successful and (ii) that the file exists on the server. Next, ensure that you get the expected values for $file, $ext, and $size. Then, you could just output the file to the screen instead of reading the file into the buffer.

Finally, what do you see in the response headers? Some servers have gzip encoding enabled by default and that may be the issue (response headers will show "Content-Encoding: gzip"). I had this issue when trying to display pdf files in the browser. You may want to disable gzip encoding in your server's php.ini or, alternatively, in your code.

Are you sure you are pushing all files to the web server? For example, you have:

require '../../php/PDO_Connection_Select.php';  
require '../../php/GetUserIpAddr.php';

Are those files in the same relative location on the web server?

Does the web server have the files at the locations described in the database?

I got it working by making changes to php.ini. The only problem now is that the filesize isn't being sent to the browser. I tried 3 different browsers and none of them are seeing the filesize. Here's the current code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

function usdloader($filenumber=NULL){
    require '../../php/PDO_Connection_Select.php';

    if (!$pdo = PDOConnect("foxclone_data")) {
        exit("unable to connect to database"); }

   $test = $pdo->query("SELECT `filename` FROM `files` WHERE `id` = $filenumber");
   $filename = $test->fetchColumn();

  if (!file_exists($filename)) {
        exit("Requested file ($file) does not exist");
      }
  else {
    $file_path  = './' . $filename;
    $size = filesize($filename);
        header('Content-Description: File Transfer');
        header('Content-Type: ' . mime_content_type($filename));
        header('Content-Disposition: attachment; filename='.$filename);
        header('Expires: 0');
        header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        readfile($filename); // Download the file
        flush();
    }
}
usdloader($_GET["f"]);

It's all working now. It involved a change to php.ini and adding SetEnv no-gzip dont-vary to my .htaccess file.

Glad you've gotten it working. I've marked your thread as solved.

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.