How to save multiple files for backend analysis using php sdk?

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

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

How can I upload them to Parse with the β€œImages” and β€œDog” tables using the parse ratio?

Or is there a better way to keep the attitude? Please advise. thanks

The code tried:

  <?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 answer, this is the error I found

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

A warning. Invalid argument provided by foreach () in .. /index.php on line 31 New object created using objectId: fz1hnCembE

On line 31, this refers to foreach ($_FILES['restaurant']['name'] as $f => $name) {

The object I saved successfully, as you see in the message above, the image was not found

+5
source share
1 answer

Make sure that the form has an enctype attribute with the value multipart/form-data , otherwise the superglobal array $_FILES will be 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 } } } ?> 
+2
source

All Articles