Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to check if file is writable in PHP?

$
0
0
  1. function is_really_writable($file){   
  2. // If we're on a Unix server with safe_mode off we call is_writable
  3.         if(DIRECTORY_SEPARATOR=='/' AND @ini_get("safe_mode")==FALSE)
  4.         {
  5.                 returnis_writable($file);
  6.         }
  7.  
  8. // For windows servers and safe_mode "on" installations we'll actually
  9.         // write a file then read it.  Bah...
  10.         if(is_dir($file))
  11.         {
  12.                 $file=rtrim($file,'/').'/'.md5(rand(1,100));
  13.  
  14.         if(($fp=@fopen($file, FOPEN_WRITE_CREATE))===FALSE)
  15.         {
  16.                 returnFALSE;
  17.         }
  18.  
  19.            fclose($fp);
  20.            @chmod($file, DIR_WRITE_MODE);
  21.            @unlink($file);
  22.            returnTRUE;
  23.         }
  24.         elseif(($fp=@fopen($file, FOPEN_WRITE_CREATE))===FALSE)
  25.         {
  26.                 returnFALSE;
  27.         }
  28.  
  29.         fclose($fp);
  30.         returnTRUE;

Viewing all articles
Browse latest Browse all 178

Trending Articles