File_put_contents:
Code:<?php file_put_contents("file.html","<b>Hello</b>"); ?>
I don't know about fopen() — that should work in PHP4 too. Check you haven't made some elementary error (path name? permissions?).
file_put_contents() is only available in PHP5.
A Small Orange has lots of features, but I hear that iWeb is very good, and offers better deals on space. Both provide PHP5. I'd be disinclined to go with iWeb for the sole reason that they need to be taught that the definition of an 'all-inclusive package' is that it, well, includes everything. Perhaps I'm overly picky.![]()
Last edited by Twey; 02-19-2009 at 01:53 AM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
bluewalrus (02-19-2009)
Make sure that the folder is writable, and use the "x" or "x+" too!
Aha, guess it is.![]()
Jeremy | jfein.net
bluewalrus (02-19-2009)
I'm going to have to assume I'm making some mistake then that's usually the case. Here is my code (I just rewrote this to double check and still no luck):
x error message:PHP Code:<?php
$create = fopen("newfile1asdfghjkl.txt", "x");
chmod($create, 0777) == true;
$contents = "This is text";
fwrite($create, $contents);
fclose($create);
?>
x+ error message:Warning: chmod() [function.chmod]: No such file or directory in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 11
The <?php is line 9 and ?>is line 15 Thanks for any more ideas you can offer here. I think I'll go with the orange server.Warning: fopen(newfile1asdfghjkl.txt) [function.fopen]: failed to open stream: File exists in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 10
Warning: chmod() [function.chmod]: No such file or directory in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 11
Warning: fwrite(): supplied argument is not a valid stream resource in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 13
Warning: fclose(): supplied argument is not a valid stream resource in /hsphere/local/home/crazychr/bluewalrus.net/thisthatthe.php on line 14
Well, here's the obvious:The fopen() fails because the file exists already — that's what 'x' (like 'x+') does. If you want to write to an existing file, use one of the 'a' (append) or 'w' (overwrite) family of flags.Code:$create = fopen("newfile1asdfghjkl.txt", "x"); chmod($create, 0777) == true;
chmod() works with a filename, but you've given it a resource handle, so it's looking for a file named '5' or something (resource handles are long integers). What exactly is that == true supposed to do? It's redundant.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
bluewalrus (02-19-2009)
IT WORKED!!
wahoo
I don't know why it needs 2 = but it does. I tried it with 1 and that was the error then i put the second one and bamo I have a file created. Thanks.
Working code...
PHP Code:<?php
$name = "newfile1asdfghjkl.txt";
$create = fopen($name, "a");
chmod($name, 0777) == true;
$contents = "This is text";
fwrite($create, $contents);
fclose($create);
?>
You can remove it entirely. Just chmod($name, 0777); should suffice. All you're doing there is comparing the result to true and then discarding the result of the comparison.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
bluewalrus (02-24-2009)
I have another one...
How can I not have quotes auto escaped. This is what the code is being written as in my text files.
That all comes from youtube but the =\" dont belong should just be =" or if not getting it to not write those how can i get it to ignore them when reading it with fread? Thanks for any ideas on this as well.Code:<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/c7BS8adXyBE&hl=en&fs=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/c7BS8adXyBE&hl=en&fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>
Nothing to do with your file reading and writing. It sounds like magic_quotes_gpc. See http://www.php.net/magic_quotes.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks