Hi all,

I'm new to PHP and at this stage I'm still learning what is and isn't possible with PHP.

I want to add a script to my website that will automatically add the file size next to any link that leads to a non-HTML document.

For example, imagine I have a webpage that links to 50 PDF files. When someone loads the page, I want a script that will loop through each link on the page and add the file-size information.

I know I can achieve this by manually adding PHP code to each link, but is there a way to set up a script that will automatically add this to all the links on a page?

In other words, rather than me having to add a PHP reference at the end of every link on the page, is there a way I can add a piece of code to the top of the page that will then, somehow, search for every link on the page and automatically add some file size code to it?

As I said, I'm still coming to terms with what is and isn't possible with PHP, so I realise that what I'm asking may be impossible.

Thanks in advance for your help. :)

A simple example... which loops through "pdf" directory, outputs all files with *.pdf extension and their size in bytes.

<?php

$dir = "./pdf";

$hnd = opendir($dir);

while($file = readdir($hnd))
{
	if(substr($file,-3) == 'pdf')
	{
		echo '<p>'.$file.': '.filesize($dir.'/'.$file).'</p>';
	}
}

closedir($hnd);

?>

A simple example... which loops through "pdf" directory, outputs all files with *.pdf extension and their size in bytes.

<?php

$dir = "./pdf";

$hnd = opendir($dir);

while($file = readdir($hnd))
{
	if(substr($file,-3) == 'pdf')
	{
		echo '<p>'.$file.': '.filesize($dir.'/'.$file).'</p>';
	}
}

closedir($hnd);

?>

Thanks for replying.

That code would solve the problem of how to get the file sizes in the first place, but how would I then distribute this information throughout the page?

The webpage itself isn't just a list of PDF links - it's a long webpage with text, headings, images, etc., and the links are scattered among everything else.

For example:

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas fermentum, felis a elementum varius, enim ante tincidunt nunc, et dapibus orci lorem vel nunc. Link to PDF

Nunc porttitor magna eget mi consequat porttitor. Link to PDF. In velit metus, ultrices et gravida a, volutpat et arcu.

  • Link to PDF
  • Link to PDF

etc...


Some links are in their own paragraph, some are in a paragraph with other text, and some are in a list. And they don't all necessarily point to the same directory - the PDFs themselves are split across several subfolders.

So I'm wondering if there's a way to get the file sizes for all the links and add that information to the end of each link wherever it appears on the page.

(I reckon I could do something like this with JavaScript, but I'm hoping to work out a way to do it in PHP so that it will all be done before the page appears in the browser.)

You are correct about Javascript being the way to go. PHP does everything BEFORE the page loads and after a submit. If you want pop up windows to display that info or maybe even a 'preview' of the file, that would best be put up with JS.

Member Avatar for diafol

How many links are we talking about here? If a lot, how about creating a regex to find each 'href="...pdf"' and the associated label '>the label</a>'.

In each instance:
1) check the existence of the file.
2) get the filesize.
3) rewrite the title attribute and/or label to include the filesize.

I hate regex/preg replace with a passion - coz I'm too stoopid to understand it - so I can't help you further - sorry.

I don't see any point to do it after the page loads, because you still need some way to get the file sizes.
Guess javascript just doesn't have the capabilities to get size of a specific file.

hi u can use while loop ,it is used to do things which are done repeatedly ,ou u can get the script by searching while loop on internet.

Thanks for all the advice.

ardav, what you suggested is sort of what I was thinking when I first came up with this idea:

How many links are we talking about here? If a lot, how about creating a regex to find each 'href="...pdf"' and the associated label '>the label</a>'.

In each instance:
1) check the existence of the file.
2) get the filesize.
3) rewrite the title attribute and/or label to include the filesize.

However, since you can't change the contents of the webpage with php after the webpage is loaded, I'm guessing all these changes would have to take place before the page loads? How would you go about doing that?

Would it be possible, in the head of a webpage, to load the rest of the page contents as a string into a variable, perform the find and replace on the variable, and then output the variable to the browser instead of the remaining contents of the page? Does that make sense?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.