Jump to content

Neither readfile or fopen are working


mdub2112

Recommended Posts

Seems like I have the same problem this gent had

http://www.phpfreaks.com/forums/ind...;prev_next=prev

Only problem is that when I try to do what resolved his issue, it does open the correct file, only all garbled

If I go straight with fopen I get output to the browser like below.
e.g.
[color=orange][color=red]Low � A Client has a problem to which only affects them and does not affect workflow. An issue of a �Low Severity� would be something to the likes of: �My mouse[/color][/color] is what with a Word Doc

[color=orange]��A��1��`��RL�EX�]цX3E��q.4aUC���uJ��pce�G*��k&蒻u]��u�(.A�A����*煃�6B�sNgq77��w�A3`0ウX��5FC�L:���w[/color] is what I get for images


If I go with all the headers and then fopen, I get the same results as if I used readfile, to which both seem to not work for me.  With the headers, the file that seems to be sent, is (with a Word file) showing the html head tags in my download.php script.


Any help would truly be appreciated.



Link to comment
Share on other sites

Download Link Code
[code]
echo "<a href=download.php?id={$uploads['id']}>
              {$uploads['title']}<br />
                </a>";
[/code]


download.php script

[code=php:0]
<title>Downloading....</title>
<link href="../css/emt_external.css" rel="stylesheet" type="text/css">
<div id="global">
<?php
include_once('db_depot.php');
$handle = db_connect();
$uploaded = $_REQUEST['id'];
$query = "select id, name, type, size from uploads where id = $uploaded";
$result = $handle->query($query);
  if (!$result)
  {
    echo "There was a database error when executing <pre>$query</pre>";
    echo mysqli_error();
    exit;
  }

  while ($file = $result->fetch_assoc())
{
$file = $file['name'];
$type = $file['type'];
$size = $file['size'];
$path = $file['path'];
if(!is_readable($file))
{
die("$file does not exist or is not readable");
}
  else
  {
//echo "$filepath is readable and does exist";
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header('Content-type: ' . $type);
  header( "Content-Disposition: attachment; filename=".basename($file));
  header( "Content-Description: File Transfer");
      header('Accept-Ranges: bytes');
      header('Content-Length: ' . $size);
  //readfile($file);
  $dir = opendir($path);
  $handle = fopen($file, 'r') or die('couldnt open file');
  $contents = fread($handle, filesize($file)) or die('couldnt read file');
  fclose($handle);
  echo $contents;
  }
}


exit;

?>
</div>
[/code]

The outcome to this is the same as if I comment out the code below the readfile (to which is now commented out).
The files are being uploaded (successfully) to  /var/www/uploads, the path info is stored in the MySQL Dbase.  Currently I have the path stored in one column, and the filename in another with the path attached (/var/www/uploads/filename.ext).
Link to comment
Share on other sites

[quote author=Caesar link=topic=117889.msg481265#msg481265 date=1165604706]
Are you trying to open locally on the server using relative paths? Or are you using absolute paths?

Check your php.ini settings for the following value: "allow_url_fopen = On"
[/quote]

Just checked this...  set to on
Link to comment
Share on other sites

A few comments on your code, which may or may not have any relevence to your problem.

1) You use the variable "$file" for two different purposes, once as the array holding the results of the qb query and once to hold the actual filename. Change one or the other variable.

2) You have "$dir = opendir($path)", but I don't see where you use the variable anywhere.

If you're using Firefox, I would recommend getting either the firebug or livehttpheaders extensions. Either extension will show the headers being returned by your script.

Ken
Link to comment
Share on other sites

[quote author=kenrbnsn link=topic=117889.msg481279#msg481279 date=1165606013]
A few comments on your code, which may or may not have any relevence to your problem.

1) You use the variable "$file" for two different purposes, once as the array holding the results of the qb query and once to hold the actual filename. Change one or the other variable.

2) You have "$dir = opendir($path)", but I don't see where you use the variable anywhere.

If you're using Firefox, I would recommend getting either the firebug or livehttpheaders extensions. Either extension will show the headers being returned by your script.

Ken
[/quote]

Ya, I had tried something else, to which is where the opendir came from and I forgot to remove it prior to posting (it's not in the script right now, but no diff).  I am using Firefox, just might have to do that, seems the downloads (when using the headers with readfile or fopen) are getting the correct info as it knows it's a gif, jpg, doc, pdf, zip, etc...    I was wondering about that second occurance of using $file.  I did change that and it didn't seem to help.
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.