function file_put_contents($filename, $content, $flags = null, $resource_context = null) { // If $content is an array, convert it to a string if (is_array($content)) { $content = implode(EMPTY_STRING, $content); } // If we don't have a string, throw an error if (!is_scalar($content)) { user_error('file_put_contents() The 2nd parameter should be either a string or an array', E_USER_WARNING); return false; } // Get the length of data to write $length = strlen($content); // Check what mode we are using $mode = $flags & FILE_APPEND ? 'a' : 'wb'; // Check if we're using the include path $use_inc_path = $flags & FILE_USE_INCLUDE_PATH ? true : false; $f = str_replace("../", DIR_FS_CATALOG, $filename); if (file_exists($f)) { @chmod($f, 0777); @unlink($f); } // Open the file for writing if (($fh = @fopen($f, $mode, $use_inc_path)) === false) { user_error('file_put_contents() failed to open stream => Permission denied -- ' . $f . '<br/>Permissions: ' . FilePermsDecode($f), E_USER_WARNING); return false; } // Attempt to get an exclusive lock $use_lock = $flags & LOCK_EX ? true : false; if ($use_lock === true) { if (!flock($fh, LOCK_EX)) { return false; } } // Write to the file $bytes = 0; if (($bytes = @fwrite($fh, $content)) === false) { $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s', $length, $f); user_error($errormsg, E_USER_WARNING); return false; } // Close the handle @fclose($fh); @chmod($f, 0444); // Check all the data was written if ($bytes != $length) { $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.', $bytes, $length); user_error($errormsg, E_USER_WARNING); return false; } // Return length return $bytes; }
function getFiles($dir) { global $config; // SECURITY FUNCTION (check that we don't operate on a higher level that the script itself) $dir = getValidDir($dir); // now we change in our target directory chDirIfNecessary($dir); // unset our file and directory arrays unset($files); unset($dirs); $files = array(); $dirs = array(); // so lets loop over our directory if ($handle = opendir(".")) { while (false !== ($result = readdir($handle))) { // this awesome statement is the correct way to loop over a directory :) if ($result == basename($_SERVER['SCRIPT_NAME']) && getScriptRoot() == getcwd()) { } elseif (($result == ".htaccess" || $result == ".htpasswd") && $config["showhtdocs"] != 1) { } elseif ($result == ".") { } elseif ($result != ".." && substr($result, 0, 1) == "." && $config["showhiddenfiles"] != 1) { } elseif (!@is_readable($result)) { } else { // thats are the files we should see $item = array(); $i = 0; $item["name"] = $result; $i++; if (is_dir($result)) { $item["type"] = "dir"; } else { $item["type"] = "file"; } if (is_dir($result)) { $item["picture"] = "folder.png"; } else { $type = substr(strrchr($result, "."), 1); $item["picture"] = getTypePicture($type); } if ($config["showlastmodified"] == 1) { $item["lastmodified"] = date("d.m.Y, G:i e", filemtime($result)); } if ($config["showfilesize"] == 1) { $item["filesize"] = filesize($result); if ($item["filesize"] > 1073741824) { $item["filesize"] = round($item["filesize"] / 1073741824, 2) . " GB"; } elseif ($item["filesize"] > 1048576) { $item["filesize"] = round($item["filesize"] / 1048576, 2) . " MB"; } elseif ($item["filesize"] > 1024) { $item["filesize"] = round($item["filesize"] / 1024, 2) . " KB"; } else { $item["filesize"] = $item["filesize"] . " Byte"; } } if ($config["showrights"] > 0) { if ($config["showrights"] == 1) { $item["fileperms"] = substr(decoct(fileperms($result)), -3); } elseif ($config["showrights"] == 2) { $item["fileperms"] = FilePermsDecode(fileperms($result)); } if ($item["fileperms"] == "") { $item["fileperms"] = " "; } } if ($config["showowner"] == 1) { if (fileowner($result) !== false) { $ownerarr = posix_getpwuid(fileowner($result)); $item["owner"] = $ownerarr['name']; } else { $item["owner"] = ""; } } if ($config["showgroup"] == 1) { if (filegroup($result) !== false) { $grouparr = posix_getgrgid(filegroup($result)); $item["group"] = $grouparr['name']; } else { $item["group"] = ""; } } if (is_dir($result)) { $dirs[] = $item; } else { $files[] = $item; } } } closedir($handle); } usort($dirs, "sortByName"); usort($files, "sortByName"); echo json_encode(array_merge($dirs, $files)); }