|
<?php |
|
|
|
$imageDir = 'img/'; |
|
$videoDir = 'videos/'; |
|
|
|
|
|
if (!is_dir($imageDir)) { |
|
mkdir($imageDir, 0777, true); |
|
} |
|
if (!is_dir($videoDir)) { |
|
mkdir($videoDir, 0777, true); |
|
} |
|
|
|
|
|
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://'; |
|
|
|
|
|
if (strpos($_SERVER['HTTP_HOST'], '.hf.space') !== false) { |
|
$protocol = 'https://'; |
|
} |
|
|
|
|
|
$host = $_SERVER['HTTP_HOST']; |
|
$baseUrl = $protocol . $host . dirname($_SERVER['SCRIPT_NAME']) . '/'; |
|
|
|
|
|
$response = ["code" => 400, "msg" => "failed"]; |
|
|
|
|
|
if ($_FILES) { |
|
|
|
$file = $_FILES['file']; |
|
$filename = basename($file['name']); |
|
$fileTmpPath = $file['tmp_name']; |
|
$fileSize = $file['size']; |
|
$fileType = $file['type']; |
|
|
|
|
|
if (strpos($fileType, 'image') !== false) { |
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
$newFilename = 'Image_' . time() . sprintf('%03d', rand(0, 999)) . '.' . $ext; |
|
$uploadFilePath = $imageDir . $newFilename; |
|
$urlPath = $baseUrl . $imageDir . $newFilename; |
|
} elseif (strpos($fileType, 'video') !== false) { |
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
$newFilename = 'Video_' . time() . sprintf('%03d', rand(0, 999)) . '.' . $ext; |
|
$uploadFilePath = $videoDir . $newFilename; |
|
$urlPath = $baseUrl . $videoDir . $newFilename; |
|
} else { |
|
|
|
$response['msg'] = 'Invalid file type'; |
|
echo json_encode($response); |
|
exit; |
|
} |
|
|
|
|
|
if (move_uploaded_file($fileTmpPath, $uploadFilePath)) { |
|
$response['code'] = 200; |
|
$response['msg'] = 'success'; |
|
if (strpos($fileType, 'image') !== false) { |
|
$response['img'] = $urlPath; |
|
} elseif (strpos($fileType, 'video') !== false) { |
|
$response['videos'] = $urlPath; |
|
} |
|
} else { |
|
$response['msg'] = 'File upload failed'; |
|
} |
|
} else { |
|
$response['msg'] = 'No file uploaded'; |
|
} |
|
|
|
|
|
echo str_replace('\/', '/', json_encode($response)); |
|
?> |
|
|