Jump to content

remote filesize


arfa

Recommended Posts

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

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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  :)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.8) 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.