Determine remote file type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Determine remote file type

Post by josh »

I need to determine the file type of a remote file

filetype() and mime_content_type() are not working...

is there another way? (besides downloading the files headers and going through them which is a pain)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

what do you mean they are not working? works fine for me. got some code to look at or some sort of error message? maybe you aren't passing it to filetype() correctly...
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

if you have the file is it that you want the extension?

heres a function

Code: Select all

function getfileextension($file) {
     $array = explode('.',$file);
     return $array[1];
}
hope that helps
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

$line=file_get_contents($remote);
$data=addslashes($line);
if (!function_exists('mime_content_type')) {
    function mime_content_type($f) {
        $f = escapeshellarg($f);
        return trim( `file -bi $f` );
    }
}
$type=mime_content_type($remote);
if ($type==NULL) {
    $type="UNKNOWN";
}
$type is set to "can't stat `http://google.com' (No such file or directory)."

but $data is set to goole's html source code

dull1554, yeah that works but could return the wrong type if a user renamed a .jpg to a .exe or did something stupid like that then my script causes mass destruction :-)

(also if the user has more then one "." in the file name all hell would break loose)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

this is true....it was a quicky.....
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yeah... I've used methods like that before though and then.... (gasp, hell broke loose) lol so now I always use mime contents types... that was when I uploaded files from a users machine, now I am writeing the part of my website where a user can use a remote image instead of uploading one and I hate hotlinkers so I am going to hotlink once, (to get the image) then I will store it on my server and use the one stored on my server as opposed to useing the original image..

Basically I have 2 options

<img src="http://someguy.com/images/1/.jpg">

or

*copy 1.jpg to my server then go
<img src="/images/1.jpg">

I choose the later option in order to save someguy.com some bandwidth and also if he renames 1.jpg then I get 404's on my website
Last edited by josh on Mon Nov 22, 2004 8:15 pm, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

what is the line where you are declaring google.com's index.html or whatever file it is you are trying to load...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

case 'upload':
			$remote=$_REQUEST['remote'];
			if ($remote==NULL || $remote == "http://") {
				if (filesize($_FILES['userfile']['tmp_name'])<1048576 AND $_FILES['userfile']['type']!=NULL) {
					$data = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));
					$type=secure($_FILES['userfile']['type']);
					//echo $data;
				} else {
					echo ("File size exceeded");
				}
			} else {
				$line=file_get_contents($remote);
				$data=addslashes($line);
				if (!function_exists('mime_content_type')) {
				   function mime_content_type($f) {
				       $f = escapeshellarg($f);
				       return trim( `file -bi $f` );
				   }
				}
				$type=mime_content_type($remote);
				if ($type==NULL) {
					$type="UNKNOWN";
				}
			}
			
			
			$user = $_SESSION['user'];
			
			$sql="INSERT INTO `files` ( `user` , `file` , `type` ) VALUES ( '$user', '$data', '$type');";
			//echo $type;
			mysql_query($sql) or die(mysql_error());
			header("location:http://pythonweb.com/jshpro2/files/");
		break;

This is just a portion of my script (the part that does the saveing of the files)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

try using $_POST or $_GET instead of $_REQUEST... i've seen people have similar problems with request screwing up what they were trying to pass.. other than that, man i dont see a problem with your code...


Edit : could you possibly post an error message in what is going on? or tell exactly what it is doing incorrectly? Unfortunately, I'm not even fully understanding what the complete problem is other than you can't get a file type. How do you know it isn't working? If it's saying that the file google.com doesn't exist, it should be a no brainer why..

edit 2 : should also note, it's been a while since i was on these boards. kinda rusty so i might be over looking something. still reviewing your code...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

yeah, well i know google exists... and i was able to do a file get contents but for some reason filetype functions cant check my remote files

could it be that file get contents was introduced after my version of php ....(i used a user defined function)

is it that filetype() or mime_content_type do not support remote files like get_file_contents does?
what function does?

Code: Select all

echo (filetype("http://google.com"));
// echo's cant stat http://google.com file does not exists!
same thing with mime_content_type
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Anyone?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you could save the retrieved file somewhere in /tmp and then run the function on that file.....
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you cant stat remote files unless your using php5, and then it will only sometimes work


you could see if the server sent a mime header

Code: Select all

fclose(@fopen('http://foo.com', 'r'));
print_r($http_response_header);
not sure if that will work for you though, sounds like your version of php is kinda old.
Post Reply