Jump to content

Recommended Posts

Posted

So, I'm trying to write a download script for my site by using the "Content-disposition" header. However, in order for it to work, I have to supply the file's MIME type. So I wrote it to automatically check for the file's type using finfo, so different files could still be downloaded. Then I got:

 

Fatal error: Call to undefined function finfo_open()

 

According to PHP.net, I found it's because the PECL extension is not installed.

 

So, please install it, because I would love to get this to work.

 

Thanks,

~Derek

 

Extra Info:

Username: derekboy

Site: unorthodox.co.cc

PHP Source:

<?php

/* The equivalent code:

*

* header('Content-disposition: attachment; filename=someFile.ext');

* header('Content-type: madeUp/MIMEtype');

*

*/

$filename = $_GET['file'];

$fileinfo = finfo_open(FILEINFO_MIME_TYPE);

 

chdir("download");

header('Content-disposition: attachment; filename=' . $filename);

header('Content-type: ' . finfo_file($fileinfo, $filename));

readfile($filename);

?>

Posted

Alright thanks, and sorry for posting in the wrong section--the FAQ said to post it here.

Posted
Alright thanks, and sorry for posting in the wrong section--the FAQ said to post it here.

That was the right forum to post in.

Posted

Oh sorry--when I saw "Moved" I just assumed. I hadn't realized it was moved to "Escalated Requests".

Posted

The PECL extension is certainly installed.

 

root@stevie [/usr/local/apache/logs]# pecl version
PEAR Version: 1.9.1
PHP Version: 5.3.3
Zend Engine Version: 2.3.0

 

I don't suppose you mean a particular PECL extension? PECL is a PHP extension installer.

Posted
I don't suppose you mean a particular PECL extension? PECL is a PHP extension installer.

 

I think what he's really wanting is for this particular function to work.

 

finfo_open()

 

Posted

Ah, okay. That appears to be in the "fileinfo" extension. I tried to install it, but the ./configure failed, and the installer said it was deprecated for a compiled PHP extension.

Posted

@Derek

 

Exactly what info do you need from a file? Would this work?

 

http://www.php.net/manual/en/function.curl-getinfo.php

 

Here's an example of what kind of info you can get with curl_getinfo. Just enter a url in the form and submit.

 

http://byrondallas.heliohost.org/php/tools/curl_get_info.php

 

EDIT: Here's a list of functions that we do support:

 

http://byrondallas.heliohost.org/php/get_d...d_functions.php

 

Posted
@Derek

 

Exactly what info do you need from a file?

 

Well, extending on what I said on the original post, I'm writing a download script that takes the file name from the url, so if I wanted to download my test.txt, I would go to "download.php?file=test.txt"

 

<?php
    $filename = $_GET['file']; //Get the file name using the variable "file" in the URL
    chdir("download"); //Files to download are in my public_html/download folder
    header('Content-disposition: attachment; filename=' . $filename); //Force the browser to download file, not view it.
    header('Content-type: text/plain'); //This would only work for test.txt
    readfile($filename);
?>

 

So, the 4th line is telling the browser what MIME type the file is, so this example would work fine, but if I tried to download, lets say, a Zip folder, it would be telling the browser that it's a text file, and it thinks it's supposed to be saved as a .txt extension.

 

That's what I needed the finfo_*() functions for--it would give me the MIME type of any file being used for $file, so it would save as a .zip, for example, instead of a .txt.

 

EDIT: Yeah, byron, I checked that example you gave me and it looks like I could use it, but it returns a lot of data when I really only need the "Content-Type" part of it, so I'll read more on those PHP.net pages you linked.

Posted

Returns only the content type:

 

<?php

$ch = curl_init("http://byrondallas.heliohost.org/audio/budtotal.wav");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_EFFECTIVE_URL, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

# For this example, just discard the output.
$src = curl_exec($ch);

# Get all information about the transfer. 
$extract = curl_getinfo($ch);

# Echo only the content type
echo $extract['content_type'];

# Clean up. 
curl_close($ch);
?>

 

http://byrondallas.heliohost.org/test/Curl-url-info.php

 

Posted

Cool, thanks. It works just like I needed. I guess I didn't need finfo at all :D

Guest
This topic is now closed to further replies.
×
×
  • Create New...