Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created December 30, 2014 11:15
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 niraj-shah/0676f64714988fe53fa7 to your computer and use it in GitHub Desktop.
Save niraj-shah/0676f64714988fe53fa7 to your computer and use it in GitHub Desktop.
How to upload a file to Parse using the Official Parse PHP SDK
<?php
// define location of Parse PHP SDK, e.g. location in "Parse" folder
// Defaults to ./Parse/ folder. Add trailing slash
define( 'PARSE_SDK_DIR', './Parse/' );
// include Parse SDK autoloader
require_once( 'autoload.php' );
// Add the "use" declarations where you'll be using the classes
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseException;
use Parse\ParseFile;
// init Parse
ParseClient::initialize('xxx', 'yyy', 'zzz');
// see if we have a file
if ( isset( $_FILES['image'] ) ) {
// save file to Parse
$file = ParseFile::createFromData( file_get_contents( $_FILES['image']['tmp_name'] ), $_FILES['image']['name'] );
$file->save();
// save something to class TestObject
$testObject = ParseObject::create( "TestObject" );
$testObject->set( "foo", "bar" );
// add the file we saved above
$testObject->set( "image", $file );
$testObject->save();
// get the object ID
echo 'Object Saved with ID: <strong>' . $testObject->getObjectId() . '</strong>.<br/>';
// get the file URL
echo 'File URL: <a href="' . $file->getURL() . '" target="_blank">' . $file->getURL() . '</a>';
} else {
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
}
@phosmakara
Copy link

how to upload mp3 file?
$file = ParseFile::createFromData( file_get_contents( $_FILES['mymusic']['tmp_name'] ), "newmusic.mp3");
$file->save();

I got this error
Warning: file_get_contents(): Filename cannot be empty
Fatal error: Uncaught exception 'Parse\ParseException' with message 'Cannot retrieve data for unsaved ParseFile.

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