Jump to content


Photo

$file['type']


  • Please log in to reply
8 replies to this topic

#1 geoffl1

geoffl1
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 05:30 AM

When I try to use:

print $file['type'];

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

Any suggestions? Thanks.

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 05:42 AM

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
<input type="file" name="pictures[]" />

you're print statement must be
print $_FILES['pictures']['type'];

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 geoffl1

geoffl1
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:19 AM

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>';
}
}


#4 geoffl1

geoffl1
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:30 AM

help please! I need to figure this out ASAP. Thanks

#5 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 06:33 AM

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
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.'
';
   }
}


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#6 geoffl1

geoffl1
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:37 AM

I get an error when I try that:

Call to undefined function mime_content_type()

#7 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 06:38 AM

put this little snippet at the top of your script
if (!function_exists('mime_content_type')) {
   function mime_content_type ($f) {
       return trim(shell_exec('file -bi '.escapeshellarg($f)));
   }
}

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#8 geoffl1

geoffl1
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 14 September 2006 - 06:43 AM

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?

#9 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 06:49 AM

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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users