Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Created May 13, 2012 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imjacobclark/2687223 to your computer and use it in GitHub Desktop.
Save imjacobclark/2687223 to your computer and use it in GitHub Desktop.
Multiple Image Uploader Using MongoDB To Store Image Paths
<form name="newad" method="post" enctype="multipart/form-data" action="uploader.php">
<br/>
<label for="file">Image 1:</label>
<input type="file" name="file" id="file" />
<label for="file">Image 2:</label>
<input type="file" name="file2" id="file2" />
<label for="file">Image 3:</label>
<input type="file" name="file3" id="file3" />
<label for="file">Image 4:</label>
<input type="file" name="file4" id="file4" />
<br/><br/>
<input type="submit" class='upload-button' name="submit" value="Upload" />
</form>
<br/><br/>
<?php
if($successfulUpload == 1){
echo "Successfully Uploaded!";
}else{
}?>
<?php
/* Demonstrates how to upload multiple images using PHP and insert the
* image path and a unique ID into a MongoDB database
* http://www.fusionstrike.com
*/
$m = new Mongo();
$db = $m->thundergallery; //Change to your database
$collection = $db->images; //Change to your collection
$cursor = $collection->find();
$upload_dir = "../uploads/"; //Specified the upload directory
if(isset($_POST['submit'])){ //Checks if the upload form has been submitted, if so, continue
$arr = array($_FILES["file"], $_FILES["file2"], $_FILES["file3"], $_FILES["file4"], $_POST['height'], $_POST['width'] ); //Begins the array for the file uploads
foreach ($arr as &$value) {
if ($value["error"] > 0){
//Error uploading the file, script stops here
}else{
if (file_exists($upload_dir . $value["name"])){
//Error uploading the file, a file with the same name already exists, script stops here
} else {
move_uploaded_file($value["tmp_name"], $upload_dir. $value["name"]);
$successful = 1; //Sets the upload flag to 1, will display sucsess message below upload form
$url = "/uploads/" . $value["name"]; //Places the Upload Path into the URL varliable
$unique_id = "content".rand(); //Generates a random ID and stores in within the unique_id variable
$obj = array( "url" => $url, "unique_id" => $unique_id, "height" => $height, "width" => $width ); //Adds the URL and Random ID to Mongo
$collection->insert($obj);
}
}
} //Ends the array
unset($value); //Unsets the value variable from the array
}else{
//If the submit form is not submitted, do nothing
}
?>
@ravi-nimavat128
Copy link

hello

It's awesome work,now i am trying to show image in browser using image _id but i can't fetch/show image in browser.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment