PHP Warning: fwrite(): supplied argument is not a valid stream resource in /home/bezdredg/public_html/php/output.php on line 6 My files are CHMODded to 777, and the file is created just fine. This is my first PHP script, so I have tried to troubleshoot, but to no avial. The basic structure of my script, is you input data into a form and when you submit it, it creates a new .php file and writes that data to it. All the form stuff is working, I just need help with this one error. My code is <?php $result = $_POST['code']; $user = $_POST['username']; $filename = "".$user.".php "; $filehandle = fopen($filename, 'w') or die("File Load Failed"); fwrite ($filename, $result); ?> PHP: Thanks, BP
Awesome thanks. And how would I make it redirect to the created file? I tried: Header ("Location: "$filehandle""); as well as $filename, but I know that is just not right (especially since it doesnt work) Anyway, green rep is given
Thanks! You will have to figure out the exact address of that file, maybe something like this: $the_url = 'http://www.yoursite.com/' .$filename; header('Location: ' .$the_url);
I am using <?php /* Redirect to a different page in the current directory that was requested */ $user = $_POST['username']; $filename = "".$user.".php "; $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $extra = $filename; $result = $_POST['code']; $filehandle = fopen($filename, 'w') or die("We have failed to open your file"); fwrite ($filehandle, $result); chmod("$filename", 0777); header("Location: http://$host$uri/$extra") ?> Code (markup): The only problem is it says "Can not find filename.php" on server...error 404. And I have triple check, it is there, and the chmod worked fine. However, in Cpanel, they only show up as Text/x-generic. So what is the problem here? Thanks, BP
You have too many quotes in your header statement, and $filehandle won't work. header('Location: ' . $filename); Code (markup): Will work. BUT... If the code posted doesn't contain the opening and closing php tags <?php and ?> it won't run. Also, if there are syntax errors in the posted code, it won't run. Lastly, you realize that by allowing people to post arbitrary php code which you then execute, you are opening up your server to hackers. I hope you aren't doing this on a live page anywhere
I am aware of the dangers involved, I just wanna get everything working before I secure it. And yes hanidorf, it is the correct URL. I even type it manually, and it still doesn't show up. Is there a possibility it is a hidden file or something? BP
What you are trying to do can be done with a database, that is you save what ever user enters (filter it first and escape it) in the database and a file that shows it to users based on the ID (or username here). Like here on DP if you look at the URL it's showthread.php?t=478277 and this forum software doesn't make an actual file for each thread. If it did, then this forum would end up with 500,000+ files and the owner couldn't change the theme or it wouldn't be searchable easily. This is the proper way to make an application because you can end up with 1,000s of files and you won't be able to change anything in them effectively. Also if you want the name of the user and the .php in the URL, that can be done with Apache's mod_rewrite. I hope this helps.
Well, this is gonna sound odd. I want to make a new file everytime I submit the form. Each file will have different styles and whatever. This sounds very odd I know. BP