1--TEST-- 2Test unlink() function : usage variations - unlinking file in a directory 3--SKIPIF-- 4<?php 5if (substr(PHP_OS, 0, 3) == 'WIN') { 6 die('skip only on Linux'); 7} 8// Skip if being run by root (files are always readable, writeable and executable) 9$filename = dirname(__FILE__)."/unlink_root_check.tmp"; 10$fp = fopen($filename, 'w'); 11fclose($fp); 12if(fileowner($filename) == 0) { 13 unlink ($filename); 14 die('skip cannot be run as root'); 15} 16 17unlink($filename); 18?> 19--FILE-- 20<?php 21/* Prototype : bool unlink ( string $filename [, resource $context] ); 22 Description : Deletes filename 23*/ 24 25/* Delete file having default permission but its dir having readonly permission 26 Delete file having readonly permission but dir having default permission 27*/ 28 29 30$file_path = dirname(__FILE__); 31 32// temp dir name used here 33$dirname = "$file_path/unlink_variation1"; 34// temp filename used here 35$filename = "$dirname/unlink_variation1.tmp"; 36 37echo "\n*** Testing unlink() on file inside a directory ***\n"; 38// create temp dir 39mkdir($dirname); 40// create temp file inside $dirname 41$fp = fopen($filename, "w"); 42fclose($fp); 43 44echo "-- Unlink file having default permission and its dir having read only permission --\n"; 45// remove write permission of $dirname 46var_dump( chmod($dirname, 0444) ); 47// now try deleting $filename 48var_dump( unlink($filename) ); // expected false 49var_dump( file_exists($filename) ); // confirm file is deleted 50 51// remove the dir 52var_dump( chmod($dirname, 0777) ); 53var_dump( unlink($filename) ); // expected true 54var_dump( rmdir($dirname) ); 55 56echo "\n-- Unlinking file without write permission, its dir having default permission --\n"; 57// create the temp dir 58var_dump( mkdir($dirname) ); 59// create the temp file 60$fp = fopen($filename, "w"); 61fclose($fp); 62 63// remove write permission from file 64var_dump( chmod($filename, 0444) ); 65// now try deleting temp file inside $dirname 66var_dump( unlink($filename) ); // expected: true 67var_dump( file_exists($filename) ); // expected: false 68 69// remove temp dir 70var_dump( rmdir($dirname) ); 71 72echo "Done\n"; 73?> 74--EXPECTF-- 75*** Testing unlink() on file inside a directory *** 76-- Unlink file having default permission and its dir having read only permission -- 77bool(true) 78 79Warning: unlink(%s): %s in %s on line %d 80bool(false) 81bool(false) 82bool(true) 83bool(true) 84bool(true) 85 86-- Unlinking file without write permission, its dir having default permission -- 87bool(true) 88bool(true) 89bool(true) 90bool(false) 91bool(true) 92Done