Determine remote file type
Moderator: General Moderators
Determine remote file type
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)
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)
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
if you have the file is it that you want the extension?
heres a function
hope that helps
heres a function
Code: Select all
function getfileextension($file) {
$array = explode('.',$file);
return $array[1];
}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";
}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)
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
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.
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)
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...
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...
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?
same thing with mime_content_type
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!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
not sure if that will work for you though, sounds like your version of php is kinda old.
you could see if the server sent a mime header
Code: Select all
fclose(@fopen('http://foo.com', 'r'));
print_r($http_response_header);