Jump to content

Recommended Posts

I have a webpage that basically just dumps a directory full of files that contain .mp3, .pdf, and .doc files primarily.  Before each filename are two options - Download or Open.  (see example1.gif)

 

I recently transitioned from a php 4.2 server to a 5.x server.  Clicking on the "Open" link simply streams the files if it's a .mp3 and plays it.  This works on both php 4.2 and 5.x servers.  However, clicking on the Download link does something different.

 

In php 4.2, when clicking the [ Download ] link, a menu pops up just in you see in example1.gif.  I can select Open or Download.  If I select Open, the file, again, simply streams and plays music if it's a .mp3 file.  It works great.

 

In php 5.x, when I click [ Download ] and then select Open, the file fails to play and gives an error as seen in example2.gif.  If I click on the [ Download ] link and select Download from the menu, it saves a file, but it's a smaller filesize than a mp3 and will not open or play.

 

Here is the code for the page that displays the files and the [ Download ] linking:

 

<td nowrap bgcolor="<? echo $bgcolor ?>"><a href="download_file.php?file=<? echo substr($Value,0,$str_pos) ?>&path=<? echo $FilePath ?>">[ Download ]</a></td>

 

So, $Value is equal to something like this "02 Create In Me.mp3|3.4 MB's", I split the string to show only the filename "02 Create In Me.mp3".

 

If you notice, the link goes to download_file.php and passes the file name and path for the file (sub folder).

 

Here is the code for download_file.php which tries to set the header based upon the file type so a user can download it:

 

<?php
$File_Path=$_GET['path'];
$File_Name=$_GET['file'];
$File_Location=$File_Path.$File_Name;
$File_Type = substr($File_Name, -4);

switch ($File_Type)
{
  Case ".mp3":
  $ContentType = "audio/mpeg3";
  break;
  Case ".avi":
  $ContentType = "video/avi";
  break;
  Case (".doc" OR "docx"):
  $ContentType = "application/msword";
  break;
  Case ".gif":
  $ContentType = "image/gif";
  break;
  Case (".jpg" OR "jpeg"):
  $ContentType = "image/jpeg";
  break;
  Case ".wav":
  $ContentType = "audio/wav";
  break;
  Case ".m4a":
  $ContentType = "audio/m4a";
  break;
  Case (".mpg" OR "mpeg"):
  $ContentType = "video/mpeg";
  break;
  Case ".rtf":
  $ContentType = "application/rtf";
  break;
  Case ".pdf":
  $ContentType = "application/pdf";
  break;
  Case ".txt":
  $ContentType = "application/txt";
  break;
}
@ob_end_clean();
// The following code makes the download non cacheable
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header('Content-disposition: attachment; filename='.$File_Name);
header('Content-type:'.$ContentType);
readfile($File_Location);
// You never see the text below print out unless I remark the 3 lines above this one to check for errors.
echo "File Name= ".$File_Name."<br>";
echo "Content Type= ".$ContentType."<br>";
echo "File Location= ".$File_Location."<br>";
echo "File Type= ".$File_Type."<br>";
?>

 

My question is, how do I make this php 5.x compliant, since it works just fine in php 4.2?

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/205608-works-in-php-4-but-not-5-why/
Share on other sites

You are accessing the file using a URL and that is probably disabled. The error you didn't post (in your unedited post) and the abbreviated contents of the downloaded file are probably errors alerting you to this fact.

 

If the file is actually local to the server, you should be using a file system path to access the file in the readfile() statement. By using a URL, you are causing your server to make a http request back to itself, to read the file, then output it. That consumes three times the bandwidth than just reading the file through the file system and outputting it.

 

Edit: If you open the abbreviated downloaded file using your programming editor, what does it contain?

I renamed the abbreviated file to .txt and opened it with notepad.

 

Here is what it contains, I replace the domain name with <URL>

 

<br />

<b>Warning</b>:  readfile(<URL>/Music_Library/02 Create In Me.mp3) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

in <b>/home2/sanjose3/public_html/download_file.php</b> on line <b>52</b><br />

File Name= 02 Create In Me.mp3<br>Content Type= audio/mpeg3<br>File Location= <URL>/Music_Library/02 Create In Me.mp3<br>File Type= .mp3<br>

 

BTW, yes the files are local files as well. 

 

So... after reading your comment like 6 times, the readfile should be like readfile(/Music_Library/<filename>) instead of with a URL, correct?

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.