无法通过API将文件从Wordpress插件上传到symfony后端服务器? [英] Unable to upload file from Wordpress plugin to symfony backend server over the API?

查看:80
本文介绍了无法通过API将文件从Wordpress插件上传到symfony后端服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有两个部分

  • wordpress前端
  • Symfony后端

我正在尝试从Wordpress插件调用may后端symfony项目的API,以在后端存储一些数据.所有的文本数据都被保存,但是当我尝试发送图像文件时,它们并没有保存在后端.我知道这是一项棘手的事情,但我确实在寻找解决方案.

I am trying to calling the API of may backend symfony project from Wordpress plugin to store some data in the backend. all the text data is being saved but when I am trying to send image files they are not being saved in the backend. I know its a tricky business but I am really looking for the solution.

对于上传图像,我们正在执行以下步骤

for uploading images we are doing following steps

  1. 我们正在将图像临时上传到我们的Wordpress上传文件夹
  2. 然后我们将在调用api的请求中发送这些文件
  3. symfony后端API正在获取请求并相应地上传图像

这是我的Wordpress前端代码及其请求变量:

here is my Wordpress front end code and its request variable:

$localUploadPath  = dirname(__DIR__).DIRECTORY_SEPARATOR.'uploads';
$randomDir  = Helper::randomDirName();
$uploadedAssets = Files::uploadPassAssets( $localUploadPath , $randomDir );
$request = new Request();
$submitedValues = $request->all();
$request->files($uploadedAssets);
$groupArray = $request->getGroups();

//remote files upload
$remoteFiles = array();
foreach($uploadedAssets as $fieldName => $imageFiles){
    $imageWithPath = $localUploadPath.'/'.$randomDir.'/'.$imageFiles; 
    $remoteFiles[$fieldName.'File'] = '@'.realpath($imageWithPath);
}
$client = new ClientApi(PASSBUILDER_ADDPASS_UPLOAD_URL);
$client->setFiles($remoteFiles);
$uploadResponse = $client->getResponse();
$this->rrmdir($localUploadPath.'/'.$randomDir);

对api的要求是:

{ ["appearance_logoNameFile"]=> string(149) "@/home/public_html/ads/wp-content/plugins/passbook-app/uploads/4e1020ee2b0dda294c746b5bb5acc0bd/26f27e532c874b63dca651dec4553b20ca237a44.png" ["appearance_eventTicketStripFile"]=> string(149) "@/home/public_html/ads/wp-content/plugins/passbook-app/uploads/4e1020ee2b0dda294c746b5bb5acc0bd/7354f12e6d185eee8142268b32eea6a055036d35.png" ["generalid"]=> string(32) "4ba11ffe3f2b8a46626632b48b38fcaf" ["pass_id"]=> int(198) ["email"]=> string(17) "sample@unknown.com" }

图像会被物理保存在Wordpress上传文件夹中,并通过api发送,但我们的后端没有拾取图像,也没有上传图像

images are physically saved in the Wordpress upload folder and also being sent through api but our backend is not picking up the images and are not uploading them

这是我们的symfony后端代码:

here is our backend code of symfony:

public function createpassuploadAction(Request $request){

    $data = $request->request->all();
    $helper = $this->get('passbook.passhelper');
    $webDir = $helper->webDir;

    $passId = $data['pass_id'];
    $userEmail = $data['email'];
    $generalid = $data['generalid'];

    $filesResult = array();
    $filesBag = $request->files->all();
    foreach ($filesBag as $file){
        $filename = $file->getClientOriginalName();
        $filesResult []=  array(
            'path' => $file->getPathname(),
            'url'  => 'ddd'
        );
        $src    =  $webDir.'/upload/'.$passId.'/';
        $file->move( $src ,$filename );
    }
}

为了记录,此代码在2年前已完美运行,但突然停止工作:

For the record this code was perfectly running 2 years ago but it suddenly stoped working:

我收到此错误

request.INFO: Matched route "curd.api.pass.create.uploads" (parameters: "_format": "json", "_controller": "Cogilent\PassbookBundle\Controller\ApiController::createpassuploadAction", "_route": "curd.api.pass.create.uploads") [] []
[2017-06-04 04:57:07] security.INFO: Populated SecurityContext with an anonymous Token [] []

推荐答案

从PHP 5.5起,在'@'.realpath($path)处出现错误,而在此之上,使用curl_file_create(realpath($path))而不是'@'.问题解决了

there was an error at '@'.realpath($path) from php 5.5 and above this is used curl_file_create(realpath($path)) instead of '@'. problem solved

这篇关于无法通过API将文件从Wordpress插件上传到symfony后端服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆