Skip to content

Commit

Permalink
Changes to fFile to better handle text filetype detection
Browse files Browse the repository at this point in the history
Improved the comment

Changes to fFile to better handle text filetype detection

Improved the comment
  • Loading branch information
Kevin Hamer authored and jeffturcotte committed Nov 26, 2012
1 parent eb2d48b commit 9071914
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions fFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,15 @@ static private function determineMimeTypeByContents($content, $extension)
}


// Text files
if (strpos($content, '<?xml') !== FALSE) {
// Better detection for text files based on the first line or so.
if (strpos($content, '<?php') !== FALSE || strpos($content, '<?=') !== FALSE) {
return 'application/x-httpd-php';
}

preg_match('/(\S.*?)\s*\n/m', $content, $lines);
$first_line = count($lines) > 1 ? $lines[1] : '';

if (strpos($first_line, '<?xml') !== FALSE) {
if (stripos($content, '<!DOCTYPE') !== FALSE) {
return 'application/xhtml+xml';
}
Expand All @@ -349,14 +356,17 @@ static private function determineMimeTypeByContents($content, $extension)
if (strpos($content, '<rss') !== FALSE) {
return 'application/rss+xml';
}
return 'application/xml';
}
return 'application/xml';
}

if (strpos($content, '<?php') !== FALSE || strpos($content, '<?=') !== FALSE) {
return 'application/x-httpd-php';
if (stripos($first_line, '<html') !== FALSE) {
return 'text/html';
}
if (stripos($first_line, '<!DOCTYPE') !== FALSE) {
return 'text/html';
}

if (preg_match('#^\#\![/a-z0-9]+(python|perl|php|ruby)$#mi', $content, $matches)) {
if (preg_match('#^\#\![/a-z0-9]+(python|perl|php|ruby)$#mi', $first_line, $matches)) {
switch (strtolower($matches[1])) {
case 'php':
return 'application/x-httpd-php';
Expand Down Expand Up @@ -1341,4 +1351,4 @@ public function write($data)
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/

0 comments on commit 9071914

Please sign in to comment.