Jump to content

[SOLVED] fopen problem


robert.juric

Recommended Posts

I've been working on a little file download program, but I'm not really sure what's wrong with it. It will send the file with the right name, but it's missing the file extension and its not the full file size. I'm not sure if it's getting interrupted or if there is something else going on.

 

<?php
// PHP Download
$mysqli = new mysqli("localhost", "***", "***", "softlib");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s<br>", mysqli_connect_error());
    exit();
}
$id = $_GET['id'];

$query = "SELECT name, path FROM soft WHERE id = $id";

if ($result = $mysqli->query($query)) {
    if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $filename = $row["name"];
        $filepath = $row["path"];
        header("Content-Type: Application/Unknown");
        header("Content-Disposition: filename=$filename");
        if ($fp = fopen($filepath, "r")) {
            while (!feof($fp)) {
                echo fgets($fp, 4096);
            }
            fclose($fp);
        }
    }
}
$mysqli->close();
?>

 

Any ideas?

Link to comment
Share on other sites

I'm tried that and I am still getting the same empty file result. I looked in the log, and when I tried running it with my original code, I got this warning:

 

[Mon Dec 31 08:26:36 2007] [error] [client 127.0.0.1] PHP Warning:  fopen(C:/Storage/filename.exe) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No error in C:\\WWW\\download.php on line 17, referer: http://localhost/search.php

 

Why would it be failing to open the stream?

Link to comment
Share on other sites

I just figured it out!!

 

When I gave the header:

header("Content-Disposition: filename=$filename");

I just realized that I was using the 'name' column  for the the $filename, however I was entering the programs actual name into the table; I wasn't using the file name and it's extension. When I designed the table, I created the 'name' column more as a title instead of the actual file name. So I went back to the table and put a name in with a file extension included and it worked!! If I had a better understanding of the headers I would have realized the mistake quicker. I added something to strip the filename from the $filepath instead of using the name column at all.

 

Here's the working code(now removes filename from $filepath):

<?php
$mysqli = new mysqli("localhost", "***", "***", "softlib");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s<br>", mysqli_connect_error());
    exit();
}
$id = $_GET['id'];

$query = "SELECT path FROM soft WHERE id = $id";

if ($result = $mysqli->query($query)) {
    if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        // The "path" column MUST be full path including filename and extension.
        $filepath = $row["path"];
        $end = strrchr($filepath, "/");
        $filename = substr($end, 1);
        header("Content-Type: application/unknown");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        $handle = fopen($filepath, "rb");
        echo fread($handle, filesize($filepath));
        fclose($handle);
    }
}
$mysqli->close();
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.