Breaking News
Home / Web / PHP / PHP Snippets / How to check if file is writable in PHP?

How to check if file is writable in PHP?

  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.                 return is_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.                 return FALSE;
  17.         }
  18.  
  19.            fclose($fp);
  20.            @chmod($file, DIR_WRITE_MODE);
  21.            @unlink($file);
  22.            return TRUE;
  23.         }
  24.         elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
  25.         {
  26.                 return FALSE;
  27.         }
  28.  
  29.         fclose($fp);
  30.         return TRUE;
[pb_builder]

Check Also

How to list files in a directory in PHP?

<?php if ($handle = opendir(‘/path/to/directory’)) {     echo "Directory handle: $handlen";     echo …

Leave a Reply