File Size for Linked File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

File Size for Linked File

Post 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
Last edited by Benjamin on Thu Aug 06, 2009 6:27 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: File Size for Linked File

Post by Benjamin »

I believe you are looking for basename();

See also:
pathinfo();
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

Re: File Size for Linked File

Post 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
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: File Size for Linked File

Post 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?
mbgritten
Forum Newbie
Posts: 7
Joined: Tue Jul 28, 2009 2:18 am

Re: File Size for Linked File

Post 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
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: File Size for Linked File

Post 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")
Post Reply