mbgritten Posted August 6, 2009 Share Posted August 6, 2009 Hello, I have put together a script that will show the file size of a linked file (e.g. <a href="myfile.pdf" title="30.7 Kb">My File</a>). I have got it working to show the link and the file size, but I need some help to hide the file's location as it's working on an absolute reference which shows the full path to the file (see below). Here's the script: <?php $file_type = array( 'K', 'M', 'G' ); $size = filesize ( $f ); for ( $t = 0; $size > 1024; $t++ ) { $size /= 1024; $file_size = round ( $size, 1 ) . ' ' . $file_type[ $t ] . 'B'; } echo "<a href=\"$f\" class=\"pdf\" title=\"$file_size\">$f</a>"; ?> ...and here's what I code in the page to show the file link: <?php $f = "/home/fhlinux13/l/mydomain.co.uk/user/htdocs/publications/myfile.pdf";?> ...and I include the 'filesize.php' in a 'require_once' at the bottom of the page. Can anyone help to show me how to hide the absolute reference to the file? It is showing on the processed page as follows: <a href="/home/fhlinux13/l/mydomain.co.uk/user/htdocs/publications/myfile.pdf" title="823.9 KB">/home/fhlinux13/l/mydomain.co.uk/user/htdocs/publications/myfile.pdf</a> ...or show me an easier way to do this? Many thanks, Mark Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/ Share on other sites More sharing options...
ram4nd Posted August 6, 2009 Share Posted August 6, 2009 <?php $file_type = array( 'K', 'M', 'G' ); $size = filesize ( $f ); for ( $t = 0; $size > 1024; $t++ ) { $size /= 1024; $file_size = round ( $size, 1 ) . ' ' . $file_type[ $t ] . 'B'; } $file_name = substr($f, -(strlen($f) - strrpos($f,'/'))); echo "<a href=\"$f\" class=\"pdf\" title=\"$file_size\">$file_name</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-892302 Share on other sites More sharing options...
mbgritten Posted August 6, 2009 Author Share Posted August 6, 2009 Hello, Thanks for the reply - a superb bit of code! I have two other questions: a) How do you remove the trailing forward slash before the filename: Currently: <a href="publications/myfile.pdf" class="pdf" title="823.9 KB">/myfile.pdf</a> ...should be: <a href="publications/myfile.pdf" class="pdf" title="823.9 KB">myfile.pdf</a> b) How do you strip the file extension in the filename so the output shows: <a href="publications/myfile.pdf" class="pdf" title="823.9 KB">myfile</a> If you could show me how this could be done, or at least point me in the right direction I'd really appreciate it! Thanks, Mark Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-892309 Share on other sites More sharing options...
kenrbnsn Posted August 6, 2009 Share Posted August 6, 2009 Just use basename <?php $fn = '/home/fhlinux13/l/mydomain.co.uk/user/htdocs/publications/myfile.pdf'; echo basename($fn) '<br>'; echo basename($fn,'.pdf'); // will strip the extension ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-892310 Share on other sites More sharing options...
mbgritten Posted August 20, 2009 Author Share Posted August 20, 2009 Hello, I'm using the following (updated) code to generate a filesize for a linked file: <?php $file_type = array( 'K', 'M', 'G' ); $size = filesize ( $f ); for ( $t = 0; $size > 1024; $t++ ) { $size /= 1024; $file_size = round ( $size, 1 ) . ' ' . $file_type[ $t ] . 'B'; } $file_name = substr($f, -(strlen($f) - strrpos($f,'/'))); echo "<a href=\"$f\" class=\"pdf\" title=\"$file_size\">"; list($f, $ext) = explode('.', $f); echo basename($f); echo " <small>($file_size)</small></a>"; ?> ...and in the page I'd call the function by including: <?php $f = "My Event.pdf";?> ...which would echo: <a href="My Event.pdf" class="pdf" title="82 KB">My Event<small>(82 KB)</small></a> However, for some reason the output is not working as planned. Instead of showing this: Watch out for [u]My Event (82 KB)[/u] - the vital source of LOCAL information. [u]Click here for more information[/u]. ...the output is creating line breaks: Watch out for - the vital source of LOCAL information. [u]Click here for more information[/u]. My Event (82 KB) Can anyone help explain why the output is taking the link outside of the paragraph and creating line breaks that shouldn't exist? Thanks, Mark Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-902756 Share on other sites More sharing options...
kenrbnsn Posted August 24, 2009 Share Posted August 24, 2009 Can you post how you're invoking the function? Ken Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-905062 Share on other sites More sharing options...
mbgritten Posted August 25, 2009 Author Share Posted August 25, 2009 Can you post how you're invoking the function? Ken Hi Ken, I'm simply using a 'require_once' to invoke the php code listed in my original post (starting $file_type = array...). This 'require_once' is invoked at the bottom of my page of code if that helps. Mark Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-905699 Share on other sites More sharing options...
kenrbnsn Posted August 25, 2009 Share Posted August 25, 2009 First change your included code into a function and include it at the top of your PHP script. I added another function to get the filesize: <?php function my_filesize($file) { // Setup some common file size measurements. $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte // Get the file size in bytes. $size = filesize($file); /* If it's less than a kb we just return the size, otherwise we keep going until the size is in the appropriate measurement range. */ if($size < $kb) return $size." B"; else if($size < $mb) return round($size/$kb,2)." KB"; else if($size < $gb) return round($size/$mb,2)." MB"; else if($size < $tb) return round($size/$gb,2)." GB"; else return round($size/$tb,2)." TB"; } function linkit($f) { $file_size = my_filesize ( $f ); return ("<a href='$f' class='pdf' title='$file_size'>" . basename($f,'.pdf') . '</a> <span style="font-size:80%">(' . $file_size . ')</span>'); } ?> Then, where you want to have the output, do <?php echo 'Watch out for ' . linkit('My Event.pdf') . ' - the vital source of LOCAL information. <span style="text-decoration:underline">Click here for more information</span>.'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-905833 Share on other sites More sharing options...
mbgritten Posted August 26, 2009 Author Share Posted August 26, 2009 Hi Ken, Thank you very much for your help - the function (and filesize) is working very well! Mark Quote Link to comment https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/#findComment-906498 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.