Skip to content

Commit

Permalink
Fix bug in file_is_text, removes mime_content_type compatibility func…
Browse files Browse the repository at this point in the history
…tion and add file_mime_content_type() [#15 state:resolved]
  • Loading branch information
Fabrice Luraine committed Jul 31, 2009
1 parent 59062eb commit c6714c0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
37 changes: 18 additions & 19 deletions lib/limonade.php
Expand Up @@ -1897,6 +1897,7 @@ function mime_type($ext = null)
'cpt' => 'application/mac-compactpro',
'csh' => 'application/x-csh',
'css' => 'text/css',
'csv' => 'text/csv',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'djv' => 'image/vnd.djvu',
Expand Down Expand Up @@ -2038,25 +2039,23 @@ function mime_type($ext = null)
return is_null($ext) ? $types : $types[strtolower($ext)];
}

if(!function_exists('mime_content_type')) {
/**
* Detect MIME Content-type for a file
*
* @param string $filename Path to the tested file.
* @return string
*/
function mime_content_type($filename)
{
$ext = strtolower(array_pop(explode('.', $filename)));
if($mime = mime_type($ext)) return $mime;
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mime;
}
else return 'application/octet-stream';
/**
* Detect MIME Content-type for a file
*
* @param string $filename Path to the tested file.
* @return string
*/
function file_mime_content_type($filename)
{
$ext = file_extension($filename); /* strtolower isn't necessary */
if($mime = mime_type($ext)) return $mime;
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mime;
}
else return 'application/octet-stream';
}


Expand Down Expand Up @@ -2132,7 +2131,7 @@ function file_extension($filename)
*/
function file_is_text($filename)
{
if($mime = mime_content_type($filename)) return substr($mime,0,5) == "text/";
if($mime = file_mime_content_type($filename)) return substr($mime,0,5) == "text/";
return null;
}

Expand Down
1 change: 1 addition & 0 deletions tests/all.php
Expand Up @@ -16,5 +16,6 @@
require $basedir."router.php";
require $basedir."request.php";
require $basedir."main.php";
require $basedir."file.php";
require $basedir."functional.php";
end_test_suite();
Binary file added tests/data/deer.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added tests/data/empty_text_file.txt
Empty file.
52 changes: 52 additions & 0 deletions tests/file.php
@@ -0,0 +1,52 @@
<?php
if(!defined('LIMONADE')){$h="HTTP/1.0 401 Unauthorized";header($h);die($h);}// Security check

test_case("File");
test_case_describe("Testing limonade file functions.");

function before_each_test_in_file()
{

}

function test_file_mime_type()
{
$mimes = mime_type();
assert_true(is_array($mimes));
assert_not_empty($mimes);
assert_empty(mime_type(''));
assert_equal(mime_type('txt'), 'text/plain');
assert_equal(mime_type('TXT'), 'text/plain');
assert_equal(mime_type('jpg'), 'image/jpeg');
assert_equal(mime_type('JPG'), 'image/jpeg');
}

function test_file_extension()
{
assert_equal(file_extension('my_file'), '');
assert_equal(file_extension('my_file.txt'), 'txt');
assert_equal(file_extension('my_file.html.php'), 'php');
assert_equal(file_extension('my_file.JPG'), 'JPG');
}

function test_file_mime_content_type()
{
assert_not_empty(file_mime_content_type(''));
assert_not_empty(file_mime_content_type('my_file'));
assert_equal(file_mime_content_type('my_file'), 'application/octet-stream');
assert_equal(file_mime_content_type('my_file.txt'), 'text/plain');
assert_equal(file_mime_content_type('my_file.TXT'), 'text/plain');
assert_equal(file_mime_content_type('my_file.jpg'), 'image/jpeg');
assert_equal(file_mime_content_type('my_file.JPG'), 'image/jpeg');
}

function test_file_is_text()
{
assert_true(file_is_text('my_file.txt'));
assert_true(file_is_text('my_file.TXT'));
assert_true(file_is_text('my_file.css'));
assert_true(file_is_text('my_file.csv'));
assert_false(file_is_text('my_file.jpg'));
}

end_test_case();

0 comments on commit c6714c0

Please sign in to comment.