• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

Link referencing wrong location

I am using the opendir to read certain files into a loop and print them in a table. This works well apart from the fact that when it comes to downloading them, they are referencing the wrong location. Instead of referencing them from the folder i set in the $dir var, they are pointing to the parent folder where my page is loaded. For example, my structure is:

users
-->destcerts

Why is it not referencing the location set in the $dir var. Thanks

<?php
   //$directory = chdir('destcerts');
   $dir = opendir('destcerts');
   echo getcwd() . "\n"; [b] --> POINTS TO USERS FOLDER WHERE MY PHP FILE IS CALLED FROM[/b]
   
   $table = '<table border="1" width="90%" cellspacing="3" cellpadding="5" align="center">
   <tr>
   <th colspan="3">Results</th>
   </tr>
   <th>File</th>
   <th>File</th>
   <th>File</th>';
   
   //List files in pdf dir
   while (($file = readdir($dir)) !== false)
   {		
   if(substr( $file, -3 ) == "pdf" )
   
   {
   //echo '<br />' . "<a href=\"" . $file . "\">" . $file . "</a>";
   
   $table .= '<tr>
   <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
   <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
   <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
   </tr>';
   
   }
   }
   
   $table .= '</table>';
   echo $table;
   
   closedir($dir);
   
   
   ?>

Open in new window

0
peter-cooper
Asked:
peter-cooper
  • 13
  • 8
1 Solution
 
Ray PaseurCommented:
Directory addresses in PHP and URL link addresses need separate consideration to work correctly.  I'll see if I can show you an example in a few moments, but in a nutshell, getcwd() will give you information that works on your server and with your PHP scripts, but you also need to know the directory structure that an internet link will use, following the cascade from your web home page through the directory list.
0
 
Ray PaseurCommented:
Here's the general use case, using relative addressing for the links.  Please look it over and post back with any questions.
http://iconoun.com/demo/temp_petercooper.php

<?php // demo/temp_petercooper.php
error_reporting(E_ALL);
echo '<pre>';

// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553444.html

/**
 * URL PATHS LOOK LIKE THIS:
 * home
 * |__demo            <-- THIS SCRIPT IS LOCATED HERE
 *    |__storage      <-- IT LISTS FILES LOCATED HERE
 *       |__file1
 *       |__file2
 *       |__etc...
 */

// WHAT IS MY CURRENT DIRECTORY?
$dir = getcwd();
echo PHP_EOL . "WEB DIRECTORY: $dir";

// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'storage';

// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
foreach ($fff as $filedata)
{
    // SKIP THE "DOT" FILES
    if (!$filedata->isDot())
    {
        // CREATE LINKS TO THESE FILES
        $nom = $filedata->getFilename();
        $lnk
        = '<a target="_blank" href="'
        . $sub
        . DIRECTORY_SEPARATOR
        . $nom
        . '">'
        . $nom
        . '</a>'
        ;
        echo PHP_EOL . $lnk;
    }
}

Open in new window

0
 
Ray PaseurCommented:
Here's how to restrict it to PDF files only.  Man page here:
http://php.net/manual/en/class.directoryiterator.php

<?php // demo/temp_petercooper.php
error_reporting(E_ALL);
echo '<pre>';

// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553444.html

/**
 * URL PATHS LOOK LIKE THIS:
 * home
 * |__demo            <-- THIS SCRIPT IS LOCATED HERE
 *    |__storage      <-- IT LISTS FILES LOCATED HERE
 *       |__file1
 *       |__file2
 *       |__etc...
 */

// WHAT IS MY CURRENT DIRECTORY?
$dir = getcwd();
echo PHP_EOL . "WEB DIRECTORY: $dir";

// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'storage';

// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
foreach ($fff as $filedata)
{
    // SKIP THE "DOT" FILES
    if (!$filedata->isDot())
    {
        // RESTRICT IT TO PDF FILES ONLY
        if ($filedata->getExtension() != 'pdf') continue;

        // CREATE LINKS TO THESE FILES
        $nom = $filedata->getFilename();
        $lnk
        = '<a target="_blank" href="'
        . $sub
        . DIRECTORY_SEPARATOR
        . $nom
        . '">'
        . $nom
        . '</a>'
        ;
        echo PHP_EOL . $lnk;
    }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peter-cooperAuthor Commented:
@Ray

Thank you very much. All I had to change was the . DIRECTORY_SEPARATOR into .'/' and it worked. How can I incorporate this into my posted code? I need to show in a table as I have posted. Many thanks
0
 
Ray PaseurCommented:
Why did you change DIRECTORY_SEPARATOR?  It's a built-in PHP constant!  Different systems use different directory separator characters, and PHP is sufficiently self-aware that it will choose the right character if you use the built-in.
http://php.net/manual/en/dir.constants.php
0
 
peter-cooperAuthor Commented:
Because it defaulted for some reason to \ instead of /. If it helps, I am using php 5.3.13. Thanks
0
 
Ray PaseurCommented:
Sorry, I don't really understand the code posted here.  It looks to me like it would be producing three identical data elements for each PDF file.  Is that what you want?  Or can you just describe the needs in plain language - perhaps we can come up with something that would make sense for a tabular display.
0
 
Ray PaseurCommented:
Interesting.  PHP 5.3 is obsolete, so there would be no value in filing a bug report.  What OS is your server using?
0
 
Ray PaseurCommented:
Or maybe not... Let me look at it again with fresh eyes.
0
 
Ray PaseurCommented:
No, I guess I'm confused.  The program logic is much easier to follow if you adopt a coding standard that includes indenting the control structures so that the logic acquires a visible quality.  Example here (with comments)

   // EACH FILE WILL BE LOCATED ONCE
   while (($file = readdir($dir)) !== false)
   {
      // IF THE FILE IS A PDF
      if(substr( $file, -3 ) == "pdf" )   
      {
         // THIS WILL CREATE THREE LINKS TO THE SAME FILE
         $table .= '<tr>
         <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
         <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
         <td align="center"><img src="destcerts/PDF_icon_100.png"><br /><a href="'.$file.'" style="font-size:12px; color:blue; text-decoration:none;">'.$file.'</a></td>
         </tr>';   
      } // END IF
   } // END WHILE

Open in new window

0
 
peter-cooperAuthor Commented:
@Ray

I am using wamp on localhost on win7 but I will be porting site to centOS + Plesk. What I am trying to achieve Ray, is if there are say 10 different files in a directory, then  display those files in a 3 column table layout. I don't wish to out put the same file. This is where I am struggling.

I take your point ray regarding formatting. Thanks
0
 
Ray PaseurCommented:
Ahh, that is a different question entirely!  I'll try to show you a general design.  We usually use arrays of data for this purpose.  Back in a few minutes...
0
 
peter-cooperAuthor Commented:
Thx Ray. Just gonna have dinner. Be back about an hour.
0
 
Ray PaseurCommented:
There are many ways to do this, but I think this is one of the easiest to understand and get right.  It uses the amazingly useful HEREDOC notation to insert the PHP variables into the HTML.  Please post back if you have any questions.
<?php // demo/temp_petercooper.php
error_reporting(E_ALL);


// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553444.html


// ALLOCATE AN ARRAY OF DATA ELEMENTS INTO A THREE-COLUMN TABLE
$datas = array( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');

// ACCUMULATE THE TABLE ROWS HERE
$trs   = NULL;

// COLLECT GROUPS OF THREE
while (!empty($datas))
{
    $td1 = array_shift($datas) or NULL;
    $td2 = array_shift($datas) or NULL;
    $td3 = array_shift($datas) or NULL;

    // USE HEREDOC TO INSERT THESE INTO A TABLE ROW
    $tr  = <<<EOD
<tr>
<td>$td1</td>
<td>$td2</td>
<td>$td3</td>
</tr>
EOD;

    // APPEND THE TABLE ROW TO THE OTHER ROWS
    $trs .= $tr;
}

// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table>
<tr colspan="3">Results</tr>
<tr>
<th>File</th>
<th>File</th>
<th>File</th>
</tr>
$trs
</table>
EOD;

// SHOW THE WORK PRODUCT
echo $tab;

Open in new window

0
 
Ray PaseurCommented:
And putting it all together, we would have something like this:
http://iconoun.com/demo/temp_petercooper.php
<?php // demo/temp_petercooper_old.php
error_reporting(E_ALL);


// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553444.html


/**
 * URL PATHS LOOK LIKE THIS:
 * home
 * |__demo            <-- THIS SCRIPT IS LOCATED HERE
 *    |__storage      <-- IT LISTS FILES LOCATED HERE
 *       |__file1
 *       |__file2
 *       |__etc...
 */

// WHAT IS MY CURRENT DIRECTORY?
$dir = getcwd();
echo PHP_EOL . "WEB DIRECTORY: $dir<br>";

// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'storage';

// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
    // SKIP THE "DOT" FILES
    if ($filedata->isDot()) continue;

    // ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
    // if ($filedata->getExtension() != 'pdf') continue;

    // CREATE LINKS TO THESE FILES
    $nom = $filedata->getFilename();
    $lnk
    = '<a target="_blank" href="'
    . $sub
    . DIRECTORY_SEPARATOR
    . $nom
    . '">'
    . 'File'
    . '</a>'
    ;

    // COLLECT THE LINKS HERE
    $sss[] = $lnk;
}

// ACCUMULATE THE TABLE ROWS HERE
$trs   = NULL;

// COLLECT GROUPS OF THREE
while (!empty($sss))
{
    $td1 = array_shift($sss) or NULL;
    $td2 = array_shift($sss) or NULL;
    $td3 = array_shift($sss) or NULL;

    // USE HEREDOC TO INSERT THESE INTO A TABLE ROW
    $tr  = <<<EOD
<tr>
<td>$td1</td>
<td>$td2</td>
<td>$td3</td>
</tr>
EOD;

    // APPEND THE TABLE ROW TO THE OTHER ROWS
    $trs .= $tr;
}

// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table>
<tr colspan="3">Results</tr>
<tr>
<th>File</th>
<th>File</th>
<th>File</th>
</tr>
$trs
</table>
EOD;

// SHOW THE WORK PRODUCT
echo $tab;

Open in new window

0
 
peter-cooperAuthor Commented:
@Ray

Thanks very much. However, some of this is quire new to me. Firstly, in my original code, I had an image above the link in the td tag. How do I incorporate that into your code? Lastly, is there a way to capture the size and date of the files. Thanks very much
0
 
peter-cooperAuthor Commented:
@Ray

Sorted out the image in link. However, can you help with size and date? Thanks
0
 
Ray PaseurCommented:
See near lines 46-50.
<?php // demo/temp_petercooper_old.php
error_reporting(E_ALL);


// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553444.html


/**
 * URL PATHS LOOK LIKE THIS:
 * home
 * |__demo            <-- THIS SCRIPT IS LOCATED HERE
 *    |__storage      <-- IT LISTS FILES LOCATED HERE
 *       |__file1
 *       |__file2
 *       |__etc...
 */

// WHAT IS MY CURRENT DIRECTORY?
$dir = getcwd();
echo PHP_EOL . "WEB DIRECTORY: $dir<br>";

// WHAT IS IN THE STORAGE DIRECTORY
$sub = 'storage';

// READ THE NAMES OF FILES IN THE SUB-DIRECTORY
$fff = new DirectoryIterator($sub);
$sss = array();
foreach ($fff as $filedata)
{
    // SKIP THE "DOT" FILES
    if ($filedata->isDot()) continue;

    // ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
    // if ($filedata->getExtension() != 'pdf') continue;

    // CREATE LINKS TO THESE FILES
    $nom = $filedata->getFilename();
    $lnk
    = '<a target="_blank" href="'
    . $sub
    . DIRECTORY_SEPARATOR
    . $nom
    . '">'
    . $nom
    . '</a>'
    . '<br>'
    . number_format( $filedata->getSize() )
    . ' bytes'
    . '<br>'
    . date('Y-m-d g:ia', $filedata->getMtime())
    . PHP_EOL
    ;

    // COLLECT THE LINKS HERE
    $sss[] = $lnk;
}

// ACCUMULATE THE TABLE ROWS HERE
$trs   = NULL;

// COLLECT GROUPS OF THREE
while (!empty($sss))
{
    $td1 = array_shift($sss) or NULL;
    $td2 = array_shift($sss) or NULL;
    $td3 = array_shift($sss) or NULL;

    // USE HEREDOC TO INSERT THESE INTO A TABLE ROW
    $tr  = <<<EOD
<tr>
<td>$td1</td>
<td>$td2</td>
<td>$td3</td>
</tr>
EOD;

    // APPEND THE TABLE ROW TO THE OTHER ROWS
    $trs .= $tr;
}

// USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
$tab = <<<EOD
<table>
<tr colspan="3">Results</tr>
<tr>
<th>File</th>
<th>File</th>
<th>File</th>
</tr>
$trs
</table>
EOD;

// SHOW THE WORK PRODUCT
echo $tab;

Open in new window

0
 
peter-cooperAuthor Commented:
@Ray

Thanks Ray. Points well deserved.
0
 
peter-cooperAuthor Commented:
Thanks once again ray for your time and patience.
0
 
Ray PaseurCommented:
Glad you've got it pointed in the right direction!  Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 13
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now