steviez Posted August 27, 2008 Share Posted August 27, 2008 Hi, I am developing a new image hosting script for my site and would like to track the bandwidth each image uses, Is there a way to do this in PHP and if so can someone please point me in the right direction Thanks Quote Link to comment Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 Assuming you're serving the file using a PHP script, simply make a filesize() call before you read/serve the image and store it wherever. Quote Link to comment Share on other sites More sharing options...
steviez Posted August 27, 2008 Author Share Posted August 27, 2008 How does that track bandwidth? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 27, 2008 Share Posted August 27, 2008 You track the filesize before serving up the image, and store it, adding to a running total for that image. Quote Link to comment Share on other sites More sharing options...
steviez Posted August 27, 2008 Author Share Posted August 27, 2008 any demos on how to do this? Quote Link to comment Share on other sites More sharing options...
benphelps Posted August 28, 2008 Share Posted August 28, 2008 <?php $file = $_GET['image']; // no trailing "/" for the image directory $imagedir = '.'; $bandwidthused = filesize($imagedir.'/'.$file) or ; ?> This would work if the image is called from the URL like this "http://domain.com/load.php?image=sample.jpg" From there the $bandwidthused var. will store the bandwidth the image used in bytes. Quote Link to comment Share on other sites More sharing options...
Prismatic Posted August 28, 2008 Share Posted August 28, 2008 You can even use mod_rewrite to make your urls look like valid image URL's yet point to your script http://www.yoursite.com/images.php?name=bleh.jpg vs http://www.yoursite.com/images/bleh.jpg Quote Link to comment Share on other sites More sharing options...
steviez Posted August 28, 2008 Author Share Posted August 28, 2008 thanks for all your help guys, i have got it working now Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted August 28, 2008 Share Posted August 28, 2008 <?php $file = $_GET['image']; // no trailing "/" for the image directory $imagedir = '.'; $bandwidthused = filesize($imagedir.'/'.$file) or ; ?> This would work if the image is called from the URL like this "http://domain.com/load.php?image=sample.jpg" From there the $bandwidthused var. will store the bandwidth the image used in bytes. This would be a unsafe way to do this and shouldn't be used on a production site. Be sure to include some error handling and sanitize your incoming variables. 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.