I am trying to send file and some other data by cruel to API . i want to appended data to JSON file and send. I can do it but in server i can not get uploaded file. When i don't use format JSON i have not any problem that i access to $_file but in JSON I have not any file i have some data string only
client:
if (isset($_REQUEST['action'])) {
if ($_REQUEST['action'] == 'submit') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$filePath = $_FILES['file_upl']['tmp_name'];
$type = $_FILES['file_upl']['type'];
$fileName = $_FILES['file_upl']['name'];
echo "filePath::" . $filePath;
echo "<br>fileName:" . $fileName;
$data = (['name' => 'Foo', 'file_upl' => curl_file_create($filePath, $type, $fileName)] );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $data ) );
$result = curl_exec($ch);
curl_close($ch);
echo "<br>result: " . $result;
}
}?>
<form name="file_up" action="" method="POST" enctype="multipart/form-data">
Upload your file here
<input type="file" name="file_upl" id="file_upl"/>
<input type="submit" name="action" value="submit"/>
</form>
serve:
$data = json_decode(file_get_contents('php://input'), true);
echo "<pre>";
echo 'in web SErvice: <br/>';
echo (print_r($data));
echo (print_r( $data->));
echo $data->file_upl->name;
echo $data->file_upl->tmp_name;
move_uploaded_file($data->file_upl->name, Yii::app()->basePath . "/files/" . $data->file_upl->postname);
result:
stdClass Object
(
[name] => Foo
[file_upl] => Array
(
[0] => stdClass Object
(
[name] => C:\xampp\tmp\php8843.tmp
[mime] => image/jpeg
[postname] => 1-7.jpg
it is important for me that use JSON format. please tell me how can use std Class Object in to method move_uploaded_file that can be get file.