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

时间:2016-03-10 09:21:37

标签: php html file-upload

我正在做一个学校项目,学生可以上传学习笔记来帮助其他学生。我已经创建了上传文字/页面文件的功能,但是我希望在文件上传后启用,其他用户可以点击该文件并为其下载。

不确定要放什么代码..(这是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.';
                }

这就是文件的位置(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>';
        }
       }
       ?>

干杯!

1 个答案:

答案 0 :(得分:1)

如果您想添加其他用户可以下载上传文件的链接,请尝试添加<?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 } } ?> 标记及其中的文件链接:

{{1}}