2

I have a text file in the same folder as the script I'm trying to run. It has several URL links each on a new line like so:

hxxp://www.example.com/example1/a.doc
hxxp://www.example.com/example2/b.xls
hxxp://www.example.com/example3/c.ppt

I'm trying to link these files but it only lists the last file in the list.

Here is my code:

<?php

    $getLinks = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/links.txt');
    $files = explode("\n", $getLinks);

    foreach ($files as $file) {

        if (substr($file, 0, 23) == 'hxxp://www.example.com/') {
            $ext = pathinfo(strtolower($file));
            $linkFile = basename(rawurldecode($file));

            if ($ext['extension'] == 'doc') {
                echo '<a href="' . $file . '"><img src="images/word.png" />&nbsp;' . $linkFile . '</a><br />';
            } elseif ($ext['extension'] == 'xls') {
                echo '<a href="' . $file . '"><img src="images/excel.png" />&nbsp;' . $linkFile . '</a><br />';
            } elseif ($ext['extension'] == 'ppt') {
                echo '<a href="' . $file . '"><img src="images/powerpoint.png" />&nbsp;' . $linkFile . '</a><br />';
            }
        }
    }

?>

*note: I've tried using the file function as well with the same results.

Ryan Mortier
  • 763
  • 1
  • 10
  • 23

2 Answers2

1

You can improve this code in many ways:

  1. Use file instead of file_get_contents to have the lines put into an array automatically
  2. Use strpos instead of substr -- more efficient
  3. Use strrpos to get the file extension -- faster and foolproof, as know exactly how it behaves
  4. You should be using rawurlencode instead of rawurldecode because you are creating urls, not reading them
  5. The if conditionals for the extensions should be replaced by an array lookup

Making all these changes, we have:

$lines = file($_SERVER['DOCUMENT_ROOT'] . '/links.txt');

$extensions = array(
    'doc' => 'word.png',
    'xls' => 'excel.png',
    'ppt' => 'powerpoint.png',
);

foreach ($lines as $file) {
    if (strpos($file, 'hxxp://www.example.com/') !== 0) {
        continue;
    }

    $ext = strtolower(substr($file, strrpos($file, '.') + 1));

    if (empty($extensions[$ext])) {
        continue;
    }

    printf('<a href="%s"><img src="images/%s" />&nbsp;%s</a><br />',
           $file, $extensions[$ext], rawurlencode(basename($file)));
}
Jon
  • 428,835
  • 81
  • 738
  • 806
0
$getLinks = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/links.txt');
$files = explode("\r\n", $getLinks);

I'm assuming you're on windows, the same as me.

\n isn't the whole windows new line character Use \r\n

When I replaced \n with \r\n it worked as expected

Adam Casey
  • 1,600
  • 12
  • 21
  • Use PHP_EOL instead of "\r\n" that way your code is portable between nix, windows, mac. $files = explode(PHP_EOL, $getLinks); http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Matt Wonlaw Mar 21 '11 at 18:23
  • @mlaw: That's not exactly correct. In this case I believe the problem is that the OP is processing a Windows-style EOL file with PHP running on nix. PHP_EOL won't help with that. – Jon Mar 21 '11 at 18:28