geoffl1 Posted September 14, 2006 Share Posted September 14, 2006 When I try to use:print $file['type'];it prints the first letter of the file instead of the MIME type. Any suggestions? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/ Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 how are you assigning $file...show a little more codeI'm assuiming it's an upload file arraybut I can't be too sureand if that is the caseremember you have to include the name of the file upload input box from you HTMLE.Gif you're box looks like this[code]<input type="file" name="pictures[]" />[/code]you're print statement must be[code]print $_FILES['pictures']['type'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91514 Share on other sites More sharing options...
geoffl1 Posted September 14, 2006 Author Share Posted September 14, 2006 I'm reading files from a directory. Here is the code. Thanks again.// open the current directory by opendir$handle=opendir(".");while (($file = readdir($handle))!==false) { print $file['type']; if($file['type']=="application/x-php"||$file['type']=="text/html"){ //doesn't allow .php or .html files to be seen } else{ print "<a href='$file'>Click to download </a>"; print $file.'<br>'; }} Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91530 Share on other sites More sharing options...
geoffl1 Posted September 14, 2006 Author Share Posted September 14, 2006 help please! I need to figure this out ASAP. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91541 Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 the reason it doesn't work is because readdir() doesn't return an arrayit return a string with JUST the filename in ityou'll have to use the mime_content_type() function to check the filetypeas simple change to your script[code]while (($file = readdir($handle))!==false) { if(mime_content_type ($file)=="application/x-php"| | mime_content_type ($file)=="text/html"){ //doesn't allow .php or .html files to be seen } else{ print "<a href='$file'>Click to download [/url]"; print $file.''; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91544 Share on other sites More sharing options...
geoffl1 Posted September 14, 2006 Author Share Posted September 14, 2006 I get an error when I try that:Call to undefined function mime_content_type() Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91546 Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 put this little snippet at the top of your script[code]if (!function_exists('mime_content_type')) { function mime_content_type ($f) { return trim(shell_exec('file -bi '.escapeshellarg($f))); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91549 Share on other sites More sharing options...
geoffl1 Posted September 14, 2006 Author Share Posted September 14, 2006 It still won't print anything. Any other ideas? It was working at first just using $file['type'] but then randomly stopped. I can't figure out what I must've changed.Could there be anything in my php.ini file causing it? Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91553 Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 I don't know what you did eitherreaddir() when used right.....will give you the filename...and that's it$file['type'] is kinda....undefinedwhen you say it's not printing anything........are you testing this script on a php file/html filebecause from you included script....it's not doing anything...explaining the blank print Quote Link to comment https://forums.phpfreaks.com/topic/20693-filetype/#findComment-91559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.