Solved

Link referencing wrong location

Posted on 2014-11-08
21
87 Views
Last Modified: 2014-11-09
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
Comment
Question by:peter-cooper
  • 13
  • 8
21 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430249
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430267
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430269
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:peter-cooper
ID: 40430425
@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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430456
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
 

Author Comment

by:peter-cooper
ID: 40430463
Because it defaulted for some reason to \ instead of /. If it helps, I am using php 5.3.13. Thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430468
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430469
Interesting.  PHP 5.3 is obsolete, so there would be no value in filing a bug report.  What OS is your server using?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430472
Or maybe not... Let me look at it again with fresh eyes.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430483
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
 

Author Comment

by:peter-cooper
ID: 40430486
@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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430491
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
 

Author Comment

by:peter-cooper
ID: 40430492
Thx Ray. Just gonna have dinner. Be back about an hour.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40430565
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40430621
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
 

Author Comment

by:peter-cooper
ID: 40430982
@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
 

Author Comment

by:peter-cooper
ID: 40431094
@Ray

Sorted out the image in link. However, can you help with size and date? Thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40431279
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
 

Author Comment

by:peter-cooper
ID: 40431296
@Ray

Thanks Ray. Points well deserved.
0
 

Author Closing Comment

by:peter-cooper
ID: 40431297
Thanks once again ray for your time and patience.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40431313
Glad you've got it pointed in the right direction!  Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question