Page 1 of 1

Determine remote file type

Posted: Mon Nov 22, 2004 7:47 pm
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)

Posted: Mon Nov 22, 2004 7:53 pm
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...

Posted: Mon Nov 22, 2004 7:55 pm
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

Posted: Mon Nov 22, 2004 8:00 pm
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)

Posted: Mon Nov 22, 2004 8:08 pm
by dull1554
this is true....it was a quicky.....

Posted: Mon Nov 22, 2004 8:13 pm
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

Posted: Mon Nov 22, 2004 8:14 pm
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...

Posted: Mon Nov 22, 2004 8:16 pm
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)

Posted: Mon Nov 22, 2004 8:24 pm
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...

Posted: Mon Nov 22, 2004 9:35 pm
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

Posted: Tue Nov 23, 2004 2:12 pm
by josh
Anyone?

Posted: Tue Nov 23, 2004 4:26 pm
by timvw
you could save the retrieved file somewhere in /tmp and then run the function on that file.....

Posted: Tue Nov 23, 2004 4:28 pm
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.