文件 - 上传,下载和删除PHP

时间:2016-04-06 08:30:45

标签: 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)我还希望管理员用户能够删除不需要的文件。我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试使用readfile()功能。 download files using php

相关问题