mime_content_type() returning empty string?

The mime_content_type() function is returning an empty string instead

Page 1 of 1

14 Replies - 2887 Views - Last Post: 21 July 2009 - 07:26 AM Rate Topic: -----

#1 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

mime_content_type() returning empty string?

Posted 20 July 2009 - 10:26 AM

I'm running the latest WAMP server on XP. I've enabled the mime_module and mime_magic_module in apache. The following code returns an empty string instead of the expected mime type. Thanks for any help.

echo '	<li> '.mime_content_type($filename).'</li>'


So if $filename = test.php it should give the line item "text/plain". Instead I'm getting an empty line item. Anybody have any idea what I'm doing wrong? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: mime_content_type() returning empty string?

#2 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 10:49 AM

That function works off a path so insure the path to the file is correct ...


NOTE** ( from the php .net :P :) )
But mainly that function is deprecated and referred to fileinfo as a replacement

http://us3.php.net/m....finfo-file.php

check out that particular function for the fileinfo ... and the extension may need to be installed as well

This post has been edited by RPGonzo: 20 July 2009 - 10:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 10:49 AM

Perhaps this could help? http://php.net/manua...nstallation.php
Was This Post Helpful? 0
  • +
  • -

#4 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 11:09 AM

Thanks for the quick response. I did set the path in php.ini.

Now when you say it works off a path, if the file is in the same directory as the php file it should just need the name right? Otherwise say I've got the file "test.php" on the site "www.webdev.net/movies/" running on "C:\wamp\www\movies\". What path should I use? The web path, or the local path? Thanks for the help. I'm still pretty new to the php scene.
Was This Post Helpful? 0
  • +
  • -

#5 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 11:21 AM

That depends on permissions and such, if you don't have the permission to access a file you might not be able to get the mime type of it either. I actually can't get the mime_content_type() function to return anything but an empty string on my part as well. :( If you're trying to get the mime type of images you could use the getimagesize()-function!

View PostSpiderSpartan, on 20 Jul, 2009 - 08:09 PM, said:

Thanks for the quick response. I did set the path in php.ini.

Now when you say it works off a path, if the file is in the same directory as the php file it should just need the name right? Otherwise say I've got the file "test.php" on the site "www.webdev.net/movies/" running on "C:\wamp\www\movies\". What path should I use? The web path, or the local path? Thanks for the help. I'm still pretty new to the php scene.

Was This Post Helpful? 1
  • +
  • -

#6 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 11:37 AM

I'm running the server on my home machine so I should have permission to access any files. I'm trying to make an index that will look something like the auto index for the wamp server. I plan on letting some friends upload files to certain folders so I want a dynamic index that neatly displays all the files in a folder. I've got it displaying all the files and I'm able to remove the extensions on the displayed files. I want to use the mime type to be able to display an appropriate icon next to the links. ie: audio, video, image, text, etc. I guess I could write a nice long php function that does this based on the extension, but I was looking for a cleaner/easier way of doing this. It's turning out to not be so easy. Thanks again for the quick replys.
Was This Post Helpful? 0
  • +
  • -

#7 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 12:04 PM

The only other method im aware of is used mainly on file uploads i don't think you could implement it in the same manor as what your speaking of ...

if your making this a "local" server than i don't see why you can't just install the extensions needed to make this a cleaner deal for you ...

This post has been edited by RPGonzo: 20 July 2009 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 12:14 PM

.. double post but whatever check this out ...

// change myfile to your file ... or you could loop through all files
$file = fopen("myfile","r");
foreach (stream_get_meta_data($file) as $k => $v) {
	echo "$k == $v<br/>";
}



the "wrapper_type" could be something of use to you .. :P


EDIT:
NEVERMIND! ... fluke i tested it and it worked now it wont ... stupid NP++ did re upload the file when i tested so it tested the same thing over and over haha .. im still pondering though cause this would be useful to me as well ..

This post has been edited by RPGonzo: 20 July 2009 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#9 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 12:26 PM

Ok, is there any reason you took the thread you started here:
http://www.dreaminco...h...28&t=115584
and duplicated it here? Assume my answer there applies here too, because I cba to paste it.

This post has been edited by moopet: 20 July 2009 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 12:39 PM

without installing the extension it would come down to you writing your own function with a array of file extensions ...

-_-
Was This Post Helpful? 1
  • +
  • -

#11 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 12:46 PM

Sorry for the double post. I didn't see the response on the other one.

I'm running PHP 5.2.9-2. I just downloaded PHP 5.3.

I'll let you know if installing that fixes my problems. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#12 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 04:12 PM

It looks like updating to php 5.3 worked. Thank you so much for all your help.

Also, with php 5.3 installed I didn't make any manual changes to the php.ini file. All I did was enable the mime modules in apache.

It does look like you have to have access to the files locally for this to work though since you have to feed mime_content_type() with the full local path.

Once I get my code cleaned up I'll post it here so you can see exactly what I'm doing.

This post has been edited by SpiderSpartan: 20 July 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#13 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 20 July 2009 - 07:56 PM

Here's basically what I'm using this for. I'm not sure, but I think this has to run on a local server for getcwd() to work.
This code is a combination of copied and original code from various sources.

<html>
	<H1 ALIGN=CENTER> Movies </H1>
</html>

//Upload code
<form enctype='multipart/form-data' method=post>
<table>
  <tr>
	<td><input type="file" size="50" name="att[]" class=input></td>
  </tr>
</table>
<input class=button type=submit value='Upload'>
<input type="button" value="Return" onclick="window.location.href='/'">
</form>

<?php
  // The HTML form handler.
  // Uploading all files
  for($i = 0; $i < count($_FILES['att']['name']); $i++)
  {
	// Moving the file from the temporary directory to the
	// files subdirectory of the Web application.
	if (copy($_FILES['att']['tmp_name'][$i],
			 "".$_FILES['att']['name'][$i]))
	{
	  // Deleting the file in the temporary directory.
	  unlink($_FILES['att']['tmp_name'][$i]);
	  // Changing the file's access permissions
	  chmod("".$_FILES['att']['name'][$i], 0644);
	}
  }

  // If the superglobal array $_POST is not
  // empty, automatically reloading the page.
  if(!empty($_POST))
  {
	echo "<HTML><HEAD>
	<META HTTP-EQUIV='Refresh' CONTENT='0; URL=".$_SERVER['PHP_SELF']."'>
		  </HEAD></HTML>";
  }
?>

<?php	
//Display the files in this directory 
$filearray = scandir(getcwd());	 
for($loop = 0; $loop < count($filearray); $loop += 1)
{	 
	if(($filearray[$loop] != '..')and($filearray[$loop] != '.')and($filearray[$loop] != 'Index.php')and($filearray[$loop] != 'Uploader.php'))
	{
		if(substr($filearray[$loop], 0, 1) != '.')
		{
			If(filetype($filearray[$loop]) == "dir"){
				echo '	<li><img src="/icons/folder.gif" alt="[DIR]"><a href="'.$filearray[$loop].'">'.$filearray[$loop]."</a></li>\n"; 
			} elseif(substr(mime_content_type(getcwd()."/".$filearray[$loop]), 0, 4) == "text"){
				echo '	<li><img src="/icons/text.gif" alt="[TEXT]"><a href="'.$filearray[$loop].'">'.RemoveExtension($filearray[$loop])."</a></li>\n";
				//echo '	<li><a href="'.$filearray[$loop].'">'.mime_content_type(getcwd()."/".$filearray[$loop]).": ".$filearray[$loop].'</a></li>';
			}else{
				echo '	<li><img src="/icons/unknown.gif" alt="[?]"><a href="'.$filearray[$loop].'">'.RemoveExtension($filearray[$loop])."</a></li>\n";
			}
		}
	}	 
}

function RemoveExtension($strName)
{
	$ext = strrchr($strName, '.');
	$ftype = filetype($strName);
	if(($ext !== false)and($ftype != "dir"))
	{
		$fnameChunks = explode(".", $strName);
		$strName = $fnameChunks[0];
		for($i = 1; $i < count($fnameChunks)-1; $i++)
		{
			$strName = $strName . "." . $fnameChunks[$i];
		}
	}
	return $strName;
}
?>


This post has been edited by SpiderSpartan: 20 July 2009 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#14 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: mime_content_type() returning empty string?

Posted 21 July 2009 - 06:07 AM

Right here im not quite sure but if i remember right you do not have to output the <html> and <head> tags to use the <meta> tag for your refresh ..

//Upload code



remember to enclose a PHP statement with the correct tags ;) if you want a HTML comment than you don't have to but the // notates a PHP comment

Don't mean to nitpick just a couple of things i saw ;) looking good though hows the functionality of it so far?
Was This Post Helpful? 0
  • +
  • -

#15 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: mime_content_type() returning empty string?

Posted 21 July 2009 - 07:26 AM

Yeah, I just added that after I had pasted the code here to try and clarify things. It works great so far. I've added checks for video, audio, and image formats too. There is one bug though. A wmv file that I have on there is giving me a mime type of "application/octet" I think I need to update the mime file that apache uses, but I'm not sure exactly how to do that. Thanks again for all the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1