0
votes

I have created a small PHP file which displays the name of text files in a directory as first.txt, second.txt, third.txt. On clicking on any of the files I get the error "object not found".

This does not work:

localhost/Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles/first.txt

But this works:

file:///Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles/first.txt

Can you help me to understand what is wrong?

Here is my code:

<?php

$directory = '/Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles';

if ($handle = opendir($directory.'/')){
    echo 'Looking inside'.$directory.'<br>';

    while ($file = readdir($handle)){
        if ($file != '.'&&$file != '..'){
            echo  '<a href="'.$directory.'/'.$file.'">'.$file.'</a><br>';
        }
    }
}
?>

Following is some updates of log file

  • [Thu Jul 18 01:06:57 2013] [error] [client ::1] File does not exist: /Applications/XAMPP/xamppfiles/htdocs/xampp/xamppfiles
  • [Thu Jul 18 01:15:14 2013] [error] [client ::1] File does not exist: /Applications/XAMPP/xamppfiles/htdocs/Applications, referer: localhost/learning/ListingFiles/file.php
  • [Thu Jul 18 01:15:43 2013] [error] [client ::1] File does not exist: /Applications/XAMPP/xamppfiles/htdocs/Applications, referer: localhost/learning/ListingFiles/file.php
  • [Thu Jul 18 01:17:13 2013] [error] [client ::1] File does not exist: /Applications/XAMPP/xamppfiles/htdocs/Applications, referer: localhost/learning/ListingFiles/file.php
  • [Thu Jul 18 01:39:04 2013] [error] [client ::1] File does not exist: /Applications/XAMPP/xamppfiles/htdocs/learning/ListingFiles/function.opendir, referer: localhost/learning/ListingFiles/file.php
2
Have you tried using a relative path to the get_cwd() current directory?MyStream
shot in the dark here, but i dont think localhost is part of the file structure on the machine.. have you tried Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles/first.txt ?user20232359723568423357842364
@user20232359723568423357842364 we cant just write "Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles/first.txt"Gaurav_soni
@MyStream getcwd() gives the following location '/Applications/XAMPP/xamppfiles/htdocs/Learning/ListingFiles 'Gaurav_soni

2 Answers

1
votes

Try using

$dir = "/Applications/XAMPP/xamppfiles/htdocs/learning/";

as your main path with the files in it.

This full path or "../learning" should get you to the right directory to your files.

When creating a link you'll want to use "/learning/" + filename to give the path to the file.

Thanks, MyStream

5
votes

Your link doesn't point correctly. When using xampp, the link should be relative to your htdocs folder, not your entire path:

/Learning/ListingFiles/first.txt

Note, that this would be the same link path you would use if your website were on the net using a domain.

It works when you use file:: because that explicitly states that the path is a file on your computer.