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?

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.