php - yii2 REST Api file upload using PUT -
php - yii2 REST Api file upload using PUT -
i trying add together rest api in yii2 used mobile applications upload image/audio file. trying utilize set method image/file info http form-data, reason fopen("php://input", "r"); returns empty stream. tried code give in sample http://www.php.net/m...put-method.php.
<?php /* set info comes in on stdin stream */ $putdata = fopen("php://input", "r"); /* open file writing */ $fp = fopen("myputfile.ext", "w"); /* read info 1 kb @ time , write file */ while ($data = fread($putdata, 1024)) fwrite($fp, $data); /* close streams */ fclose($fp); fclose($putdata); ?>
meanwhile, using post method works though. using next code post
$putdata = fopen($_files['photo']['tmp_name'], "r"); $filename = $this->documentpath.uniqid().'.jpg'; /* open file writing */ $fp = fopen($filename, "w"); /* read info 1 kb @ time , write file */ while ($data = fread($putdata, 1024)) fwrite($fp, $data); /* close streams */ fclose($fp); fclose($putdata);
here's how send set curl:
curl -x set -d 'blablabla' http://localhost/upload
then disable csrf validation in upload controller:
\yii::$app->request->enablecsrfvalidation = false;
here's illustration upload controller action using code:
public function actionindex() { \yii::$app->request->enablecsrfvalidation = false; $putdata = fopen("php://input", "r"); // create sure have /web/upload directory (writeable) // work $path = \yii::getalias('@webroot')."/upload/myputfile.ext"; $fp = fopen($path, "w"); while ($data = fread($putdata, 1024)) fwrite($fp, $data); /* close streams */ fclose($fp); fclose($putdata);
}
check upload:
$ cat /path/to/webroot/upload/myputfile.ext blablabla
php rest yii2
Comments
Post a Comment