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
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
}
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With