protected function createFileLog(&$filename) { $dirname = dirname($filename); $basename = Amslib_String::slugify2(basename($filename), "-", "."); $filename = "{$dirname}/{$basename}"; if (file_exists($filename)) { return true; } if (!is_dir(dirname($filename)) && !@mkdir(dirname($filename), 0777, true)) { Amslib_Debug::log(error_get_last(), $dirname, $basename, $filename); return false; } $t = $c = 0; if ($t = @touch($filename) && ($c = @chmod($filename, 0777))) { return true; } Amslib_Debug::log("file failed to create, or modify it's permissions", error_get_last(), "touch=" . intval($t), "chmod=" . intval($c)); return false; }
public static function moveFile($src_filename, $directory, &$dst_filename, &$fullpath = NULL) { // You need a valid src filename and directory, otherwise you can't really move files around if (!strlen($src_filename) || !strlen($directory)) { return false; } $error = false; $directory = self::absolute($directory); // grab warning/error output to stop it breaking the output ob_start(); // NOTE: Perhaps all this checking and copying or directories etc, should be formalised into the api?? // If the destination directory doesnt exist, attempt to create it if ($error == false && !is_dir($directory) && !mkdir($directory, 0777, true)) { Amslib_Debug::log("There was an error with the directory, either permissions, or creating it was not possible", $directory, error_get_last()); $error = true; } // It REALLY REALLY should exist now, but lets check just in case if ($error == false && !is_dir($directory)) { Amslib_Debug::log("The directory does not exist, so cannot write to the location", $directory, error_get_last()); $error = true; } // If there was not dst_filename given, use the original filename from the src if (!strlen($dst_filename)) { $dst_filename = basename($src_filename); } $dst_filename = Amslib_String::slugify2(basename($dst_filename), "_", "."); $destination = self::reduceSlashes("{$directory}/{$dst_filename}"); // Try to move the file into the correct destination if ($error == false && !rename($src_filename, $destination)) { Amslib_Debug::log("It was not possible to save to the requested filename", error_get_last()); $error = true; } // If there are no errors, you have uploaded the file ok, however, you could still fail here if ($error == false && !chmod($destination, 0777)) { Amslib_Debug::log("file uploaded ok (apparently), but chmod failed", $destination, error_get_last()); } // if the output was not empty, something bad happened, like a warning or error // when this happens, sometimes with file operations, it can break json, or website output // so I use an output buffer to grab all the output without it going to the user in some way // then once I have it, I can do something with it, like output it only to the error log, etc, etc. $output = ob_get_clean(); if (strlen($output)) { Amslib_Debug::log("Error or warning executing file operations", $output); } // Set the full path of the file, it's final destination in the filesystem $fullpath = $destination; return !$error; }