MattCoulter Posted October 4, 2013 Share Posted October 4, 2013 This is what I have: mkdir($username); $file=fopen($username."/index.htm","w"); fwrite($file,"<html>"."</html>"); fclose($file); where $username refers to what the user entered for their username. The .php I'm using this code in is merely for a signup process. I'm extremely new to PHP (like, today), so please go easy on me. Quote Link to comment https://forums.phpfreaks.com/topic/282723-create-folder-create-file-add-to-file-close-file/ Share on other sites More sharing options...
DavidAM Posted October 4, 2013 Share Posted October 4, 2013 1) You have not stated a question or problem. 2) Please use tags when posting code: mkdir($username); $file=fopen($username."/index.htm","w"); fwrite($file,"<html>"."</html>"); fclose($file);3) Be careful using user input for filesystem access. There are certain characters (/, \, :, <, >, $, |, <space>, etc.) that will either cause the mkdir command to fail or create problems accessing the files later (especially from the command line). If I was going to do something like this, I would use the user's ID from the database (an Integer) perhaps prepended with zeros -- so UserID # 4 might be "000000000004" for a folder name. This would allow the username to change without the complication of renaming the folder. 4) You should really test each step to be sure an error has not occurred: // Error reporting during development error_reporting(E_ALL); ini_set('display.errors', 1); if (! mkdir($username)) trigger_error('mkdir Failed', E_USER_ERROR); $file=fopen($username."/index.htm","w"); if (! $file) trigger_error('fopen Failed', E_USER_ERROR); if (! fwrite($file,"<html>"."</html>")) trigger_error('fwrite Failed', E_USER_ERROR); fclose($file); Of course, that's not any better than just using or die('xxx Failed');. You need to do something logical at any particular failure point. I would stick the whole thing in a function and handle it: error_reporting(E_ALL); ini_set('display.errors', 1); function makeUserIndex($username) { if (! mkdir($username)) { trigger_error('mkdir Failed', E_USER_WARNING); return false; } $file=fopen($username."/index.htm","w"); if (! $file) { trigger_error('fopen Failed', E_USER_WARNING); return false; } if (! fwrite($file,"<html>"."</html>")) { trigger_error('fwrite Failed', E_USER_WARNING); fclose($file); return false; } fclose($file); return true; } $user = 'DavidAM'; if (! makeUserIndex($user)) { // Something here to handle the failed attempt } (not tested) Ideally, the filename (index.htm) and the content would be passed to the function as well. Quote Link to comment https://forums.phpfreaks.com/topic/282723-create-folder-create-file-add-to-file-close-file/#findComment-1452627 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.