Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

php - Unable to upload file from Wordpress plugin to symfony backend server over the API?

My project have two Parts

  • wordpress frontend
  • Symfony backend

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. we are temporarily uploading images to our Wordpress upload folder
  2. then we are sending those files in our request in calling api
  3. symfony backend API is getting the request and uploading the images accordingly

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);

request for the api is:

{ ["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" }

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

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 );
    }
}

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

I am getting this error

request.INFO: Matched route "curd.api.pass.create.uploads" (parameters: "_format": "json", "_controller": "CogilentPassbookBundleControllerApiController::createpassuploadAction", "_route": "curd.api.pass.create.uploads") [] []
[2017-06-04 04:57:07] security.INFO: Populated SecurityContext with an anonymous Token [] []
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...