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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.