Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created January 14, 2014 11:42
Show Gist options
  • Select an option

  • Save itsjavi/8417008 to your computer and use it in GitHub Desktop.

Select an option

Save itsjavi/8417008 to your computer and use it in GitHub Desktop.
PHP filesystem permission test
<?php
error_reporting(-1);
ini_set('display_errors', true);
echo '<h1>PHP permission test</h1>';
$currdir = realpath(dirname(__FILE__));
$testdir = $currdir . '/' . time();
$testfile = $testdir . '/mytest.txt';
$testdir_passed = true;
$testfile_passed = true;
echo '<h2>Dir test:</h2>';
echo '<pre>' . $testdir . '</pre>';
mkdir($testdir, 0755, true);
if (is_readable($testdir)) {
echo '<p style="color:green">is_readable OK</p>';
} else {
$testdir_passed = false;
echo '<p style="color:red">is_readable FAIL</p>';
}
if (is_dir($testdir)) {
echo '<p style="color:green">is_dir OK</p>';
} else {
$testdir_passed = false;
echo '<p style="color:red">is_dir FAIL</p>';
}
if (is_writable($testdir)) {
echo '<p style="color:green">is_writable OK</p>';
} else {
$testdir_passed = false;
echo '<p style="color:red">is_writable FAIL</p>';
}
echo '<h2>File test:</h2>';
echo '<pre>' . $testfile . '</pre>';
if ($testdir_passed) {
file_put_contents($testfile, 'this is a test');
if (is_readable($testfile)) {
echo '<p style="color:green">is_readable OK</p>';
} else {
$testfile_passed = false;
echo '<p style="color:red">is_readable FAIL</p>';
}
if (is_file($testfile)) {
echo '<p style="color:green">is_dir OK</p>';
} else {
$testfile_passed = false;
echo '<p style="color:red">is_dir FAIL</p>';
}
if (is_writable($testfile)) {
echo '<p style="color:green">is_writable OK</p>';
} else {
$testfile_passed = false;
echo '<p style="color:red">is_writable FAIL</p>';
}
} else {
$testfile_passed = false;
echo '<p style="color:red">DIR test failed, cannot continue</p>';
}
try {
// Clean tests
if ($testfile_passed) {
unlink($testfile);
if ($testdir_passed) {
rmdir($testdir);
}
}
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment