I have implemented CRUD
operations using PHP + MongoDB
. The below code is used to upload any file using GridFS
.
$conn = new MongoClient();
$db = $conn->selectDB('mydb');
$gridfs = $db->getGridFS('uploads');
foreach ($_FILES as $file) {
if(in_array($file['type'], $acceptedTypes)){
$id = $gridfs->storeFile($file['tmp_name'], array('filename' => $file['name'], 'type' => $file['type'], "id"=> 13, "parentId" => "10", "title" => $file['name'], "isFolder" => 0));
}else{
echo 'file is not of correct type';
}
}
$conn->close();`
I am able to upload any document using above code, but when I try to update the title
of any file it just do not work. The code to update the file name is as below.
$files = $db->fs->uploads;
$primId = $_GET['id_value'];
$renameTitle = $_GET['name'];
$updateData = array("title" => $renameTitle, "updatedDate" => date('Y-m-d'));
$files->update(array("id" => $primId), array( '$set' => $updateData ));
$conn->close();
This will update the title
of any file whose ID
is passed. The code works if I need to update any record but its not working for any GridFS
file upload.
To be more precise if I pass ID
like below,
$file = $gridfs->findOne(array('id' => 13)); **//WORKS**
$file = $gridfs->findOne(array('id' => $_GET['id'])); **//DO NOT WORKS**
Is there any other method to be used in order to update GridFS
records ?
Thanks.