robert.juric Posted December 30, 2007 Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83695-solved-fopen-problem/ Share on other sites More sharing options...
btherl Posted December 30, 2007 Share Posted December 30, 2007 A simpler way is this: $bytes_written = readfile($filepath); if ($bytes_written === false) die("Error writing file"); Quote Link to comment https://forums.phpfreaks.com/topic/83695-solved-fopen-problem/#findComment-425912 Share on other sites More sharing options...
robert.juric Posted December 31, 2007 Author Share Posted December 31, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83695-solved-fopen-problem/#findComment-426428 Share on other sites More sharing options...
rajivgonsalves Posted December 31, 2007 Share Posted December 31, 2007 it could be a permission issue, apache might not have permission on the folder Quote Link to comment https://forums.phpfreaks.com/topic/83695-solved-fopen-problem/#findComment-426430 Share on other sites More sharing options...
robert.juric Posted December 31, 2007 Author Share Posted December 31, 2007 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83695-solved-fopen-problem/#findComment-426450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.