无法通过API将文件从Wordpress插件上传到symfony后端服务器?

num*_*rah 7 php api wordpress image-uploading symfony

我的项目有两个部分

  • wordpress前端
  • Symfony后端

我试图从Wordpress插件调用可能后端symfony项目的API来在后端存储一些数据.正在保存所有文本数据,但是当我尝试发送图像文件时,它们不会保存在后端.我知道这是一项棘手的业务,但我真的在寻找解决方案.

为了上传图像,我们正在执行以下步骤

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

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

$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);
Run Code Online (Sandbox Code Playgroud)

请求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" }
Run Code Online (Sandbox Code Playgroud)

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

这是我们的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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

为了记录这段代码完全在2年前运行,但它突然停止工作:

我收到了这个错误

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 [] []
Run Code Online (Sandbox Code Playgroud)

num*_*rah 3

从 php 5.5 及以上版本开始出现错误,'@'.realpath($path)使用该错误curl_file_create(realpath($path))而不是'@'. 问题解决了