cakephp rest api multiple file uploading - php

I trying to upload multiple filesby REST API in cakephp V2.3. I am running api by postman addon of chrome.
The issue is the format of array of files. Below is the format I am getting. It is hard to get data from the above array. Please guide me how set key to get value in standard format.
Array
(
[Reports] => Array
(
[name] => Array
(
[0] => Chrysanthemum.jpg
[1] => Jellyfish.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpDZoRzW
[1] => /tmp/phpVyb98b
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 879394
[1] => 775702
)
)
)

I am not entirely sure I got your question right but I think one problem lies in the data format. I assume you are using the savemany method. The data is expected in this format:
$data = array(
array('name' => 'Chrysanthemum.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpDZoRzW', 'error' => 0, 'size' => 879394),
array('name' => 'Jellyfish.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpVyb98b', 'error' => 0, 'size' => 775702)
);
Basically you are providing the data field by field instead of record by record. I don't think that cake can process this correctly.
To get data in the desired format you can either loop through the data or use the convenience method Hash that cakephp provides, e.g. by using extract on the required keys/values.
Receive the data in the right format
If you are able to change the submit-form you name the file input fields like this. The result should be the desired format.
<input type="file" name="Report.0">
<input type="file" name="Report.1">
This will result in the format:
[Reports] => Array
(
[0] => Array
(
[name] => 'Chrysanthemum.jpg'
[type] => 'image/jpeg'
)
[1] => Array
(
[name] => 'Jellyfish.jpg'
[type] => 'image/jpeg'
)
)
You should use MODELNAME.{n}.FIELDNAME as naming for form fields in Cakephp, though. So if reports is your model it would make sense having a field name for your file-field.

Related

How to Filter course object fields from Google Classroom courses list using PHP

I want to filter course object fields from the Google Classroom courses list. When I call API for the course list It responds to all objects. But I want specific course object names, sections from the course list.
$optParams = array(
'pageSize' => 100,
'courses' => 'name','section',
'fields' => 'courses(id)'
);
$results = $service->courses->listCourses($optParams);
How I get specific course object names, sections from the course list using PHP.
The below problem shows if the code test
Fatal error: Uncaught Google\Exception: (list) unknown parameter: 'courses' in C:\xampp\htdocs\classroom\vendor\google\apiclient\src\Service\Resource.php:153 Stack trace: #0 C:\xampp\htdocs\classroom\vendor\google\apiclient-services\src\Classroom\Resource\Courses.php(122): Google\Service\Resource->call('list', Array, 'Google\\Service\\...') #1 C:\xampp\htdocs\classroom\quickstart1.php(70): Google\Service\Classroom\Resource\Courses->listCourses(Array) #2 C:\xampp\htdocs\classroom\quickstart1.php(132): test('406487331584') #3 {main} thrown in C:\xampp\htdocs\classroom\vendor\google\apiclient\src\Service\Resource.php on line 153
If the code part will as below then the response has all objects names without value and shows fields object name,section and value which I want.
$optParams = array(
'pageSize' => 100,
'fields' => 'courses(name,section)'
);
$results = $service->courses->listCourses($optParams);
List Courses Response Object:
[courses] => Array
(
[0] => Google\Service\Classroom\Course Object
(
[collection_key:protected] => courseMaterialSets
[alternateLink] =>
[calendarId] =>
[courseGroupEmail] =>
[courseMaterialSetsType:protected] => Google\Service\Classroom\CourseMaterialSet
[courseMaterialSetsDataType:protected] => array
[courseState] =>
[creationTime] =>
[description] =>
[descriptionHeading] =>
[enrollmentCode] =>
[guardiansEnabled] =>
[id] =>
[name] => Android
[ownerId] =>
[room] =>
[section] => PC-D
[teacherFolderType:protected] => Google\Service\Classroom\DriveFolder
[teacherFolderDataType:protected] =>
[teacherGroupEmail] =>
[updateTime] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[1] => Google\Service\Classroom\Course Object
(
[collection_key:protected] => courseMaterialSets
[alternateLink] =>
[calendarId] =>
[courseGroupEmail] =>
[courseMaterialSetsType:protected] => Google\Service\Classroom\CourseMaterialSet
[courseMaterialSetsDataType:protected] => array
[courseState] =>
[creationTime] =>
[description] =>
[descriptionHeading] =>
[enrollmentCode] =>
[guardiansEnabled] =>
[id] =>
[name] => CSS
[ownerId] =>
[room] =>
[section] => PC-D
[teacherFolderType:protected] => Google\Service\Classroom\DriveFolder
[teacherFolderDataType:protected] =>
[teacherGroupEmail] =>
[updateTime] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
But I need to Look like as below:
[courses] => Array
(
[0] => Google\Service\Classroom\Course Object
(
[name] => Android
[section] => PC-D
)
)
What you are looking for is something called partial response. All of the google apis support it as far as I know.
The way it works is you request the properties you want to see. It can be a bit tricky to get to work I recommend using the try me on the documentation page to test it out before you add it to your code.
Lets look at how it works though.
This is the response object for your method. It returns a list of courses. So the first thing you will probably want is courses.
{
"courses": [
{
object (Course)
}
],
"nextPageToken": string
}
Then you may want to limit the object within courses. Which is a course object A course object has an id. So what if you just wanted all the ids for all the course.
The code should look something like this.
$optParams = array(
'pageSize' => 100,
'courses' => 'name','section',
'fields' => 'courses(id)'
);
$results = $service->courses->listCourses($optParams);
Note i dont have access to this api so i cant test it let me know if you have any issues.
update from updated question and comment
What fields does is it ensures that the fields you ask for are only retured. So by the looks of it the changes i sugested are working
However you state you want to see something like this
[courses] => Array
(
[0] => Google\Service\Classroom\Course Object
(
[name] => Android
[section] => PC-D
)
)
That is not possible. The API will just return nulls in the fields that you request partial response is just intended to make the request faster. Your code should just ignore the null fields.

CodeIgniter Loses Images when Form Validates

I've got a multipart form that's set to accept multiple images from a single field, defined as follows:
echo form_upload('userfile[]', set_value('userfile[]'), 'multiple');
When I run a successful form (using CodeIgniter's built-in validator), it only uploads one image (the last one in the list).
Suspicious, I did a dump of the $_FILES global when the form validated, and it showed the following:
Array
(
[userfile] => Array
(
[name] => image_three.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpZl6RHT
[error] => 0
[size] => 18236
)
)
If I go back and clear out any required field (it doesn't matter which), the same input shows the following in $_FILES:
Array
(
[userfile] => Array
(
[name] => Array
(
[0] => image_one.jpg
[1] => image_two.jpg
[2] => image_three.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpEFaVib
[1] => /tmp/phpkcXJmL
[2] => /tmp/phpoqcDql
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 43308
[1] => 60883
[2] => 18236
)
)
)
Based on the fact that the incomplete form registers all three images properly, I'm assuming that the form is defined properly, and that something in the validation engine is what's causing it. That said, my attempt to test that assumption by disabling all of the form validation code returned the one-file version of $_FILES. For the record, there are no validation rules tied to the file upload field.
Any ideas on what is causing the form to lose the additional images?

Decode Base 64 in Array

im trying to save a base64 image into the webserver from a post request, currently my array looks like this.
(
[0] => Array
(
[id] => 0
[storename] => test
[notes] => test
[image] => data:image/jpeg;base64,/9j/4........to long
When i try to update my code to allow for decode and save all i get echo from image is a number.
PHP
$random = md5(rand());
$nonerejected[] = array(
'id' => $data['id'],
'storename' => $data['storename'],
'notes' => $data['notes'],
'image' => $data['image'] == "" ? "" : file_put_contents(''.$random.'.JPG',base64_decode($data['image'])),
);
Array output
(
[0] => Array
(
[id] => 0
[storename] => test
[notes] => test
[image] => 503331
)
)
Any ideas guys?
The data:image/jpeg;base64, bit isn't base64, so base64 can't really do anything with it. You'll have to strip this off in order to store your file.

PDOStatement::getColumnMeta returns original table name instead of view name

I am using the CakePHP framework. When returning the results of a query, the framework calls the "experimental" PDOStatement::getColumnMeta to "arrayify" the data when it comes back from the database. However, there are mixed results depending on the query.
There are times when the array of data comes back as expected where all columns are associated to the name of the view. Other times, the data comes back mixed, where some of the data sits in an array associated with the original table that corresponds to the view.
// correct
Array(
[MyInstall] => Array
(
[id] => a6d1342a-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2009-06-15 12:43:30
[version] => 3.2.1
[admin_url] => wp-admin
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
//incorrect
Array(
[MyInstall] => Array
(
[id] => c71a2368-7b4d-11e1-8397-60195b7d6275
[user_id] => dc038c9e-7b4b-11e1-8397-60195b7d6275
[path] =>
[url] =>
[created] => 2011-11-07 22:26:38
[version] => 3.2.1
[admin_url] => wp-admin
)
[Script] => Array
(
[script_id] => 057de1e0-7b48-11e1-8397-60195b7d6275
[name] => WordPress
[icon] => icon_WordPress.gif
)
)
The way the results are built is from the results of the PDOStatment::getColumnMeta. Here is what a sample result of getColumnMeta looks like:
Array
(
[native_type] => STRING
[pdo_type] => 2
[flags] => Array
(
[0] => not_null
)
[table] => MyInstall
[name] => id
[len] => 108
[precision] => 0
)
Any suggestions on how I can get this same information using PDO for MySQL? Or is there another solution to this problem?
BTW: I already filed a bug with the PHP folks on this.
As it turns out, this is a now known bug in MySQL: http://bugs.mysql.com/bug.php?id=66794, still pending at the time of writing.

Trying to set array variable from part of another array variable

I'm trying to set a part of array variable into a new array variable like below:
$this->data['uploadFront'] = array();
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];
But I'm getting undefined index error.
The $this->data['Card'] array is like below:
Array
(
[Card] => Array
(
[name] =>
[company_name] =>
[firstname] =>
[lastname] =>
[position] =>
[location] =>
[phone] =>
[website] =>
[mobile] =>
[comp_name] => 0
[uploadFront] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[uploadBack] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
)
What could be wrong that needs to be fixed in this process?
$this->data['uploadFront'][] = $this->data['Card']['uploadFront'];
If you want to append to array, you have to use [] to show that you are appending, not assinging.
Sorry, I didn't read your post right the first time.
As Catalin told you, if your array is what you tell us, ie $this->data['Card'] = ARRAY, you're creating an unnecessary extra level: two times 'Card', so ['Card']['Card']. I propose you define $this->data as what you told is your array, and then ask to copy ['uploadFront'] to the first level of your $this->data array. In what you described you're trying to access an unexisting part of the array.
In code (probably you'll need to declare the $this->data array somewhere else in your classes, but for the sake of explaining I write it down like this):
$this->data = array
(
'Card' => array
(
'name' => '',
'company_name' => '',
...
'uploadFront' => array
(
'name' =>'',
'type' => '',
'tmp_name' => '',
'error' => '4',
'size' => '0'
)
'uploadBack' => array
(
...
)
)
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];

Categories