function tryGetDefaultByType($type) { $ext; switch ($type) { case "document": $ext = ".docx"; break; case "spreadsheet": $ext = ".xlsx"; break; case "presentation": $ext = ".pptx"; break; default: return; } $demoName = "demo" . $ext; $demoFilename = GetCorrectName($demoName); if (!@copy(dirname(__FILE__) . DIRECTORY_SEPARATOR . "app_data" . DIRECTORY_SEPARATOR . $demoName, getStoragePath($demoFilename))) { sendlog("Copy file error to " . getStoragePath($demoFilename), "logs/common.log"); //Copy error!!! } return $demoFilename; }
function save() { $contentType = "text/plain"; $downloadUri = $_GET["fileuri"]; $fileName = $_GET["filename"]; if (empty($downloadUri) || empty($fileName)) { $result["error"] = 'Error request'; return $result; } $newType = trim(pathinfo($downloadUri, PATHINFO_EXTENSION), '.'); $currentType = trim(!empty($_GET["filetype"]) ? $_GET["filetype"] : pathinfo($downloadUri, PATHINFO_EXTENSION), '.'); if (strtolower($newType) != strtolower($currentType)) { $key = GenerateRevisionId($downloadUri); $newFileUri; try { $percent = GetConvertedUri($downloadUri, $newType, $currentType, $key, FALSE, $newFileUri); if ($percent != 100) { $result["error"] = "error: Can't convert file"; return $result; } } catch (Exception $e) { $result["error"] = "error: " . $e->getMessage(); return $result; } $downloadUri = $newFileUri; $newType = $currentType; } $path_parts = pathinfo($fileName); $ext = $path_parts['extension']; $name = $path_parts['basename']; $baseNameWithoutExt = substr($name, 0, strlen($name) - strlen($ext) - 1); $fileName = $baseNameWithoutExt . "." . $newType; if (($data = file_get_contents(str_replace(" ", "%20", $downloadUri))) === FALSE) { $result["error"] = 'Bad Request'; return $result; } else { file_put_contents(getStoragePath($fileName), $data, LOCK_EX); } $result["success"] = 'success'; return $result; }
function DoUpload($fileUri) { $_fileName = GetCorrectName($fileUri); $ext = strtolower('.' . pathinfo($_fileName, PATHINFO_EXTENSION)); if (!in_array($ext, getFileExts())) { throw new Exception("File type is not supported"); } if (!@copy($fileUri, getStoragePath($_fileName))) { $errors = error_get_last(); $err = "Copy file error: " . $errors['type'] . "<br />\n" . $errors['message']; throw new Exception($err); } return $_fileName; }
public function test_getStoragePath() { $result = getStoragePath(); $expected = APPROOT . 'userdata/'; $this->assertEquals($expected, $result); }
function GetCorrectName($fileName) { $path_parts = pathinfo($fileName); $ext = $path_parts['extension']; $name = $path_parts['basename']; $baseNameWithoutExt = substr($name, 0, strlen($name) - strlen($ext) - 1); for ($i = 1; file_exists(getStoragePath($name)); $i++) { $name = $baseNameWithoutExt . " (" . $i . ")." . $ext; } return $name; }