Jump to content

file_put_contents() problems


kidinbed

Recommended Posts

I'm trying to create a an admin backend page for a food truck website. They need to be able to update their menu on the page pretty frequently, so I created a page that would use

file_put_contents() to replace the text in an html file that I would display on the page in iframes, this would be the menu text. I have everything coded and working except

file_put_contents() will not work at all. I'm using Go Daddy hosting, and they said that I could create an .ini file turning whatever setting it is that controls file_put_contents(), but I have tried numerous setting and can't get anywhere. This is the php code I'm working with:

 

<?php

$menu_text = $_POST['updateleft'];
$html_begin = "<html><body><table bgcolor='black'><td><tr><font size='5' color='yellow'>";
$html_end = "</font></tr></td></table></body></table></html>";
$final_page = $html_begin.$menu_text.$html_end;

$file_name = 'menuleft.html';

file_put_contents($final_page,$file_name);
header("location:http://www.timbertheband.com/menu/menuadmin.html");
?>

 

and this is the admin page, not working:

 

http://www.timbertheband.com/menu/menuadmin.html

 

I'm not married to file_put_contents() or even this method so any suggestions at all I'm open to hearing. I've been working on this problem over a month now and I really need to get it figured out.  The idea is I just need to have a way for them to update their menu and I don't want to create a database or anything, just keep it as simple as possible.

Link to comment
Share on other sites

not creating a database makes things much more difficult. if you stored the information in a database you wouldn't have to write an HTML file at all. you could just have PHP display the contents of the database. if you are married to not using a database, perhaps you should try fopen() and fwrite().

Link to comment
Share on other sites

There is no php.ini setting that controls file_put_contents.

 

What php errors do you get that would be telling you why the statement is failing? Since you are redirecting on the page, if output buffering is on, you won't see any php generated errors anyway.

 

I recommend commenting out the header() redirect until you get the code on the page to work.

Link to comment
Share on other sites

 

What php errors do you get that would be telling you why the statement is failing? Since you are redirecting on the page, if output buffering is on, you won't see any php generated errors anyway.

 

I recommend commenting out the header() redirect until you get the code on the page to work.

 

I've isolated the issue to be file_put_contents(). Here is the error I got when I tried your suggestion.

 

Warning: file_put_contents(<html><body><table bgcolor='black'><td><tr><font size='5' color='yellow'>asdf</font></tr></td></table></body></table></html>) [function.file-put-contents]: failed to open stream: Invalid argument in D:\Hosting\7317202\html\menu\menuleft.php on line 10

Link to comment
Share on other sites

I'm trying to keep the site small and simple, and there is no need to store the old menus because it's always different.

 

Using a database doesn't necessarily mean storing historic data, but whatever you think is easier.......

 

Here is a tutorial on writing content to file:

 

http://www.tizag.com/phpT/filewrite.php

 

I modified my code to your suggestion using fopen() and fwrite()

<?php
$myFile = "menuright.html";
$fh = fopen($myFile, 'w') or die("can't open file");
$html_begin = "<html><body><table bgcolor='black'><td><tr><font size='5' color='yellow'>";
$html_end = "</font></tr></td></table></body></table></html>";
$menu_text = $_POST['updateright'];
$stringData = $html_begin.$menu_text.$html_end;
fwrite($fh, $stringData);
fclose($fh);
?>

 

Now I'm getting a permissions error:

 

 

Warning: fopen(menuright.html) [function.fopen]: failed to open stream: Permission denied in D:\Hosting\7317202\html\menu\menuleft.php on line 3

can't open file

 

according to my phpinfo page allow_url_fopen is set to on, so I have no idea why it wouldn't work.

Link to comment
Share on other sites

You have the arguments reversed. The filename comes first.

 

I changed that and I'm still getting the permissions error

 

 

Warning: file_put_contents(menuright.html) [function.file-put-contents]: failed to open stream: Permission denied in D:\Hosting\7317202\html\menu\menuright.php on line 9

 

Link to comment
Share on other sites

try using the full path to the file instead of just the file name. for instance:

 

$myFile = "D:\Hosting\7317202\html\menu\menuright.html";

 

Tried this, and had no effect, same error message as before

 

 

Warning: fopen(menuleft.html) [function.fopen]: failed to open stream: Permission denied in D:\Hosting\7317202\html\menu\menuleft.php on line 3

 

Warning: file_put_contents(D:\Hosting\7317202\html\menu\menuright.html) [function.file-put-contents]: failed to open stream: Permission denied in D:\Hosting\7317202\html\menu\menuright.php on line 9

can't open file

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.