Page 1 of 1

File Size for Linked File

Posted: Thu Aug 06, 2009 6:18 am
by mbgritten
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:

Code: Select all

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

Code: Select all

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

Code: Select all

<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

Re: File Size for Linked File

Posted: Thu Aug 06, 2009 6:28 am
by Benjamin
I believe you are looking for basename();

See also:
pathinfo();

Re: File Size for Linked File

Posted: Thu Aug 06, 2009 6:51 am
by mbgritten
Hello,

Thanks for your quick reply. What am I doing wrong here?

Code: Select all

<?php
$path = "/home/fhlinux13/l/mydomain.co.uk/user/htdocs/";
$f = basename($path);
 
function splitFilename($f)
{
    $pos = strrpos($f, '.');
    if ($pos === false)
    { // dot is not found in the filename
        return array($f, ''); // no extension
    }
    else
    {
        $basename = substr($f, 0, $pos);
        $extension = substr($f, $pos+1);
        return array($f, $extension);
    }
} 
 
$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>";
?>
...the PHP code in the page is:

Code: Select all

<?php $f = "index.php";?>
Any ideas?

Mark

Re: File Size for Linked File

Posted: Thu Aug 06, 2009 7:52 am
by towerspl
just wondering why you need to use an absolute reference and how does that even work in the link when you click it?

Re: File Size for Linked File

Posted: Thu Aug 06, 2009 7:56 am
by mbgritten
Hello,

I'm trying to keep the link to the file as simple as possible due to other people involved in possibly editing the page. I don't need an absolute reference to the file (answering previous post in this thread).

The script is working fine for files in the current directory, but not for any files elsewhere.

Here's the script from the page that calls for a file size:

Code: Select all

<?php $file = "publications/events/myfile.pdf";?>
...and here's 'filesize.php' which is used through 'require_once':

Code: Select all

<?php
$path = "/home/fhlinux13/l/mydomain.co.uk/user/htdocs/";
$f = basename($file);
 
function splitFilename($f)
{
    $pos = strrpos($f, '.');
    if ($pos === false)
    { // dot is not found in the filename
        return array($f, ''); // no extension
    }
    else
    {
        $basename = substr($f, 0, $pos);
        $extension = substr($f, $pos+1);
        return array($f, $extension);
    }
} 
 
$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=\"$file\" class=\"pdf\" title=\"$file_size\">$file</a>";
?>
If you could indicate how to get this script working for both files within the current directory and anywhere else, I'd appreciate it!

I would like to be able to call both:

Code: Select all

<?php $file = "publications/events/myfile.pdf";?>
...and...

Code: Select all

<?php $file = "myfile.pdf";?>
Thanks,

Mark

Re: File Size for Linked File

Posted: Fri Aug 07, 2009 8:48 am
by towerspl
im not exactly sure whats going on to be honest lol but from what i can gather for some reason you need the following:


/home/sites/mysite.co.uk/httpdocs/pdf/mypdf.pdf

and also

mypdf.pdf


if so why dont you just use:

explode("/", "/home/sites/mysite.co.uk/httpdocs/pdf/mypdf.pdf")