function api_all_upload($app, $media_id = '', $suffix = '') { $user = $app->auth->getUser(); $baseDir = dirname(__DIR__); $fd = $user['user_id']; $is_photo = false; if ($media_id == 'photo') { $fd = 'photos'; $is_photo = true; } $uploadsDir = dirname($baseDir) . '/uploads/' . $fd . '/'; $chunksDir = dirname($baseDir) . '/.chunks/' . $fd . '/'; require $baseDir . '/Flow/Autoloader.php'; Flow\Autoloader::register($baseDir); $config = new Flow\Config(); $config->setTempDir($chunksDir); $file = new Flow\File($config); if ($_SERVER['REQUEST_METHOD'] === 'GET') { if (!is_dir($uploadsDir)) { @mkdir(dirname($uploadsDir)); @mkdir($uploadsDir); } if (!is_dir($chunksDir)) { @mkdir(dirname($chunksDir)); @mkdir($chunksDir); } if ($file->checkChunk()) { header($_SERVER["SERVER_PROTOCOL"] . " 200 Ok"); } else { header($_SERVER["SERVER_PROTOCOL"] . " 204 No Content"); exit; } } else { if ($file->validateChunk()) { $file->saveChunk(); } else { // error, invalid chunk upload request, retry header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request"); exit; } } $media_file = strtolower($file->getFileName()); $profile = $media_id == 'photo'; if ($media_id) { if ($suffix) { $suffix = '.' . $suffix; } if ($profile) { $media_file = $user['user_id'] . '.' . pathinfo($media_file)['extension']; } else { $media_file = md5($media_id) . $suffix . '.' . pathinfo($media_file)['extension']; } } if ($is_photo) { //update user $u = $app->orm->user()->where('user_id', $user['user_id'])->fetch(); if ($u) { $u->update(array('date_modified' => new NotORM_Literal("NOW()"))); } } else { if ($media_id) { $u = $app->orm->media()->where('media_id', $media_id)->fetch(); if ($u) { $u->update(array('date_modified' => new NotORM_Literal("NOW()"))); } } } $media_file = preg_replace('/\\.php$/', '.php._', $media_file); if ($file->validateFile() && $file->save($uploadsDir . $media_file)) { if ($profile) { resize_image($uploadsDir . $media_file, $uploadsDir . $user['user_id'] . '.jpg', 120); if ($media_file != $user['user_id'] . '.jpg') { @unlink($uploadsDir . $media_file); $media_file = $user['user_id'] . '.jpg'; } } else { if ($suffix) { resize_image($uploadsDir . $media_file); create_thumbnail($uploadsDir . $media_file); } } echo $media_file; $files = glob($chunksDir . '*'); foreach ($files as $file) { if (is_file($file)) { @unlink($file); } } } else { echo "ไฟล์ยังอัพโหลดไม่สมบูรณ์"; } }
function all_upload($id = '0', $doc = '') { global $api; global $norm; $api->api_check_user(); $usr = $api->api_get_user(); $rs = $norm->cases()->where('id', $id)->where('user_id', $usr['id']); $ok = count($rs) > 0 && ($doc == 'file1' || $doc == 'file2'); if (!$ok) { // error, invalid chunk upload request, retry header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request"); exit; } $baseDir = dirname(__DIR__); $uploadsDir = dirname($baseDir) . '/documents/' . $usr['id'] . '/'; $chunksDir = dirname($baseDir) . '/.chunks/' . $usr['id'] . '/'; require $baseDir . '/Flow/Autoloader.php'; Flow\Autoloader::register($baseDir); $config = new Flow\Config(); $config->setTempDir($chunksDir); $file = new Flow\File($config); if ($_SERVER['REQUEST_METHOD'] === 'GET') { if (!is_dir($uploadsDir)) { @mkdir(dirname($uploadsDir)); @mkdir($uploadsDir); } if (!is_dir($chunksDir)) { @mkdir(dirname($chunksDir)); @mkdir($chunksDir); } if ($file->checkChunk()) { header($_SERVER["SERVER_PROTOCOL"] . " 200 Ok"); } else { header($_SERVER["SERVER_PROTOCOL"] . " 204 No Content"); exit; } } else { if ($file->validateChunk()) { $file->saveChunk(); } else { // error, invalid chunk upload request, retry header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request"); exit; } } $row = $rs->fetch(); $filename = strtolower($file->getFileName()); $filename = preg_replace('/[^a-zA-Z0-9_\\.]/', '_', $filename); if ($doc) { $filename = strtolower($doc . '-' . $filename); } $filename = $usr['id'] . '-' . $row['id'] . '-' . $filename; $filename = str_replace('-', '_', $filename); if ($file->validateFile() && $file->save($uploadsDir . $filename)) { $it = array(); $it[$doc] = $filename; $row->update($it); echo "File upload was completed "; echo $filename; } else { echo "This is not a final chunk, continue to upload"; } exit; }