I use angular-upload-file with a server side built with PHP Slim Framework. On my FileUploader config I simply have :
$scope.uploader = new FileUploader({
url: 'upload/upload.php'
});
Then in my backend, like the related wiki does :
if ( !empty( $_FILES ) ) {
$tempPath = $_FILES[ 'file' ][ 'tmp_name' ];
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
move_uploaded_file( $tempPath, $uploadPath );
$answer = array( 'answer' => 'File transfer completed' );
$json = json_encode($answer);
echo $json;
} else {
echo 'No files';
}
The problem is when I put this code without using PHP Slim it works fine, but when I try to wrap it with PHPSlim , I get a GET request instead of POST:
with a such code :
$scope.uploader = new FileUploader({
url: 'API/upload/img'
});
And into the API :
$app->post('/upload/img', function(){
if ( !empty( $_FILES ) ) {
$tempPath = $_FILES[ 'file' ][ 'tmp_name' ];
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
move_uploaded_file( $tempPath, $uploadPath );
$answer = array( 'answer' => 'File transfer completed' );
$json = json_encode($answer);
echo $json;
} else {
echo 'No files';
}
});
I try to figure out where the issue come from, server or client side ? I tried to use a similar plugin ng-file-upload but I have exactly the same problem, so I think it should be PHPSlim but I am not sure.
I need to use PHPSlim because I use a middleware for some required authentication requests.
Thanks in advance,
First you need to call right address for the API:
var uploader = $scope.uploader = new FileUploader({
url: 'api/path/upload'
});
in backend you need
$app->post('/upload', function() {
if ( !empty( $_FILES ) ) {...
}
})
if you have problem with message response need to formatted I build on this way:
$answer = array(
'status' => 'success',
'message' => 'File transfer completed',
);
...
echoResponse(200, $answer);
...
function echoResponse($status_code, $response) {
global $app;
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response, JSON_NUMERIC_CHECK);
}
also create and set write folder permission for
$uploadPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $_FILES[ 'file' ][ 'name' ];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.