O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - uploading images to GridFS

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']; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required