我一直在与CRUD
和MongoDB
进行PHP
操作。我可以为文件上传功能开发一个演示应用程序,用户可以上传image
,view
和delete
。
[编辑]
文件上传
<?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> Size: '.$obj->getSize().'
<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
开发多个文件上传。用例如下
view
download
格式的.zip
选项。我怎样才能实现它?
感谢。
答案 0 :(得分:0)
<select name="document_type" id="document_type">
来确定文件mime类型。文件$ _FILES [fieldname]应该返回文件的mime类型($ _FILES [&#39; pics&#39;] [&#39; type&#39;])。`
//形式
<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();
}
?>