Jump to content

Create Folder, Create File, Add To File, Close File


MattCoulter

Recommended Posts

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.
Link to comment
Share on other sites

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.

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.