如何使用php sdk保存多个文件对象来解析后端?

Fra*_*eap 5 php html5 parse-platform

我有一个类型文件(多个)的html输入:

  <input id="image" type="file" name="dog[]" multiple>
Run Code Online (Sandbox Code Playgroud)

如何使用解析关系将它们上传到Parse,其中包含"Images"和"Dog"的表格?

或者有更好的方式来存储关系?请指教.谢谢

代码尝试:

        <?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();
     }
Run Code Online (Sandbox Code Playgroud)

编辑::感谢您的回复,这是我发现的错误

注意:未定义的索引:第31行的../index.php中的restaurant_images

警告:在第31行的../index.php中为foreach()提供的参数无效使用objectId创建的新对象:fz1hnCembE

第31行是指 foreach ($_FILES['restaurant']['name'] as $f => $name) {

我在上面的消息中看到的成功保存的对象,找不到图像

cet*_*ver 2

确保表单具有enctype带有值的属性multipart/form-data,否则超全局数组$_FILES将为空

<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
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)