arfa Posted October 27, 2008 Share Posted October 27, 2008 I work well in PHP but am entering a new area here. The scenario: I have mp3's remotely hosted [archive.org], have written code to force a download but... it would be nice to have access to the filesize - both to display next to the download and, to tell the downloader so it can display xkb of yMb on the progress bar. So, ___________________ *first question*: How to test that the host will allow filesize() or some equivalent I have used: if (ini_get('allow_url_fopen') == '1') and returned true Using either filesize() or stat() gives error other suggestions? ____________ *second question*: A simple routine (or links to). There are several on the web using many approaches but, none of them work for me. It would be good to have an answer q1 so I can be more clear how to approach q2 One of those 'not exactly sure where to start' things for me. thanks - arfa Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2008 Share Posted October 27, 2008 Php cannot get remote file information using the HTTP protocol. The file size is part of the stat() functionality. You would need to use a protocol (such as FTP) in order to get a remote file's size - http://us3.php.net/wrappers Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 you can use CURL to do a HEAD request (usually supported by web servers). this will retrieve only the info about the remote file. it won't actually download the file, so it shouldn't slow your script down too much. <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.phpfreaks.com/media/images/forums/logo.png"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD"); curl_exec($ch); $size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD); print $size; curl_close($ch); ?> Quote Link to comment Share on other sites More sharing options...
arfa Posted October 28, 2008 Author Share Posted October 28, 2008 thanks for the 2 replies 1: looked at the wrappers info and continue to boggle at how much I don't know Any leads as to how to implement this would be much appreciated 2: Tried the curl code - thanks rhodesa Alas, it returned a big '0' Is there any way of testing that the remote host will run curl? cheers - arfa Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 28, 2008 Share Posted October 28, 2008 the host doesn't need to 'support' curl. if you run the code i posted, pointing at the PHPFREAKS image, does it work? if you remove this line: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD"); does it come back with a size? it will take longer cus it will actually download the file... Quote Link to comment Share on other sites More sharing options...
arfa Posted October 28, 2008 Author Share Posted October 28, 2008 great to hear back from you Yes, the curl code works on the phpfreaks logo I tried this on another server to eliminate that possibility. both returned 43246 but, modify the URL in the same (test) file pointing to www.archive.org and it returns '0' The full URL is: http://www.archive.org/download/ConnectionAndAlienation/20080221AVIRADHAMMO_32kb.mp3 Feed it to a browser and it loads the file -- it *is* there.!!! @$@#$@ readfile() works OK for the download script and, while I don't fully understand wrappers we have "allow_url_fopen" working???? So, what could be missing? this kind of thing is what makes it challenging/fun/... thanks again - arfa Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 28, 2008 Share Posted October 28, 2008 ah, they have a hidden forward in there....you need to follow the location: <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.archive.org/download/ConnectionAndAlienation/20080221AVIRADHAMMO_32kb.mp3"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_exec($ch); $size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD); print $size; curl_close($ch); ?> Quote Link to comment Share on other sites More sharing options...
Stephen Posted October 29, 2008 Share Posted October 29, 2008 What about something like: <?php echo "mp3 file is " . strlen(file_get_contents("http://ia310813.us.archive.org/2/items/ConnectionAndAlienation/20080221AVIRADHAMMO_32kb.mp3")) . "bytes"; ?> Quote Link to comment Share on other sites More sharing options...
arfa Posted October 29, 2008 Author Share Posted October 29, 2008 way cool for curl I see the more I learn the more I see there is to learn. Many thanks rhodesa Stephen: that could work but strlen does not accurately represent filesize. also, this whole thing with remote files means (certainly in this case) file_get_contents returns an error: file not found or some such. thanks to all - yet another case closed Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 What about something like: <?php echo "mp3 file is " . strlen(file_get_contents("http://ia310813.us.archive.org/2/items/ConnectionAndAlienation/20080221AVIRADHAMMO_32kb.mp3")) . "bytes"; ?> the reason i would recommend my method over this, is that my way should not have to download the entire file, and therefore be faster. arfa, you may also want to cache the filesize. so once it's retrieved once, you don't have to do it again... Quote Link to comment Share on other sites More sharing options...
arfa Posted October 29, 2008 Author Share Posted October 29, 2008 cache filesize - hmmmmm? The page build is around a podcast database which will be modified (hopefully) regularly. The testing I have done (on dial-up) seems quick enough for on-the-fly. It does give me the thought to running the curl script when the file is uploaded (by an non-tech volunteer) and writing the value to the database. Is there some way to test if curl is installed. It is not on my local PHP build (EasyPHP 1. and gives a fatal error. I am downloading ver2.0 >> Call to undefined function: curl_init() This is a minor point but I like to cover the bases. >> they have a hidden forward in there I studied the code and, apart from the "FOLLOWLOCATION" line couldn't see the diff. Where is the 'hidden forward'? thanks again - arfa Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 if you create a page, and put the following in it: <?php phpinfo(); ?> it will give you all sorts of information including which extensions are loaded ...the 'hidden forward' means the URL to the MP3 isn't the TRUE url. it's an alias. it's nothing to worry about. the server though was giving back a 302 status, which means the file has moved. without the FOLLOWLOCATION option, curl will stop there. by setting that to true, it will follow the alias to the file. browsers do this automatically, that is why you don't see that when you open the URL in a browser. Quote Link to comment Share on other sites More sharing options...
arfa Posted October 29, 2008 Author Share Posted October 29, 2008 Great that I am not the only one that sitting for hours poking keys and punching a mouse ) thanks for the quick turnaround. Yes, I can run phpinfo but this just gives me a list of the install. I was wondering if there was an on-the-fly test for curl, as in... if (!file_exist('sdf.txt') {...} else {...} if no curl then I would set size and '0' and bypass the whole routine Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 if(extension_loaded('curl')){ } Quote Link to comment Share on other sites More sharing options...
arfa Posted November 2, 2008 Author Share Posted November 2, 2008 Snow is on the way; wood cut but keys unpoked for a while thanks again - it works just fine - arfa Quote Link to comment 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.