PDA

Click to See Complete Forum and Search --> : Resource id #2


dandono
Apr 25th, 2005, 04:21 PM
hi, i have made this code that reads a text doc and echos it to the page. here is my code:
<?
$user = $_GET['user'];
echo '<html>
<head>
<title>Dans Web Server 2005!</title>
</head>
<body>
<h1>';
echo fopen($user . "fn.txt","r");
echo '</h1>
</body>
</html>'
?>
it is ment to echo what is in a text file (username and "fn.txt") but insted it echos Resource id #2. what is wrong with my code?
thanks, dandono

visualAd
Apr 27th, 2005, 04:45 AM
Once you have opened a file with fopen, a resource is returned. The resource holds all the information about the file, like the current position of the pointer, its name, its handle and other things.

So once you have opened your file you must use functions such as fread() (http://www.php.net/fread) and fgets() (http://www.php.net/fgets) to read the contents, which all take a file pointer resource as one of their parameters.

In your case, as you want to just dump the entire file, you should use the fpassthru() (http://www.php.net/fpassthru) function.

<?php
$user = $_GET['user'];

// assign the resource to a variable
if (! $fhwnd = fopen($user . 'fn.txt','r')) {
die('Failed to open file.');
}
?>
<html>
<head>
<title>Dans Web Server 2005!</title>
</head>
<body>
<h1><?php fpassthru($fhwnd) ?></h1>
</body>
</html>

dandono
Apr 27th, 2005, 02:41 PM
how would i be able to do this when opening more than 1 text doc?

visualAd
Apr 27th, 2005, 03:13 PM
I'm not entirely sure what you mean. But, you can have more than one file open at a time. Just assign each resource to a different varaible. Don't forget to close the file after you have finished with it by using the fclose() (http://www.php.net/fclose) function.

dandono
Apr 28th, 2005, 10:10 AM
i ment to echo one files contents and then another files contents and so on

visualAd
Apr 28th, 2005, 10:28 AM
<?php
$user = $_GET['user'];

// assign the resource to a variable
if (! $fhwnd1 = fopen($user . 'file1.txt','r')) {
die('Failed to open file 1.');
}

if (! $fhwnd2 = fopen($user . 'file2.txt','r')) {
die('Failed to open file 2.');
}
?>
<html>
<head>
<title>Dans Web Server 2005!</title>
</head>
<body>
<h1><?php fpassthru($fhwnd1) ?></h1>
<h1><?php fpassthru($fhwnd2) ?></h1>
</body>
</html>

dandono
Apr 28th, 2005, 11:46 AM
thanks, dandono.