When wanting to use file.save service endpoint and sending an image lets say from a mobile device to the end point, if you add to your array
// file
$file = array(
'filesize' => filesize($filename),
'filename' => basename($filename),
'file' => base64_encode(file_get_contents($filename)),
'filepath' => 'public://images/' . $logged_user->user->uid . "/" . basename($filename) ,
'uid' => $logged_user->user->uid,
);
and then look in the database file_managed you will see that it always inserts a 0 in, aka anonymous user, which isnt great cause I have no way to know who is uploading what from mobile devices.
Even if i hardcode uid => 1, for example it still doesnt save it, I believe this is because of the way file_save_data is handled but was unable to dig further.
Here is a small text php example that would work.. note this is drupal 7..
<?php
// node data
$filename = 'simefilename.png';
$services_url = 'http://yoururl/yourendpoint';
/*
* Server REST - user.login
*/
// REST Server URL for auth
$request_url = $services_url . '/user/login';
// User data
$user_data = array(
'username' => 'xxxx',
'password' => 'xxxx',
);
$user_data = http_build_query($user_data);
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo "User Login" . $response . "</br>";
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die('Auth error ' . $http_message);
}
// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;
echo "coookie" . $cookie_session;
/*
* Server REST - file.create
*/
if(!file_exists($filename)) {
die('File not exists');
}
if(!is_readable($filename)) {
die('File not readable');
}
// file
$file = array(
'filesize' => filesize($filename),
'filename' => basename($filename),
'file' => base64_encode(file_get_contents($filename)),
'filepath' => 'public://images/' . $logged_user->user->uid . "/" . basename($filename) ,
'uid' => 1, $logged_user->user->uid,
);
$file = http_build_query($file);
// REST Server URL for file upload
$request_url = $services_url . '/file';
echo "request url = " . $request_url;
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded','Accept: application/json'));
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $file); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo "File Data Saved" . $response . "</br>";
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$file_data = json_decode($response);
print_r($file_data);
}
else {
// Get error msg
$http_message = curl_error($curl);
die('Sending file error<br>' . $http_message . "\n<br>");
}
// file id (nessesary for node)
$fid = $file_data->fid;
... rest of code if you want to attach the fid to a new node....
?>
Comments
Comment #1
ygerasimov commentedWhen you create a file, it is created via file_save_data() core function. There only global $user is used, so if your calls are done by anonymous users, they will be saved with uid = 0.
As the solution for your case I would recommend to login before creating a file, so uid will be properly stored.
Moving this issue to "support request".
Comment #2
ygerasimov commentedClosing issue. Please feel free to reopen if have any difficulties.