Page 1 of 1

Displaying files from folder into the browser

Posted: Mon Aug 25, 2003 10:20 pm
by fernado1283
Hi,
I am new in PHP and i really need help from you. I am trying to retrieve the lastest file from the particular folder and display it into the browser while the rest of the files remain as downloading links for the users. Can anyone please help me with my problems. Thanks.
Regards.
Fernado

Code: Select all

<?php

	include("doc_function.inc.php");
	htmlheader(); // prints html header


	$dir = "./"; // current directory


	// Print the directory name as a heading title 
	$filedir =  getcwd();  // get current dir
	$dirname = getHeader($filedir);
	print "<br>";
	print "<font size=+1 face="Times New Roman" color="Blue"><b> ";
	print "$dirname </b>";
	print "</font> ";
	print "<hr>";


	getFiles($dir); // displays all the files in the folder
	

	getFooter($dir); // displays the "back" and "home" links

	print "</blockquote>
			<body>
			 </html>";

	$number_of_records = 0;
	$directory = "./";
	$directory_listing = array();
	recrusive_list_dir_contents($directory);
	krsort($directory_listing);
	reset($directory_listing);
	$i = 0;
	while(list($ctime, $file) = each($directory_listing))
	{
		if($i++ == $number_of_records)
			break;
		else
			$recent_files[$ctime] = $file;
	}	
	Global $recent;
	$recent=$file;
	echo "Latest file path is $recent";
	
	function recrusive_list_dir_contents($directory)
	{
		GLOBAL $directory_listing;
		if(!is_dir($directory)){
			echo "$directory is not a directory";die;
		}
		if($directory[strlen($directory)-1] != "/")
		$directory .= "/";
		$dp = opendir($directory);
		while (($file = readdir($dp)) !== false){
			if($file[0] != "." && $file != "index.php")
			{
				if(is_dir($directory.$file))
				recrusive_list_dir_contents($directory.$file);
			else
				$directory_listing[filectime($directory.$file)] .= $directory.$file;
			}
		}
			//return 0; // a mere formality
	}
$openfile=fopen ($recent ,'r+b' ); 
?> 

?>

Posted: Mon Aug 25, 2003 10:55 pm
by volka
what exactly is the problem?

Posted: Tue Aug 26, 2003 12:08 am
by fernado1283
Hi,
The problem is how am i going to display the information inside the file into the browser.

Fernado

Posted: Tue Aug 26, 2003 12:31 am
by volka
ah, ic.
That really depends on the content-type of those files.
might be as simple as

Code: Select all

echo '<div class="filecontent"><pre>';
readfile($filename);
echo '</div>';
if it is a text file with trusted content (e.g. no html)

Posted: Tue Aug 26, 2003 12:42 am
by fernado1283
Hi,
So if happens the file to be in pdf format???

Fernado

Posted: Tue Aug 26, 2003 12:56 am
by fernado1283
Hi,

Actually because all my files are in pdf format, therefore, i could not display the information in the browser. Can you help me how I should do it please. Thanks

Fernado

Posted: Tue Aug 26, 2003 1:24 am
by volka
you could let the client handle the output.
Since the <object> approach doesn't seem to work anymore for AcrobatReader you might try something like

Code: Select all

<html>
	<head></head>
	<body>
		<iframe src="test.pdf" style="float: left; width: 500px; height: 600px;"></iframe>
		<div style="float: left;">
			dummy file list
			<ul>
				<li>a file</li>
				<li>b file</li>
				<li>c file</li>
			</ul>
		</div>
	</body>
</html>

Posted: Tue Aug 26, 2003 1:47 am
by fernado1283
Hi,
Sorry, the thing dont work. I think is because im working on Mac Safari, cannot use iFrame on top of it. Is there any other methods to display the pdf file. Thanks

Fernado

Posted: Tue Aug 26, 2003 1:56 am
by volka
no iframes? o_O
ok, I don't like them as well, but ...
what happens with

Code: Select all

<html>
   <head></head>
   <body>
      <iframe src="anotherExistingHTMLFile.html" style="float: left; width: 500px; height: 600px;">sorry, can't do that</iframe>
      <div style="float: left;">
         dummy file list
         <ul>
            <li>a file</li>
            <li>b file</li>
            <li>c file</li>
         </ul>
      </div>
   </body>
</html>
and what if you enter the url of a pdf-file in your browser's navbar?

Posted: Tue Aug 26, 2003 2:23 am
by fernado1283
Hi,
Actually I also not sure where the problem lies, because it print out the code instead. This is what i did below.

Code: Select all

<?php

	include("doc_function.inc.php");
	htmlheader(); // prints html header


	$dir = "./"; // current directory


	// Print the directory name as a heading title 
	$filedir =  getcwd();  // get current dir
	$dirname = getHeader($filedir);
	print "<br>";
	print "<font size=+1 face="Times New Roman" color="Blue"><b> ";
	print "$dirname </b>";
	print "</font> ";
	print "<hr>";


	getFiles($dir); // displays all the files in the folder
	

print"
      < iframe src="$recent" style ="float: left; width: 500px; height: 600px;"></ iframe >
      < div style ="float: left;" >
      </ div >";

	getFooter($dir); // displays the "back" and "home" links

	print "</blockquote>
			</body>
			 </html>";

	$number_of_records = 0;
	$directory = "./";
	$directory_listing = array();
	recrusive_list_dir_contents($directory);
	krsort($directory_listing);
	reset($directory_listing);
	$i = 0;
	while(list($ctime, $file) = each($directory_listing))
	{
		if($i++ == $number_of_records)
			break;
		else
			$recent_files[$ctime] = $file;
	}	
	Global $recent;
	$recent=$file;
	echo "Latest file path is $recent";
	
	function recrusive_list_dir_contents($directory)
	{
		GLOBAL $directory_listing;
		if(!is_dir($directory)){
			echo "$directory is not a directory";die;
		}
		if($directory[strlen($directory)-1] != "/")
		$directory .= "/";
		$dp = opendir($directory);
		while (($file = readdir($dp)) !== false){
			if($file[0] != "." && $file != "index.php")
			{
				if(is_dir($directory.$file))
				recrusive_list_dir_contents($directory.$file);
			else
				$directory_listing[filectime($directory.$file)] .= $directory.$file;
			}
		}
			//return 0; // a mere formality
	}

?> 

?>

Posted: Tue Aug 26, 2003 3:23 am
by volka
which browser do you use and which program displays pdfs on your system?

Posted: Tue Aug 26, 2003 3:43 am
by fernado1283
Hi,
I am working on Mac OS using safari and internet explorer to view
and the program to display is preview only.

Fernado

Posted: Tue Aug 26, 2003 10:08 am
by volka
that might be the problem. There is no pdf viewer with a plugin for your browser available? I doubt there isn't.
Of course you might let the server render the pdf to another format but I'd rather save the server's time ;)

Posted: Tue Aug 26, 2003 8:39 pm
by fernado1283
Hi,
if now I wanted to open a powerpoint file and display it out, can it be done, if yes, how is it done. Please help me.

Fernado