Source: put.php
⬇️ Tải file Trang chính
<?php
require __DIR__.'/s3helper.php';
$cfg=s3_load_config();
$msg=null;

if($_SERVER['REQUEST_METHOD']==='POST'){
  $mode = $_POST['mode'] ?? 'file';
  if($mode==='folder'){ // tạo "thư mục" rỗng (object key kết thúc '/')
    $prefix = s3_norm_prefix($_POST['prefix'] ?? '');
    if($prefix===''){ $msg=['ok'=>0,'text'=>'Vui lòng nhập tên thư mục']; }
    else{
      $r=s3_send_request($cfg,'PUT',$cfg['bucket'],$prefix,[],['Content-Type'=>'application/x-directory'],'');
      $msg=$r['ok']?['ok'=>1,'text'=>"📁 Đã tạo thư mục: $prefix"]:['ok'=>0,'text'=>"❌ Lỗi tạo thư mục (HTTP {$r['http_code']})"];
    }
  } else { // upload file
    if(!isset($_FILES['file']) || !is_uploaded_file($_FILES['file']['tmp_name'])){
      $msg=['ok'=>0,'text'=>'Vui lòng chọn file']; 
    } else {
      $f=$_FILES['file'];
      $prefix=s3_norm_prefix($_POST['prefix'] ?? '');
      $keyInput=trim((string)($_POST['key'] ?? ''));
      $key = $keyInput !== '' ? ltrim($keyInput,'/') : s3_join($prefix, $f['name']);
      $ctype=$f['type']?:'application/octet-stream';
      $fh=fopen($f['tmp_name'],'rb');
      $r=s3_send_request($cfg,'PUT',$cfg['bucket'],$key,[],['Content-Type'=>$ctype],$fh);
      fclose($fh);
      $msg=$r['ok']?['ok'=>1,'text'=>"✅ Upload thành công: $key"]:['ok'=>0,'text'=>"❌ Upload lỗi (HTTP {$r['http_code']})\n".substr($r['body'],0,200)];
    }
  }
}
?>
<!doctype html><html><head><meta charset="utf-8"><title>Upload</title>
<style>
body{font-family:Inter,Arial;background:#fafafa;padding:20px}
label{display:block;margin-top:8px;font-weight:600} input,button{padding:8px;margin-top:4px}
.flash{padding:10px;border-radius:8px;margin-top:10px}.ok{background:#dcfce7;color:#166534}.err{background:#fee2e2;color:#991b1b}
.card{background:#fff;border:1px solid #e5e7eb;border-radius:12px;padding:16px;max-width:720px}
</style></head><body>
<div class="card">
<h2>📤 Upload vào: <?=htmlspecialchars($cfg['bucket'])?></h2>
<?php if($msg):?><div class="flash <?=$msg['ok']?'ok':'err'?>"><?=nl2br(htmlspecialchars($msg['text']))?></div><?php endif;?>

<form method="post" enctype="multipart/form-data">
  <input type="hidden" name="mode" value="file">
  <label>Thư mục (prefix) — ví dụ: <code>images/2025/</code></label>
  <input name="prefix" placeholder="prefix (tùy chọn)">
  <label>File</label>
  <input type="file" name="file" required>
  <label>Ghi đè tên đầy đủ (key) — tùy chọn</label>
  <input name="key" placeholder="vd: images/2025/photo.jpg">
  <button>Upload</button>
</form>

<hr style="margin:18px 0">

<form method="post">
  <input type="hidden" name="mode" value="folder">
  <label>Tạo thư mục (prefix kết thúc bằng '/')</label>
  <input name="prefix" placeholder="vd: docs/">
  <button>➕ Tạo thư mục</button>
</form>

<p style="margin-top:12px"><a href="index.php">← Trang chính</a> • <a href="view.php" target="_blank">Xem danh sách</a> • <a href="view_source.php?file=put.php" target="_blank">Source</a></p>
</div>
</body></html>