溫馨提示×

debian php中如何實現文件上傳下載

PHP
小樊
97
2024-08-14 12:46:39
欄目: 編程語言

文件上傳:

  1. 在HTML表單中添加一個文件上傳的input標簽:
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>
  1. 創建一個PHP處理文件上傳的腳本upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
?>

文件下載:

  1. 創建一個PHP腳本download.php來處理文件下載請求:
<?php
$file = 'path/to/file.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
  1. 在需要提供文件下載的頁面中創建一個鏈接指向download.php,傳遞文件名參數:
<a href="download.php?file=file.txt">Download file</a>

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女