Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[web] PHP function return error...

Started by sathenzar Feb 24, 2005 at 1:31 PM 3 replies 1.1k views
Original Post
sathenzar
sathenzar
Hey everyone, I have a PHP function that reads in data from a webpage and returns it. However, it's not working right and I don't know why. Here's the source below:

function loadTemp($temp_path, $db)
{
 	$temp_data = fopen($temp_path, "r");
	return $temp_data;
}


Then in my PHP document have $data = loadTemp(2, $dbh); echo "$data"; all it echos out to be is: Resource ID #19. Any one have any ideas? Thanks.
Evil Steve
Evil Steve
Quote:
Original post by sathenzar
Hey everyone,
I have a PHP function that reads in data from a webpage and returns it. However, it's not working right and I don't know why. Here's the source below:
*** Source Snippet Removed ***
Then in my PHP document have $data = loadTemp(2, $dbh); echo "$data";
all it echos out to be is: Resource ID #19. Any one have any ideas? Thanks.
fopen() doesn't read from a file, it returns a resource handle to the file. If you try to echo the return value, PHP will tell you what it's called (Resource ID #19).
You want to call fread() to read data.
sanguineraven
sanguineraven
As Steve said, the fopen() function just returns a resource handle with the filename specified in the mode you tell it to.

$handle = fopen("data.txt", "r");


You can then use this handle for future file operations such as fread() or fscanf() which take the resource handle as a parameter.

Also, don't forget to close the handle after your done with it using:

fclose($handle);


In case you arn't using it, the PHP manual will give examples of how to use the functions and give the information needed, i.e which parameters to pass. The filesystem functions are located here.

For other reference you may want to try PHP freaks or PHP builder or check out various other articles and tutorials such as this or a quick google search.

Hope this helped and good luck. :)
kanzler
kanzler
file_get_contents("location\of\file") will return the contents of a file as a string saving you from using fopen, fread and fclose. This function is also supposed to be somewhat faster than using other functions to read file contents into a string.
GroZZleR
GroZZleR
File() will return a file's contents back to you as an array, with every line being an element. Can be quite handy, and saves all the fopen, fread, fclose.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.