Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save multiple file object to parse backend with php sdk?

I have a html input with type file (multiple):

  <input id="image" type="file" name="dog[]" multiple>

How can i upload them to Parse with a table for "Images" and "Dog" using parse relation?

Or is there a better way for to store the relation? Please advice. thanks

Code attempted:

        <?php
        require 'vendor/autoload.php';

        session_start();

        use Parse\ParseClient;
        use Parse\ParseUser;
        use Parse\ParseSessionStorage;
        use Parse\ParseObject;
        use Parse\ParseFile;
        use Parse\ParseGeoPoint;

        ParseClient::initialize('xxx', 'yyy', 'zzz');
        ParseClient::setStorage(new ParseSessionStorage());
        $currentUser = ParseUser::getCurrentUser();

        $filearray = [];

        $count = 0;

        if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
            foreach ($_FILES['dog']['name'] as $f => $name) {
                if ($_FILES['dog']['error'][$f] == 4) {
                    continue; // Skip file if any error found
                }
                if ($_FILES['dog']['error'][$f] == 0) {
                    $tmp = $_FILES['dog']['tmp_name'][$count];
                    $count = $count + 1;

                    $file = ParseFile::createFromData(file_get_contents($tmp), $_FILES['dog']['name']);
                    $file->save();
                    array_push($filearray, $file);
                }
            }
        }
     $dogobj = new ParseObject("Dog");
     $dogobj->setArray("dogimage", $filearray);

     try {
         $dogobj->save();
     }  catch (ParseException $ex) {
         echo 'Failed to create new object, with error message: ' . $ex->getMessage();
     }

EDIT:: Thanks for the reply, this is the error i found

Notice: Undefined index: restaurant_images in ../index.php on line 31

Warning: Invalid argument supplied for foreach() in ../index.php on line 31 New object created with objectId: fz1hnCembE

By line 31, it is referring to foreach ($_FILES['restaurant']['name'] as $f => $name) {

The object i saved successful as u see in the message above, the image is not found

like image 638
Francis Yeap Avatar asked Oct 30 '22 19:10

Francis Yeap


1 Answers

Ensure that form has attribute enctype with value multipart/form-data otherwise superglobal array $_FILES will empty

<form action="" method="post" enctype="multipart/form-data">
    <input id="image" type="file" name="dog[]" multiple>
    <input type="submit"/>
</form>

<?php
if (isset($_FILES['dog']) === true) {
    $dog = $_FILES['dog'];
    $limit = count(current($dog));
    for ($i = 0; $i < $limit; $i++) {
        $error = $dog['error'][$i];
        if ($error === UPLOAD_ERR_OK) {
            $name = $dog['name'][$i];
            $type = $dog['type'][$i];
            $tmp_name = $dog['tmp_name'][$i];
            $size = $dog['size'][$i];
            //other code
        }
    }
}
?>
like image 129
cetver Avatar answered Nov 02 '22 11:11

cetver