Jump to content

Archived

This topic is now archived and is closed to further replies.

geoffl1

$file['type']

Recommended Posts

When I try to use:

print $file['type'];

it prints the first letter of the file instead of the MIME type.

Any suggestions? Thanks.

Share this post


Link to post
Share on other sites
how are you assigning $file...show a little more code
I'm assuiming it's an upload file array
but I can't be too sure

and if that is the case
remember you have to include the name of the file upload input box from you HTML

E.G
if 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]

Share this post


Link to post
Share on other sites
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>';
}
}

Share this post


Link to post
Share on other sites
the reason it doesn't work is because readdir() doesn't return an array
it return a string with JUST the filename in it

you'll have to use the mime_content_type() function to check the filetype

as 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I don't know what you did either
readdir() when used right.....will give you the filename...and that's it

$file['type'] is kinda....undefined

when you say it's not printing anything........are you testing this script on a php file/html file
because from you included script....it's not doing anything...explaining the blank print

Share this post


Link to post
Share on other sites

×

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.