Page 1 of 1

[SOLVED] Can't put output from function to a file...

Posted: Wed Aug 04, 2004 6:22 pm
by tomfra
Ok here is the problem...

There is a function called ShowAmazon() that grabs some content from Amazon through their provided XML. There is no problem with this function and it works just fine.

You can see some test output of it at http://www.key2marketing.com/amazon/ama ... p?com=show . As you can see, it does its job.

Now I would like to put this output into a file instead of screen. I created this function:

Code: Select all

function file_put_body ($filename, $file_body) {
  $tempname = tempnam($file_path, $filename);
    $handle = fopen($tempname, 'wb');
    fwrite($handle, $file_body);
    fclose($handle);
      unlink ($filename);
      rename($tempname, $filename);
      chmod ($filename, 0777);  
  return $file_body;
}
It may seem a little strange but it works, I've used it before with no problem.

Then let's put the output to a file...

Code: Select all

$file = 'replace.html';
file_put_body ($file, ShowAmazon());
But this results in empty output which is then written to $file. I am obviously doing something very wrong but can't figure out how to make it put there the right content.

Any help is *very* welcome.

Thanks!

Tomas

Posted: Wed Aug 04, 2004 6:44 pm
by LiquidPro

Code: Select all

function file_put_body($file_name, $file_body) {
   $fp = fopen($file_name, "w+");
   fwrite($fp, $file_body);
   fclose($fp);

   return $file_body;
}
Try using that, it however does get rid of your calls to your... tempnam() and rename() function. I don't see any purpose of calling these, you can output directly to your final file. You don't necessarily need to write to a temporary one, then rename it, then delete it... it's just causing PHP to do more work and move slower.

Try what I provided, let me know how it turns out

Posted: Wed Aug 04, 2004 8:25 pm
by tomfra
LiquidPro,

It didn't help. I should say that there is no problem with the file_put_body function itself, it does write the content properly. The problem is that the second argument in the function call - i.e. file_put_body ($file, ShowAmazon()); is the ShowAmazon function which in this case does not return any results for some reason.

Why is my file_put_body function so complicated? There is a very good reason for it. When the file exists and you need to replace something in it, you will be confronted with a permission problem very often because most files have MOD 644 which means they are writable only by the owner which usually is not PHP.

But if you CHMOD your folder to 666 or 777 (or another MOD that makes it writable by any user), you can leave the files there with their default MOD 644. But even when the directory is writable by anyone, you can't replace parts of files that are not writable by the user ID used by PHP.

But you can delete them. So I write the contents from that file to a temporary one first, unlink the original file and then rename the temp file to the filename used by the original one. Why? Because the file is now created by PHP and therefore PHP can write to it even when the MOD is 644 because it's the file owner.

In other words, as long as the directory is writable by PHP it's not important what MOD the files in it have. That's very useful for what I need.

So again, there is no problem with the file_put_body function. When I use something like:

Code: Select all

file_put_body ($file, 'Write This...');
... it works just fine, the 'Write This...' string will be put to the file.

Tomas

Posted: Wed Aug 04, 2004 10:15 pm
by LiquidPro
Well.. my other thing would be to make sure that you return an actual value from the ShowAmazon() function...

Posted: Wed Aug 04, 2004 10:28 pm
by d3ad1ysp0rk
and make sure "ShowAmazon" is the correct capitalization.

Posted: Wed Aug 04, 2004 10:52 pm
by tomfra
Yes, ShowAmazon is the correct form. There is no problem with calling the function and having the results output in the browser. For example this is what I get by calling the function:

http://www.key2marketing.com/amazon/ama ... p?com=show

I used this function for this (the parameters changed since my earlier post):

Code: Select all

ShowAmazon('key2marketing--20', 'php', 'bold', 3); // Amazon ID, Keyword, Bold keyword, # of books displayed
But when I want to use it as a string for output to file, it does not work. And when I do something like:

Code: Select all

$ShowAmazon = ShowAmazon('key2marketing--20', 'php', 'bold', 3);
var_dump($ShowAmazon);
... it returns NULL - and displays the output in browser before. It looks like the result of the ShowAmazon function is not a string? I am still too unskilled scripter to find the problem I am afraid...

Tomas

Posted: Wed Aug 04, 2004 11:00 pm
by d3ad1ysp0rk
It isn't returning anything. It's outputting directly to the browser..

Basically, you can search and replace every "echo " with "$content .= " and then add "return $content;" to the end.

Posted: Wed Aug 04, 2004 11:15 pm
by tomfra
That will be it LiLpunkSkateR! I thought about it before but then it got lost from my head and I forgot about it completely... Well, that will teach me never to play with PHP at 3AM... ;)

I'll try it but I am quite sure it will do the trick.

Thanks!

Tomas

Posted: Thu Aug 05, 2004 1:18 am
by LiquidPro
LiquidPro wrote:Well.. my other thing would be to make sure that you return an actual value from the ShowAmazon() function...
That is what I meant by that...

LiLpunkSkateR thanks for saying it again.

Sorry if I wasn't specific enough, this is my first time helping people on a PHP forum like this... I guess I need to learn how to be extremely specific with everything.

Posted: Thu Aug 05, 2004 1:29 am
by d3ad1ysp0rk
yea, a lot of times it's more of a "hey, this is what you did wrong, and he's the solution", more than a "hey, heres what you did wrong", because we all know that nobody likes to find the solution at 3am. :)

Posted: Thu Aug 05, 2004 1:40 am
by LiquidPro
Yeah, I guess I could have gone and told him that it wasn't returning a variable, instead of saying it wasn't returning anything... lol

Posted: Thu Aug 05, 2004 1:42 am
by d3ad1ysp0rk
lol, i think he took "its not returning anything" as meaning nothing is happening, showing up, etc.
but something was showing up, because it was being echoed, not returned.

you'll get used to it eventually. heh