Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) "[email protected]" }

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": "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 [] []
like image 377
numerah Avatar asked Jun 04 '17 09:06

numerah


People also ask

What is the uploadedfile class in Symfony?

In Symfony applications, uploaded files are objects of the UploadedFile class. This class provides methods for the most common operations when dealing with uploaded files; A well-known security best practice is to never trust the input provided by users.

Why the uploaded file could not be moved to /wp-content/Uploads folder?

the uploaded file could not be moved to… This error is a result of failure while attempting to upload a file to your /wp-content/uploads folder. The first step in troubleshooting any issue is to attempt to gain an understanding of where the error is coming from, and how to replicate the issue.

How to import WordPress XML data if it failed?

Follow these steps to import your WordPress XML data even if it’s failed in the past. 1. Export XML data You probably already did this. If not, go to your old WordPress website. In the WP-Admin area, find the Tools > Export menu item. Hit the blue “Download Export File” button and save the XML file generated.

Why is my Wordpress site not working?

Technically, the failure isn’t WordPress’ fault. It’s the server’s. But of course WordPress’ system files can’t foresee or control that, so they have no way of alerting the user when it happens―there’s no WP error message for that.


1 Answers

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

like image 67
numerah Avatar answered Nov 13 '22 05:11

numerah