MP3 files are not loading in Mongodb with php - php

I can successfully upload / download image files in Mongodb with php through GridFS, but when I try to upload mp3 file, nothing happens. Neither I get any error nor any it is uploaded and further I have checked the permission of the file it's very similar of those image files plus the file size is also not too big it is around 6mb only. But for unknown reason it is not being uploaded in the Mongodb database through GridFS with php. Please help me loading the MP3 files to the database. The code used for uploading files is as below :
<?php
error_reporting(0);
$m = new MongoClient();
$gridfs = $m->selectDB('mydb')->getGridFS();
$gridfs->storeUpload('pic', array('_id'=>$_POST['_id'],'Username' => _POST['username'] ));
echo 'File Uploaded Successfully';
?>

Instead of storeUpload use storeFile as given below example
ex:
<?php
// connect to the ‘myGrid’ GridFS
$m = new Mongo();
$db = $m->myDB;
$myGrid = $db->getGridFS('myGrid');
$some_file='path of your video or audio file';
// some extra data you may want to store with your file
$data_array = array(
'mime' => mime_content_type($some_file),
'timestamp' => time(),
);
// store a file into the GridFS
$myGrid->storeFile($some_file, $data_array);
?>

Related

uploadFile function in Dropbox api returns nothing after uploading new file

i am facing issue regarding to upload file in dropbox via php.Actually i am using uploadFile function.This function supposed to return the meta data of newly uploaded file after uploading file.But in my case it returns nothing and browser throws 504 gateway timing error.But file uploads successfully.I am using this code.
echo 'Sending file to DropBox';
$appInfo = dbx\AppInfo::loadFromJsonFile(ABSPATH."wp-content/plugins/wp-cloud-safe/lib/app-info.json");
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
$filename=ABSPATH.'clients.webkitmedia.com_dfd_16th-February-2016-10:43.zip';
$dbxClient = new dbx\Client($this->dropboxGeneratedAccessToken, "PHP-Example/1.0");
//print_r($dbxClient);
$f = fopen($filename,'rb');
//$filesize=filesize($filename);
$reult= $dbxClient->uploadFile('/test/testing.zip', dbx\WriteMode::add(),$f);
print_r($reult);
die();
fclose($f);
Please suggest any idea?
I replace these lines in upper code and issue get resolved.
$f = file_get_contents($filename);
$reult= $dbxClient->uploadFileFromString('/test/testing.zip', dbx\WriteMode::add(),$f);

Zend File rename after uploading zend form

I'm not getting the information I am looking for from research. I'd like to perform a rename on the file upload after it has uploaded. I need the original filename as well as renaming it. Here is what I have so far:
$form = new Sam_Form_Database($this->resource);
$form->setMethod(Zend_Form::METHOD_POST);
if($this->getRequest()->isPost()){
if($form->isValid($this->getRequest()->getPost())){
$data = $form->getValues();
try {
$form->fileelement->receive();
$originalFilename = pathinfo($form->fileelement->getFileName());
$newFilename = Sam_Util::generateHash().'.'.$originalFilename['extension'];
$filterFileRename = new Zend_Filter_File_Rename(array(
'target' => $newFilename,
'overwrite' => true,
'keepExtension' => true
));
$filterFileRename->filter($form->fileelement->getFileName());
} catch(Exception $e){
Sam::exception("Cannot upload file");
}
Sam_Util::insertDataIntoDatabase($data,$this->resource);
Sam_Util::redirectSimple('list');
}
The problems:
nothing seems to be uploading
before when it was uploading it wasn't renaming the file in the destination
What I need is a fluent way to handle uploading, retrieving the original filename, and performing a rename on the target file using zend.

Getting the real URL of files uploaded in Moodle

I am trying to get the url to a file that I have uploaded to moodle. I want to access the file via the browser using http. The pathname of the file is hashed in the moodle database. Is there a way of getting the real url of uploaded files in moodle? this is the code I was trying out using the Moodle File API.
<?php
require_once("../config.php");
$course_name=$_GET["course"];
$table_files="files";
$results=$DB->get_records($table_files,array('filename'=>$course_name));
//Get the file details here::::
foreach($results as $obj){
$contextid=$obj->contextid;
$component=$obj->component;
$filearea=$obj->filearea;
$itemid=$obj->itemid;
}
$url=$CFG->wwwroot/pluginfile.php/$contextid/$component/$filearea/$itemid/$course_name;
echo print_r($url);
?>
Will appreciate the help.
This works for Moodle version 2.3
<?php
require_once("../config.php");
$filename = "my_image.jpg";
$table_files = "files";
$results = $DB->get_record($table_files, array('filename' => $filename, 'sortorder' => 1));
$baseurl = "$CFG->wwwroot/pluginfile.php/$results->contextid/$results->component/$results->filearea/$results->itemid/$filename";
echo $baseurl;
The result is the Url to the image "my_image.jpg"

How can I download the most recent file on FTP with PHP?

On FTP server has some files. For any hour on this server is uploading new files. I'd like to download last file. How can I get last uploaded file from this server? All files are with different names.
I used folowing script to downloading one file.
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"user","pass");
ftp_get($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);
Thanks in advance !!!
There is no way to be sure which file is the most recent, as there is no such thing as an "uploaded time" attribute. You didn't mention much about the FTP server, but if you have some level of management over the uploads, you could ensure that the last modified time is set on upload. Whether this ends up working is down to your FTP server and possibly clients.
Assuming your modified times are equivalent to upload times, you could do something like this:
// connect
$conn = ftp_connect('ftp.addr.com');
ftp_login($conn, 'user', 'pass');
// get list of files on given path
$files = ftp_nlist($conn, '');
$mostRecent = array(
'time' => 0,
'file' => null
);
foreach ($files as $file) {
// get the last modified time for the file
$time = ftp_mdtm($conn, $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
ftp_get($conn, "target.txt", $mostRecent['file'], FTP_ASCII);
ftp_close($conn);
ftp_rawlist($conn);
extract latest filename and get it.

PHP Transfer File on server to Rackspace Cloud Files

I am trying to script some code that will search a specified folder on the server for the latest file with a specific file extension (in my case .zip) and transfer that file to Rackspace's Cloud Files. The code below is as far i got and i keep getting the error:
Fatal error: Uncaught exception 'IOException' with message 'Could not open file for reading: Resource id #8' in /home/test/public_html/cloudapi/cloudfiles.php:1952 Stack trace: #0 /home/test/public_html/final.php(60): CF_Object->load_from_filename(Resource id #8) #1 {main} thrown in /home/test/public_html/cloudapi/cloudfiles.php on line 1952
The code that i'm using below was originally done for uploading content via an html upload form & i'm trying to adapt the same code to use a local server file instead of an uploaded file. You will see commented code with was previously for an upload script to show you how the upload script had worked.
<?php
// include the Cloud API.
require('cloudapi/cloudfiles.php');
// START - Script to find recent file with the extension .zip in current folder
$show = 2; // Leave as 0 for all
$dir = ''; // Leave as blank for current
if($dir) chdir($dir);
$files = glob( '*.zip');
usort( $files, 'filemtime_compare' );
function filemtime_compare( $a, $b )
{
return filemtime( $b ) - filemtime( $a );
}
$i = 0;
foreach ( $files as $file )
{
++$i;
if ( $i == $show ) break;
$value = $file; //Variable $value contains the filename of the recent file to be used in Cloud Files API
}
// END - Script to find recent file with the extension .zip in current folder
// START - Rackspace API code to upload content to cloud files container
// Rackspace Connection Details;
$username = "randomusername"; // put username here
$key = "234887347r93289f28h3ru283h2fuh23093402398"; // api key
// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
//Set the Container you want to use
$container = $conn->get_container('Backups');
//Temp store the file
//$localfile = $_FILES['uploadfile']['tmp_name'];
//$filename = $_FILES['uploadfile']['name'];
$localfile = fopen($value, "r");
$filename = $value;
//Uploading to Rackspace Cloud
$object = $container->create_object($filename);
$object->load_from_filename($localfile);
echo "Your file has been uploaded";
// END - Rackspace API code to upload content to cloud files container
?>
I know this is an old thread. But for the benefit of people who may land at this page while searching for a solution...
The problem with your code is:
The following statement opens the file for reading
$localfile = fopen($value, "r");
However when you place the load_from_filename call, the routine again tries to open the same file and fails because you already have it open in $localfile.
So comment out the previous command and you should be able to run the script successfully.
Error's being thrown in because content type not defined from read file.

Categories