Jump to content

Else if question with files and directories


coolbeansdude51

Recommended Posts

Hello forum!  Hope everyone is doing well.  I have a question for your genius!

 

All I want to do is make sure that:

 

1.  That in the user_data folder there is a folder named userid.

2.  If that's not there then it needs to be created.

3.  It then needs to check for another folder inside of that folder.  The month and year.

4.  If that's not there then it needs to be created.

5.  Once all of those are check then it needs to create a file inside of that folder.

 

Here is my logic.  I know that the code below doesn't work but .... anyone have any suggestions?

 

$timestamp = time();
$month = date('m_Y', $timestamp);

$userid = '71625181';

if(file_exists('user_data/'.$userid.'/'.$month)) {
echo 'level 1';
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
} else {
echo 'level 2';
mkdir('user_data/'.$userid.'/'.$month);
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
} else {
echo 'level 3';
mkdir('user_data/'.$userid);
mkdir('user_data/'.$userid.'/'.$month);
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
}

 

or I was thinking this

 

if(file_exists('user_data/')){
echo 'level 1';
mkdir('user_data/'.$userid);
mkdir('user_data/'.$userid.'/'.$month);
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
} elseif(file_exists('user_data/'.$userid)) {
echo 'level 2';
mkdir('user_data/'.$userid.'/'.$month);
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
} else { 
echo 'level 3';
$file = 'user_data/'.$userid.'/'.$month.'/'.$timestamp.".xml";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $xml);
fclose($fh);
};

 

Thanks for your input!

 

-Adam

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.