【问题标题】:Upload Multiple Files and Download its Zip上传多个文件并下载其 Zip
【发布时间】:2014-09-02 20:53:13
【问题描述】:

我一直在使用MongoDBPHP 进行CRUD 操作。我可以开发一个演示应用程序以及文件上传功能,用户可以上传imageviewdelete

[编辑]

文件上传

<?php
if(isset($_POST['btn_submit']))
{
        $conn = new MongoClient();
        $db = $conn->selectDB('mydb');
        $gridfs = $db->getGridFS();

        $id = $gridfs->storeUpload('pic', array('tag' => ""));
        $gridfsFile = $gridfs->get($id);
        var_dump($gridfsFile->file);
        /*
         $files = $db->fs->files;
        $files->update(array("filename" => $_FILES['pic']['name']), array('$set' => array("enterby" => "Gayatri patel", "tag" =>"image")));*/
        $conn->close();
}
?>
<form method="POST" enctype="multipart/form-data">
    <label for="username">Select Type</label>
    <select name="document_type" id="document_type">
        <option value="image/jpeg">jpg</option>
        <option value="image/gif">gif</option>
        <option value="image/png">png</option>
        <option value="text/plain">txt</option>
        <option value="application/pdf">pdf</option>
        <option value="application/x-zip">zip</option>
    </select>

    <label for="pic">Please upload a profile picture:</label>
    <input type="file" name="pic" id="pic" />

    <input type="submit" name="btn_submit" />
</form>

文件列表

<?php
   $conn = new MongoClient();
    $db = $conn->selectDB('mydb');        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS

    $cursor = $grid->find();

    foreach ($cursor as $obj) {                   // iterate through the results
        echo 'Filename: <a href="fileDownload.php?file='.urlencode ($obj->getFilename()).'">'.$obj->getFilename().'</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Size: '.$obj->getSize().'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="fileDelete.php?file='.urlencode ($obj->getFilename()).'">Delete</a><br/>';
    }

    $conn->close();                                // Disconnect from Server
    exit(0);
?>

文件查看

<?php
    $conn = new MongoClient();
    $db = $conn->selectDB('mydb');        // Authenticate to MongoDB
    $grid = $db->getGridFS();                   // Initialize GridFS

   $ask = urldecode($_REQUEST['file']);                   // Get filename requested


    $file = $grid->findOne(array('filename' => $ask));
    $files = $db->fs->files;
    $file1 = $files->findOne(array('filename' => $ask));
    $id = $file->file['_id'];

    if ( (substr($ask,-3) == 'zip') || (substr($ask,-3) == 'pdf') ) {
       /* Any file types you want to be downloaded can be listed in this */
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename='.'"'.$ask.'"');
       header('Content-Transfer-Encoding: binary');
       $cursor = $db->fs->chunks->find(array("files_id" => $id))->sort(array("n" => 1));
       foreach($cursor as $chunk) {
          echo $chunk['data']->bin;
       }
    }
    else {
       //header('Content-Type: '.$file1["contentType"]);
        header('Content-Type: image/jpeg');
       echo $file->getBytes();
    }   

    $conn->close();                                // Disconnect from Server
    exit(0);
?>

文件删除

<?php
   $conn = new MongoClient();
    $db = $conn->selectDB('mydb');        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS

    $filename = urldecode($_REQUEST["file"]);                // Get requested filename

    $file = $grid->findOne($filename);             // Find file in GridFS
    $id = $file->file['_id'];                    // Get the files ID
    $grid->delete($id);                            // Delete the file

    $conn->close();                                // Disconnect from Server
    exit(0);
?>

现在我需要使用MongoDB 开发多个文件上传。用例如下

  1. 如果用户上传单张图片,请给view它提供选项
  2. 如果用户上传多个图像,即多个文件上传,请给出 download .zip 格式的选项。

我怎样才能实现它?

谢谢。

【问题讨论】:

  • 告诉我们你做了什么。

标签: php mongodb file-upload


【解决方案1】:
  1. 您不需要使用&lt;select name="document_type" id="document_type"&gt; 来确定文件mime 类型。文件 $_FILES[fieldname] 应该返回文件的 mime 类型 ($_FILES['pics']['type'])。
  2. 我假设您有多个上传字段?在您的表单名称上,所有文件输入为 pics[]。 "[]" 将使上传的文件成为 php 脚本中的数组。您可以通过在 $_FILES['pics'] 上运行 for 循环来检索它们;
  3. 文件可以使用 php 压缩,使用额外的扩展名,请先查看手册。 http://php.net/manual/en/zip.examples.php
  4. 在您在标头后回显 pdf 内容的行上,可能存在标头已发送错误。

`
//表格

<form method="POST" enctype="multipart/form-data">
<label for="username">Select Type</label>
<label for="pic">Please upload a profile picture:</label>
<input type="file" name="pics[]"/>
<input type="file" name="pics[]"/>
<input type="file" name="pics[]"/>
<input type="file" name="pics[]"/>

<input type="submit" name="btn_submit" />
</form>

``

    if(isset($_POST['btn_submit']) && isset($_FILES['pics'])){
        $conn = new MongoClient();
        $db = $conn->selectDB('mydb');
        $gridfs = $db->getGridFS('uploads');
        $uploadedIds = array();
        $acceptedTypes = array('image/jpeg', 'application/pdf');

        foreach ($_FILES['pics'] as $file) {
            if(in_array($file['type'], $acceptedTypes)){
                $id = $gridfs->storeFile($file['tmp_name'], array('tag' => "",'filename' => 'filename', 'type' => $file['type']));
                array_push($uploadedIds, (string)$id);
            }else{
                echo 'file is not of correct type';
            }
        }

        print_r($uploadedIds);
        $conn->close();
    }
    ?>

    <?php
    //view file
    // you need to feth files from /?files[]=file1.jpg&files[]=file1.jpg

        $conn = new MongoClient();
        $db = $conn->selectDB('mydb'); 
        $grid = $db->getGridFS('uploads');  

        $ask = $_GET['files']; 

        if(count($ask) == 1){
            $file = $grid->findOne(array('filename' => $ask[0]));
            header('Content-Type: '.$file['type']);
            echo $file->getBytes();
            exit();
        }else{
            header('Content-Type: application/octet-stream');
            header('Content-Transfer-Encoding: binary');

            foreach ($ask as $f) {
                $file = $grid->findOne(array('filename' => $f));
                echo $file->getBytes();
            }
            exit();
        }
    ?>

【讨论】:

  • 感谢您的回复,但我怎样才能与代码一起上传多个文件?我们有工作演示吗?
  • 多个如 php 或你的表单?
  • 多张图片上传或拖放。
  • 您能否修改我的代码以使其适用于多个文件上传?
  • @Slimshadddyyy 我没有测试代码。我认为应该这样做。
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 2012-02-21
相关资源
最近更新 更多