icklechurch Posted March 10, 2009 Share Posted March 10, 2009 Hello, Having a bit of a problem with .wmv and ot her media files.. Basically, I am trying to upload the contents of a file ($file) using the following method: if(file_exists($file)) { $fh = fopen($file, 'rb'); print(fread($fh, filesize($file))); } Trouble is, my $file is of the form http://myserver/00000001.wmv (or .jpg or .mp3). Php does not seem to be recognising the URL of the file as a file with the file_exists function. Even if I skip this and go to fopen, again, it thinks nothing is there But if you went to http://myserver/00000001.wmv though (for example) the file plays fine. Please help - this has been driving me up the wall all day!! ??? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/ Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 file_exists() takes a server path not a url. Extract the filename from the url and specify the path i.e. /var/www/html/myfiles/media/00001.wmv Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781362 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Thanks for that. Problem is that the files are sitting on an external server. Is there an equivalent of file_exists and fopen for a url? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781887 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2009 Share Posted March 11, 2009 Then I would use the HTTP status code to check the file exists. From the url http://yoursite,com/files/000.wmv - if it returns a 200 then the url is fine. If it returns a 404 then the file does not exist. Test it out here http://www.seoconsultants.com/tools/headers.asp Then write a PHP script to do it, not that hard. Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781898 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Hi, Yes, the file definitely exists - it's just a case of opening it from an alternate URL (for security reasons). The site will be dealing with 1000s of images and pictures so downloading to a file on the server is not an option...is there really not a file_exists and fopen equivelent for a url? I'm a bit of a newbie to this so apologies if my questions seem a bit obvious! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781908 Share on other sites More sharing options...
trq Posted March 11, 2009 Share Posted March 11, 2009 file_exists and fopen work perfectly fine with urls providing allow_url_fopen is enabled within your php.ini. Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781912 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2009 Share Posted March 11, 2009 Yes you can use fopen() but only if the remote server allows this. What is wrong with checking the HTTP header with the URL as I said. Heres some code. Try it out: <?php function httpHeader($url) { $urlParts = parse_url($url); $fp = fsockopen($urlParts['host'],80,$errno,$errstr,10); $out = "GET ".$url." HTTP/1.1\r\n"; $out.= "Host: ".$urlParts['host']."\r\n"; $out.= "Connection: Close\r\n\r\n"; fwrite($fp,$out); $content = fgets($fp); return $content; } $url = "http://www.google.co.uk/intl/en_uk/images/logo.gif"; print httpHeader($url); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781916 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Still no joy - I swear I'm doing something daft :-\ The httpHeader function worked fine - I got the 'HTTP/1.1 200 OK' reply for my URL. And allow_url_fopen is on in my php.ini file But when I try to add the following code to open the image, no joy: $url = "http://www.google.co.uk/intl/en_uk/images/logo.gif"; $fh = fopen($url, 'rb'); print(fread($fh, filesize($url))); Any ideas?! Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781918 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2009 Share Posted March 11, 2009 function getSizeFile($url) { if (substr($url,0,4)=='http') { $x = array_change_key_case(get_headers($url, 1),CASE_LOWER); if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; } else { $x = $x['content-length']; } } else { $x = @filesize($url); } return $x; } print getSizeFile("http://www.google.co.uk/intl/en_uk/images/logo.gif"); Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781920 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Cool, thanks. I had something similar (but not quite as good ) to get the filesize. I think I'm nearly there (we hope!). Below is the code but instead of outputing the image I get 'GIF89a' .... is print the wrong function to use here? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781927 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2009 Share Posted March 11, 2009 This is not meant to display the image. It gets the filesize from the header. Why are you trying to display the image. I thought you were trying to check if a file exists / get the size of the file. Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781932 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Oh no! Sorry for not making myself clear enough. I need to check it exists, get the size AND display it - I might have missed off the last point though in trying to get the first two to work... Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781933 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 Think I've solved the last bit myself! To download the image, I use the following code: $file = 'http://www.google.co.uk/intl/en_uk/images/logo.gif'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $filesize); ob_clean(); flush(); readfile($file); exit; That seems to download the file Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781938 Share on other sites More sharing options...
JonnoTheDev Posted March 11, 2009 Share Posted March 11, 2009 if your files are wmv then how do you plan on displaying them? Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-781959 Share on other sites More sharing options...
icklechurch Posted March 11, 2009 Author Share Posted March 11, 2009 The above code downloads mp3, wmv and jpg files (which are the three I'm dealing with) - you get a pop-up window asking if you want to download the files. The wmv files simply download into the wmv player...or whatever player the user has (i think!) Quote Link to comment https://forums.phpfreaks.com/topic/148797-wmv-files-and-file_exist/#findComment-782031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.