Jump to content

Shorthand If

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Would something like this be do-able with shorthand if statements, or am I wasting my time?


$original = time();

$dir = getcwd();

$dir = substr($dir,0,-7);

$dir .= $dir . '/stuff/' . $_SESSION['mem_id'];


$init = (is_dir($dir) ? true : (mkdir($dir,0777) ? true:false));

if($init)

{

	$dir .= '/' . $original;

	$created = (is_dir($dir) ? true : (mkdir($dir,0777) ? true:false));

}

else

{

	$created = false;

}


Rather than :

if(!is_dir($dir))

{

	if(mkdir($dir,0777))

	{

		$dir .= '/' . $original;

		if(!is_dir($dir))

		{

			if(mkdir($dir,0777))

			{

				$created = true;

			}

			else

			{

				$created = false;

			}

		}

		else

		{

			$created = true;

		}

	}

	else

	{

		$created = false;

	}

}

else

{

	$created = true;

}


Bassically just check if directory exists, otherwise create it. Doin it wrong?

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You can do that but it would be prone to errors in logic, and should be refractored for clarity. Personally I would take this approach for security (should work):
$original = time();
$dir = substr(getcwd(),0,-7) . '/' . $_SESSION['mem_id'];
$subdir = $dir . '/' . $orignal;

if(is_writable(dirname($dir))) {
    if(is_dir($dir) !== true) {
        mkdir($dir, 0777); //guarenteed to be writable
    }
    if(is_dir($subdir) !== true) {
        mkdir($subdir, 0777);
    }
} else {
    //will not be able to create
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Ah, cool. Thanks for replying :) I'll be leeching your code if you don't mind ^^

Rep+

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it