簡體   English   中英

文件-上傳,下載和刪除PHP

[英]Files - Upload, Download and Delete PHP

我在自己的網站上添加了一個頁面,管理員可以在其中上傳單詞文件,普通用戶可以下載這些文件。 第一次嘗試代碼時,它下載正常,但是現在,當我再次嘗試時,任何已上傳的文件都將以zip文件的形式下載,並且不包含已上傳的文檔。 出了什么問題?

upload_file.php

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Uploading File</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>Upload File</label>
</div>
<div id="body">
 <form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="File" name="File" />
 <button type="submit" name="btn-upload">Upload</button>
 </form>
    <br /><br />
    <?php
 if(isset($_GET['success']))
 {
  ?>
        <label>File Uploaded Successfully...  <a href="view.php">click here to view file.</a></label>
        <?php
 }
 else if(isset($_GET['fail']))
 {
  ?>
        <label>Problem While File Uploading !</label>
        <?php
 }
 else
 {
  ?>
        <?php
 }
 ?>
</div>
</body>
</html>

upload.php

<?php
include_once("functions.php");
if(isset($_POST['btn-upload']))
{    

 $file = rand(1000,100000)."-".$_FILES['File']['Name'];
    $file_loc = $_FILES['File']['tmp_name'];
 $file_size = $_FILES['File']['Size'];
 $file_type = $_FILES['File']['Type'];

 // new file size in KB
 $new_size = $file_size/1024;  
 // new file size in KB

 // make file name in lower case
 $new_file_name = strtolower($file);
 // make file name in lower case

 $final_file=str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($file_loc,$final_file))
 {
  $sql="INSERT INTO Uploads(File,Type,Size) VALUES('$final_file','$file_type','$new_size')";
  mysql_query($sql);
  ?>
  <script>
  alert('successfully uploaded');
        window.location.href='view.php?success';
        </script>
  <?php
 }
 else
 {
  ?>
  <script>
  alert('error while uploading File');
        window.location.href='upload_file.php?fail';
        </script>
  <?php
 }
}
?>

view.php

<?php
include_once("functions.php");

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>
<div id="header">
<label>View Files</label>
</div>
<div id="body">
 <table width="80%" border="1">
    <tr>
    <th colspan="4">Uploads...</th>
    </tr>
    <tr>
    <td>File Name</td>
    <td>File Type</td>
    <td>File Size(KB)</td>
    <td>Download</td>
    </tr>
    <?php
 $sql="SELECT * FROM Uploads";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
 {
  ?>
        <tr>
        <td><?php echo $row['File'] ?></td>
        <td><?php echo $row['Type'] ?></td>
        <td><?php echo $row['Size'] ?></td>
        <td><a href="<?php echo $row['File'] ?>" target=" ">Download File</a></td>
        </tr>
        <?php
 }
 ?>
    </table>

</div>
</body>
</html>

2)我也希望管理員用戶能夠刪除不需要的文件。 我該怎么辦?

嘗試使用readfile()函數。 使用php下載文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM