upload($data)支援兩種操作模式:upload(上傳)與 delete(刪除暫存檔)。使用 Session 記錄暫存檔路徑,確保同一使用者同一時間只有一張暫存圖。
function upload($data) {
// 參數檢查:file, img_tag_id, img_dir, max_file_size, type_array, sub_type
// 驗證檔案類型、大小、目錄是否存在
// 產生唯一檔名:時間 + 隨機數 + 原始檔名
// move_uploaded_file 移動至目標目錄
// 寫入 $_SESSION['name'] 與 $_SESSION['imgpath']
// 回傳圖片路徑供前端預覽
}
<form action="ad.php" method="post" enctype="multipart/form-data" target="frame">
<input type="file" name="file" class="input">
<input type="submit" name="submit_upload" value="上傳">
<input type="submit" name="submit_delete" value="刪除">
<iframe id="frame" name="frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>
</form>
<img id="picview" height="100" style="display:none;">
<script>
if("<?php echo $imgpath; ?>") {
parent.document.getElementById("picview").src = "<?php echo $imgpath; ?>";
parent.document.getElementById("picview").style.display = "block";
}
</script>
<script> 通知父視窗更新 <img> src 並顯示。