Solved

Link referencing wrong location

Posted on 2014-11-08
21
84 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:peter-cooper
Comment Utility
@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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Because it defaulted for some reason to \ instead of /. If it helps, I am using php 5.3.13. Thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Or maybe not... Let me look at it again with fresh eyes.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:peter-cooper
Comment Utility
@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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thx Ray. Just gonna have dinner. Be back about an hour.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
@Ray

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
@Ray

Thanks Ray. Points well deserved.
0
 

Author Closing Comment

by:peter-cooper
Comment Utility
Thanks once again ray for your time and patience.
0
 
LVL 108

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now