has($hash)) { Storage::disk('textures')->move($path, $hash); } else { // delete the temp file unlink($absolute_path); } return $hash; } else { Log::warning("Failed to upload file $path"); } } } public static function download($url, $path) { @set_time_limit(0); touch($path); Log::info("[File Downloader] Download started, source: $url"); Log::info("[File Downloader] ======================================"); if ($fp = fopen($url, "rb")) { if (!$download_fp = fopen($path, "wb")) { return false; } while (!feof($fp)) { if (!file_exists($path)) { // cancel downloading if destination is no longer available fclose($download_fp); return false; } Log::info("[Download] 1024 bytes wrote"); fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8); } fclose($download_fp); fclose($fp); Log::info("[File Downloader] Finished downloading, data stored to: $path"); Log::info("[File Downloader] ==========================================="); return true; } else { return false; } } public static function getRemoteFileSize($url) { $regex = '/^Content-Length: *+\K\d++$/im'; if (!$fp = @fopen($url, 'rb')) { return false; } if ( isset($http_response_header) && preg_match($regex, implode("\n", $http_response_header), $matches) ) { return (int)$matches[0]; } return strlen(stream_get_contents($fp)); } /** * Generate random string * * @param int $length * @param bool $special_chars * @return string */ public static function generateRndString($length, $special_chars = true) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; if ($special_chars) $chars .= "!@#$%^&*()-_ []{}<>~`+=,.;:/?|"; $rnd_string = ''; for ($i = 0; $i < $length; $i++) { $rnd_string .= $chars[mt_rand(0, strlen($chars) - 1)]; } return $rnd_string; } public static function getTimeFormatted($timestamp = 0) { return ($timestamp == 0) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $timestamp); } /** * Replace content of string according to given rules * * @param string $str * @param array $rules * @return string */ public static function getStringReplaced($str, $rules) { foreach ($rules as $search => $replace) { $str = str_replace($search, $replace, $str); } return $str; } public static function convertUploadFileError($errno = 0) { $phpFileUploadErrors = [ 0 => 'There is no error, the file uploaded with success', 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 3 => 'The uploaded file was only partially uploaded', 4 => 'No file was uploaded', 6 => 'Missing a temporary folder', 7 => 'Failed to write file to disk.', 8 => 'A PHP extension stopped the file upload.', ]; return $phpFileUploadErrors[$errno]; } }