Displaying files from folder into the browser

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Displaying files from folder into the browser

Post 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' ); 
?> 

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what exactly is the problem?
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post by fernado1283 »

Hi,
The problem is how am i going to display the information inside the file into the browser.

Fernado
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post by fernado1283 »

Hi,
So if happens the file to be in pdf format???

Fernado
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post 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
	}

?> 

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

which browser do you use and which program displays pdfs on your system?
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
fernado1283
Forum Newbie
Posts: 22
Joined: Mon Aug 25, 2003 2:00 am

Post 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
Post Reply