We are going to build a simple image uploader with PHP that will store images uploaded by the user to GridFS. Let's start:
1. Open your text editor and put the following code in a new file:
<?php require 'dbconnection.php'; $action = (isset($_POST['upload']) && $_POST['upload'] === 'Upload') ? 'upload' : 'view'; switch($action) { case 'upload': //check file upload success if($_FILES['image']['error'] !== 0) { die('Error uploading file. Error code '. $_FILES['image']['error']); } //connect to MongoDB sevrer $mongo = DBConnection::instantiate(); //get a MongoGridFS instance $gridFS = $mongo->database->getGridFS(); $filename = $_FILES['image']['name']; $filetype = $_FILES['image']['type']; $tmpfilepath = $_FILES['image']['tmp_name']; $caption = $_POST['caption']; //storing the uploaded file $id = $gridFS->storeFile($tmpfilepath, array('filename' => $filename, 'filetype' => $filetype, 'caption' => $caption)); break; default...