Jump to content

[SOLVED] Files in a directory below webroot not linking, (files in an array)


CG_dude

Recommended Posts

Hello,

I have a php page in the webtoot called testprodcyc.php which the webroot is /users/apache/hpws/apache/htdocs.  From the testprodcyc page, there is a link to an archive page, that runs a php script from the webroot. ( The php script is located on the webroot as well). However, the script runs an array and lists all the files that are located in a directory, that is directly 1 level below the webtoot.

PHP script located /users/apache/hpws/apache/htdocs/prod_report_archives.php

The directory the files are located in is "/users/apache/hpws/apache/htdocs/prod_report_archives/"

My problem is that the script works great except that it will not link the files in the lower directory, these files are HTML files.  The array displayes the HTML files, but when you go to click on the files to view it doesn't work.  My script is wrong somewhere, but I can't figure it out.

Here is the script, any help will be appreciated.

 

<?php

// open this directory

$myDirectory = opendir("/users/apache/hpws/apache/htdocs/prod_report_archives");

$TheFileName = "prod_report_archives.php";

 

 

 

// get each entry

while($entryName = readdir($myDirectory)) {

$dirArray[] = $entryName;

}

 

// close directory

closedir($myDirectory);

 

 

// count elements in array

$indexCount = count($dirArray);

//Print ("$indexCount files<br>\n");

 

// sort 'em

sort($dirArray);

 

// print 'em

print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");

print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");

// loop through the array of files and print them all

for($index=0; $index < $indexCount; $index++) {

        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files   

print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");

print("<td>");

print(filetype($dirArray[$index]));

print("</td>");

print("<td>");

print(filesize($dirArray[$index]));

print("</td>");

print("</TR>\n");

 

}

}

 

print("</TABLE>\n");

 

 

?>

 

 

 

 

 

 

 

 

<style type="text/css">

<!--

h7

  {

  color:303065;

  font-size:12pt;

  font-style:italic;

  }

-->

</style>

<h7><center>Archives are available for a 18 month period</h7>

 

I should be able to click on the html files the array gives me and view what each file has in it.

Link to comment
Share on other sites

I should mention if I place the PHP script in the prod_report_archives directory instead of placing it on the webroot, it will work, however, the array catches the php script and publishes as a file along witht eh html files.  I want to be able to keep the script in the webroot.  Thanks,

Link to comment
Share on other sites

This is very confusing to me.

I don't understand what you want.

 

Please simplify your post.

To the best of my understanding, you could just have said this:

This script should output a table showing the filename, filetype, and filesize of all of the folders in the directory of the script being executed.

 

Everything works, except for the link in the filename field isn't working correctly.

I am expecting a link to the file. Instead, I am getting __________________.

 

Relevant code is posted below:


<?php
// open this directory
$myDirectory = opendir("/users/apache/hpws/apache/htdocs/prod_report_archives");
$TheFileName = "prod_report_archives.php";



// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);


// count elements in array
$indexCount   = count($dirArray);
//Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");

// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
    if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files       
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
        print("<td>");
        print(filetype($dirArray[$index]));
        print("</td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}

print("</TABLE>\n");
?>

 

By the way, $myDirectory points at prod_report_archives.. is that where it should be pointing if you place it in htdocs like you mentioned?

Link to comment
Share on other sites

Sorry if to you it was confusing, and thanks for the reply, but did not see where you suggested a change, and yes, the mydirectory points to and is opened so that the array can take place.

Example; the array publishes on the screen 4 files, named file1.html, file2.html, file3.html, and file4.html.  The files are in blue, so they are being linked, but when you click on the files, you get nothing.

Where the file type and file size is it give me these errors

 

For file Type

Warning: lstat failed for (errno=2 - No such file or directory) in /users/apache/hpws/apache/htdocs/prod_report_archives.php on line 77

( The line in refers to is this "print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");"

 

same error for file size but different line, but will be caused by the same issue.

Link to comment
Share on other sites

I have solved this puzzle, the issue was that it needed the correct HREF link.  Took me awhile, but the more I analyzed the code, I realized it had to be with the link, so made some adjustments and it is working like it should.  Here is the correct code

 

 

<?php

// open this directory

$myDirectory = opendir("/users/apache/hpws/apache/htdocs/prod_report_archives");

 

// get each entry

while($entryName = readdir($myDirectory)) {

    $dirArray[] = $entryName;

}

 

// close directory

closedir($myDirectory);

 

 

// count elements in array

$indexCount  = count($dirArray);

//Print ("$indexCount files<br>\n");

 

// sort 'em

sort($dirArray);

 

// print 'em

print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");

print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");

 

// loop through the array of files and print them all

for($index=0; $index < $indexCount; $index++) {

    if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files     

        print("<TR><TD><a href=http://keynes:8080/prod_report_archives/$dirArray[$index]>$dirArray[$index]</a></td>");

        print("<td>");

        print(filetype($dirArray[$index]));

        print("</td>");

        print("<td>");

        print(filesize($dirArray[$index]));

        print("</td>");

        print("</TR>\n");

    }

}

 

print("</TABLE>\n");

?>

 

 

Link to comment
Share on other sites

Alright, I spent a while giving your script a makeover, so I'm hoping this will get us a little closer.

I commented each line that wasn't 100% self-explanatory, and added an error message if any function returned anything unexpected. So give it a shot, and let me know what it does.

 

By the way, there was a typo in your "don't list hidden files" line. You treated the variable as a string, so that line always evaluated the condition:

 

if ("$" != ".") {
//If "$" is not equal to ".", then print a row of data.
//Since this condition is always true, hidden files would still be shown.
}

 

So hidden files would still be shown. Anyway, here's the shiny new script for you.

 

<?php
$myDirectory = '"/users/apache/hpws/apache/htdocs/prod_report_archives"'; //Working directory.

if (is_dir($myDirectory)) { //Check to make sure $myDirectory is actually a directory. Expected to return true.
if ($myDirectory = opendir($myDirectory)) { //Open directory  $myDirectory (expected to return a resource).

	while ($entryName = readdir($myDirectory)) { //Read filenames. Expected to return filenames, and then return false as the list ends.
		$dirArray[] = $entryName; //Add filenames to the array $dirArray
	}

	closedir($myDirectory); //Close open directory $myDirectory.
	if ($indexCount = count($dirArray)) { //Counts elements in an array. Expected to return an integer.
		if (sort($dirArray)) { //Sort array $dirArray. Expected to return true.

			print('<table border="1" cellpadding="5" cellspacing="0" class="whitelinks">' . "\n"); //Print table.
			print('<tr><th>Filename</th><th>Filetype</th><th>Filesize</th></tr>' . "\n"); //Print first row of table with titles.

			// loop through the array of files and print them all
			for($index=0; $index < $indexCount; $index++) {
				if (substr($dirArray[$index], 0, 1) != "."){ //Check to make sure that the filename doesn't begin with a period.      
					$fileName = $dirArray[$index]; //Rename the filename variable (for the sake of cleanliness.
					if filetype($dirArray[$index] = $fileType) { //Rename the filetype variable (for the sake of cleanliness.
							if (filesize($dirArray[$index]) = $fileSize) { //Rename the filesize variable (for the sake of cleanliness).
								print('<tr>' . "\n" . '<td>'); //Start row, newline, start first column.
								print('<a href="' . $fileName . '">' . $fileName . '</a>' . "\n"); //Insert link to file (with filename) in second column.
								print('</td>' . "\n" . '<td>'); //End first column, newline, start second column.
								print($fileType); // Insert filetype into second column.
								print('</td>' . "\n" . '<td>'); //End second column, newline, start third column.
								print($fileSize); //Insert filesize into third column.
								print('</td></tr>' . "\n"); //End last column, end row.

							} else {
								print ('Error: Could not distinguish the file size.');
							}
					} else {
						print ('Error: Could not distinguish file type.');
					}
				} else {
					//File was hidden.
				}
			}
			print('</table>' . "\n"); //End table
		}


	} else {
		print ('Error: Could not count elements in array.');
	}
} else {
	print ('Error: Couldn\'t open directory.');
}
} else {
print ('Error: $myDirectory is not a valid directory.');
}
?>

Link to comment
Share on other sites

Just read your message. Fixed code:

 


<?php
$myDirectory = '/users/apache/hpws/apache/htdocs/prod_report_archives'; //Working directory.

if (is_dir($myDirectory)) { //Check to make sure $myDirectory is actually a directory. Expected to return true.



if ($myDirectory = opendir($myDirectory)) { //Open directory  $myDirectory (expected to return a resource).










while ($entryName = readdir($myDirectory)) { //Read filenames. Expected to return filenames, and then return false as the list ends.







$dirArray[] = $entryName; //Add filenames to the array $dirArray





}










closedir($myDirectory); //Close open directory $myDirectory.





if ($indexCount = count($dirArray)) { //Counts elements in an array. Expected to return an integer.







if (sort($dirArray)) { //Sort array $dirArray. Expected to return true.









print('<table border="1" cellpadding="5" cellspacing="0" class="whitelinks">' . "\n"); //Print table.









print('<tr><th>Filename</th><th>Filetype</th><th>Filesize</th></tr>' . "\n"); //Print first row of table with titles.









// loop through the array of files and print them all









for($index=0; $index < $indexCount; $index++) {











if (substr($dirArray[$index], 0, 1) != "."){ //Check to make sure that the filename doesn't begin with a period.      













$fileName = $dirArray[$index]; //Rename the filename variable (for the sake of cleanliness.













if filetype($dirArray[$index] = $fileType) { //Rename the filetype variable (for the sake of cleanliness.

















if (filesize($dirArray[$index]) = $fileSize) { //Rename the filesize variable (for the sake of cleanliness).



















print('<tr>' . "\n" . '<td>'); //Start row, newline, start first column.



















print('<a href="http://keynes:8080/prod_report_archives/' . $fileName . '">' . $fileName . '</a>' . "\n"); //Insert link to file (with filename) in second column.



















print('</td>' . "\n" . '<td>'); //End first column, newline, start second column.



















print($fileType); // Insert filetype into second column.



















print('</td>' . "\n" . '<td>'); //End second column, newline, start third column.



















print($fileSize); //Insert filesize into third column.



















print('</td></tr>' . "\n"); //End last column, end row.




































} else {



















print ('Error: Could not distinguish the file size.');

















}













} else {















print ('Error: Could not distinguish file type.');













}











} else {













//File was hidden.











}









}









print('</table>' . "\n"); //End table







}



















} else {







print ('Error: Could not count elements in array.');





}



} else {





print ('Error: Couldn\'t open directory.');



}
} else {



print ('Error: $myDirectory is not a valid directory.');
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.