1

Why is the PHP variable defined as $real in the code below empty but still set after one iteration of the while loop yet the $entry variable is both set and contains the correct value on every iteration of the loop. Does the calling of a function '' put the resulting variable outside of the scope and if so how can I fix this?

    <!DOCTYPE html>
 <html>

   <head>
       <meta charset="utf-8">
        <title>Total </title>
          <link type="text/css" rel="stylesheet" href="course.css"> 
           <script src="../jquery.js"></script>
            <script src="../jquery-ui.js"></script>

   </head>
   <body>


  <?php 
      if ($handle = opendir('../uploads')) 
      {
          while (false !== ($entry = readdir($handle))) 
         {

          if ($entry != "." && $entry != "..") 
          {                
            $real= realpath($entry);
              echo  "<button class='files accordian'>$entry $real</button>";
              $real= realpath($entry);

                 if(isset($real))
                    {
                      echo 'the real varibale is set';

                    }
                    else
                    {
                     echo 'the real varibale is not set';
                    }

                 if(empty($real))
                    {
                    echo 'the real varibale is empty';
                    }
                    else
                    {
                    echo 'the real varibale is not empty';
                     echo 'and is '. $real;
                    }

                echo  "<button class='files accordian'>$entry $real</button>
                         <div class='panel'>
                            <p>$real</p>";
          }

        }       
     } 

   ?>  


   </body>
  </html> 

enter image description here

6
  • Do you have any error checking?Add this to the top of your code add tell the error which is occurring: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Commented Jun 6, 2017 at 12:02
  • 2
    And to be honest i have't understood your question. Commented Jun 6, 2017 at 12:03
  • Please read minimal reproducible example and edit your question so as we can all understand what is wrong here. Commented Jun 6, 2017 at 12:04
  • from the docs: "Note: The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE." Commented Jun 6, 2017 at 12:04
  • Are you sure that echo statement is correct? Maybe try something like this: echo "<button....>".$entry." ".$real."</button>"; ? Commented Jun 6, 2017 at 12:07

1 Answer 1

3

Please change your code where you are getting realpath like below,

$real= realpath("../uploads/".$entry);

Issue: you are just trying to get the realpath of a file. So, it is looking the file in your current path where you are executing your file. Hence, you need to append the origin too.

Sign up to request clarification or add additional context in comments.

5 Comments

This is great and works but do you have any idea why it worked on the first iteration of the loop - see photo above ,but not the second. I have amended code and all work now but juts intrigued as to why it is different after the first iteration of the loop? Thanks
Yes, it might work but only for one time. It will work only if you have same file name in both the folders. For your case, it was '.DS_Store'. This file was found in your current folder & in uploads.
@jon, I just tested the code myself. My previous comment was not 100% correct. Actually, it will give you as many realpaths as number of files names match between the 2 folders. if you have 10 files in your current folder matching the files in your uploads directory then it will give you realpaths for all these 10 files.
I am trying to let the user see the file, would this be the path I would need to place in <a href=""> to get the user to see the file if they clicked on this link currently I am getting this result when I do that "The requested URL /Applications/MAMP/htdocs/tutorvid/uploads/Applicatgion Text.pages was not found on this server" please see stackoverflow.com/questions/44400055/get-file-url-schema
@jon, I just posted an answer on the other question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.