Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

phosmakara commented May 11, 2015

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
You can’t perform that action at this time.