Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/169074-solved-file-size-for-linked-file/
Share on other sites

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

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

  • 2 weeks later...

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

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

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

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.