简体   繁体   English

上载Word文档以创建文件下载(PHP)

[英]Upload word document to create a file download (PHP)

I am doing a school project where students can upload study notes to help other students. 我正在做一个学校项目,学生可以在其中上传学习笔记以帮助其他学生。 I have created the ability to upload word/pages files but I want to enable that once the file is uploaded, other users can click on that file and it will download for them. 我已经创建了可以上载word / pages文件的功能,但是我想启用该功能,一旦文件上载,其他用户可以单击该文件,然后为其下载。

Not really sure what code to put.. (This is upload.php) 不太确定要放什么代码。(这是upload.php)

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $name     = $_FILES['file']['name'];
        $tmpName  = $_FILES['file']['tmp_name'];
        $error    = $_FILES['file']['error'];
        $size     = $_FILES['file']['size'];
        $ext      = strtolower(pathinfo($name, PATHINFO_EXTENSION));

        switch ($error) {
            case UPLOAD_ERR_OK:
                $valid = true;
                //validate file extensions
                if ( !in_array($ext, array('jpg','jpeg','png','gif', 'docx', 'mp3')) ) {
                    $valid = false;
                    $response = 'Invalid file extension.';
                }

And this is where the files go (files.php) 这就是文件所在的位置(files.php)

<?php
        //scan "uploads" folder and display them accordingly
       $folder = "uploads";
       $results = scandir('uploads');
       foreach ($results as $result) {
        if ($result === '.' or $result === '..') continue;

        if (is_file($folder . '/' . $result)) {
            echo '
            <div class="col-md-3">
                <div class="thumbnail">
                    <img src="'.$folder . '/' . $result.'" alt="...">
                        <div class="caption">
                        <p><a href="remove.php?name='.$result.'" class="btn btn-danger btn-xs" role="button">Remove</a></p>
                    </div>
                </div>
            </div>';
        }
       }
       ?>

Cheers! 干杯!

If you want to add a link where other users can download the files that are uploaded, try adding an <a> tag with the link of the file in it: 如果要添加其他用户可以下载上传文件的链接,请尝试添加<a>标签,其中包含文件的链接:

<?php
  //scan "uploads" folder and display them accordingly
  $folder = "uploads";
  $results = scandir('uploads');
  foreach ($results as $result) {
    if ($result === '.' or $result === '..') continue;

    if (is_file($folder . '/' . $result)) {
      $file = $folder.'/'.$result;
      echo '<a href="{$file}" target="_blank">{$result}</a><br/>';
      //other codes
    }
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM