Untitled

Anonymous
php
10/19/2026 11:55 AM
1.9 KB
3
Indexable
<?php
// Thư mục lưu trữ file tải lên (nằm cùng cấp với file php)
$target_dir = "uploads/";

// Nếu thư mục "uploads" chưa tồn tại thì tự động tạo mới
if (!file_exists($target_dir)) {
    mkdir($target_dir, 0777, true);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Kiểm tra xem có file nào được gửi lên không
    if (isset($_FILES['file-upload'])) {
        $file = $_FILES['file-upload'];
        
        // Đường dẫn đầy đủ để lưu file
        $target_file = $target_dir . basename($file["name"]);

        // Tiến hành di chuyển file từ thư mục tạm của hệ thống về thư mục lưu trữ
        if (move_uploaded_uploaded_file($file["tmp_name"], $target_file)) {
            echo "<script>
                    alert('Thành công: Tệp tin " . basename($file["name"]) . " đã được tải lên tại thư mục " . $target_dir . "');
                    window.location.href = 'upload.html';
                  </script>";
        } else {
            echo "<script>
                    alert('Lỗi: Không thể di chuyển tệp tin vào thư mục lưu trữ. Hãy kiểm tra lại quyền (permission) của thư mục.');
                    window.location.href = 'upload.html';
                  </script>";
        }
    } else {
        echo "Không tìm thấy dữ liệu tệp tin.";
    }
}
?>
Editor is loading...
Leave a Comment