Upload image/video file to server using filetransfer


#1

Hello,
i am having problem in uploading image/video to server using php apis.

Ionic side code:

    this.params.u_id = this.user.u_id;
    this.params.country = this.user.country;
    this.params.ad_type = this.adType;
const fileTransfer: FileTransferObject = this.transfer.create();
    const options: CameraOptions = {
      quality: 100,
      mediaType: this.adType - 1,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      targetWidth: 250,
      targetHeight: 250,
    };
    this.platform.ready().then((readySource) => {
      console.log(options.mediaType);
      this.camera.getPicture(options).then((imageData) => {
        // this.loaderProvider.show();

        if (this.adType == 1) {
          this.params.path = imageData;
          this.params.thumbnail_image = imageData;
        } else {
          this.params.path = imageData;
          this.params.thumbnail_image = 'assets/icon/video-player.png';
        }
        let options1: FileUploadOptions = {
          params: this.params,
          chunkedMode: false,
          fileKey: 'file',
          mimeType: "multipart/form-data"
        }
        console.log(imageData);
        var upload = fileTransfer.upload(imageData, encodeURI(this.globals.SERVER_URL + 'addMarketingAd'), options1).then((data) => {
          console.log("success: ", data);
          // IN CONSOLE message "please upload video or image" 
          this.loaderProvider.hide();
          const toast = this.toast.create({
            message: "Ad successfully uploaded",
            duration: 3000,
            position: 'bottom'
          });
          toast.present();
        }, (err) => {
          // fileTransfer.abort();
          console.log("Cannot upload image");
          this.loaderProvider.hide();
          const toast = this.toast.create({
            message: "Cannot upload this image",
            duration: 3000,
            position: 'bottom'
          });
          toast.present();
        });
      }, (err) => {
        console.log("image upload :", err);
      });
    }).catch(err => {
      console.log("device is not ready");
    });

PHP side Code

public function actionaddMarketingAd(){
    
    $wb = new Webservice();
    $fields = $_REQUEST;
    // Set parameters
    if (isset($fields['u_id']) && $fields['u_id'] != '' && isset($fields['country']) && $fields['country'] != '' && isset($fields['user_type']) && $fields['user_type'] != '' && isset($fields['ad_type']) && $fields['ad_type'] != '' && isset($fields['ad_category']) && $fields['ad_category'] != '' ) {


        // images / video upload 
        if(isset($_FILES["path"]))
        {   
               
                    $uploadedFile=$_FILES["path"]["name"];
                    $filename=explode(".", $uploadedFile);
                    $fileext = $filename[count($filename) - 1];
                    $file_name = time(). '.'.$fileext; 
                    move_uploaded_file($_FILES["path"]["tmp_name"],$this->markrtingPath .$file_name);
                    

                    if(isset($_FILES["thumbnail_image"]))
                    {
                        $thumbnail_uploadedFile=$_FILES["thumbnail_image"]["name"];
                        $thumbnail_filename=explode(".", $thumbnail_uploadedFile);
                        $thumbnail_fileext = $thumbnail_filename[count($thumbnail_filename) - 1];
                        $thumbnail_name = time(). '1.'.$thumbnail_fileext; 
                        move_uploaded_file($_FILES["thumbnail_image"]["tmp_name"],$this->markrtingPath .$thumbnail_name);
                        
                    }
                    else
                    {
                        $thumbnail_name = ""; 
                    }
                   
                    
                    $table = 'marketing_ads';// Pass table name
                    $columns = array(
                                    'u_id' => $fields['u_id'],
                                    'country_id' => $fields['country'],
                                    'ur_id' => $fields['user_type'],
                                    'ad_type' => $fields['ad_type'],
                                    'path' => $file_name,
                                    'thumbnail_image' => $thumbnail_name,
                                    'ad_category_id' => $fields['ad_category'],
                                    'inserted_date' => date('Y-m-d H:i:s'),
                                    'expire_date' => date('Y-m-d H:i:s', strtotime('+1 day', strtotime(date('Y-m-d h:i:s')))),
                                   );
                    $response = $wb->addData($table,$columns);
                    $ad_id = $response['lastid'];
                    if($response['status'] == '1')
                    {    

                        //process for payment
                        $user_query = "SELECT * FROM user_master WHERE u_id = ".$fields['u_id'];
                        $user_detail = $wb->getRowDetail($user_query);
                        //faturah payment gateway
                       
                        $name = $user_detail['fullname'];
                        $email = $user_detail['email'];
                        $mobile = $user_detail['mobile'];
                        $service_name = 'Advertisement';

                        $post_string = '<?xml version="1.0" encoding="utf-8"?>
                        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                          <soap:Body>
                            <PaymentRequest xmlns="http://tempuri.org/">
                              <req>
                                <CustomerDC>
                                  <Name>'.$name.'</Name>
                                  <Email>'.$email.'</Email>
                                  <Mobile>'.$mobile.'</Mobile>
                                  <Gender>string</Gender>
                                  <DOB>string</DOB>
                                  <civil_id>string</civil_id>
                                  <Area>string</Area>
                                  <Block>string</Block>
                                  <Street>string</Street>
                                  <Avenue>string</Avenue>
                                  <Building>string</Building>
                                  <Floor>string</Floor>
                                  <Apartment>string</Apartment>
                                </CustomerDC>
                                <MerchantDC>
                                  <merchant_code>00000000</merchant_code>
                                  <merchant_username>testapi@myfatoorah.com</merchant_username>
                                  <merchant_password>XXXXX</merchant_password>
                                  <merchant_ReferenceID>00000</merchant_ReferenceID>
                                  <ReturnURL>http://solutiontrackers.com/dev-a/zerototwo/demo.php?success</ReturnURL>
                                  <merchant_error_url>http://solutiontrackers.com/dev-a/zerototwo/demo.php?error</merchant_error_url>
                                </MerchantDC>
                                <lstProductDC>
                                  <ProductDC>
                                  <product_name>'.$service_name.'</product_name>
                                  <unitPrice>'.$fields['amount'].'</unitPrice>
                                  <qty>1</qty>
                                  </ProductDC>
                                </lstProductDC>
                                <totalDC>
                                  <subtotal>'.$fields['amount'].'</subtotal>
                                </totalDC>
                                <paymentModeDC>
                                  <paymentMode>BOTH</paymentMode>
                                </paymentModeDC>
                                <paymentCurrencyDC>
                                  <paymentCurrrency>KWD</paymentCurrrency>
                                </paymentCurrencyDC>
                              </req>
                            </PaymentRequest>
                          </soap:Body>
                        </soap:Envelope>';
                        
                        $url = 'https://test.myfatoorah.com/pg/PayGatewayServiceV2.asmx';
                        $user = ''; 
                        $password = '';
                        $soap_do = curl_init();
                        curl_setopt($soap_do, CURLOPT_URL,$url );
                        curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
                        curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
                        curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
                        curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
                        curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
                        curl_setopt($soap_do, CURLOPT_POST, true );
                        curl_setopt($soap_do, CURLOPT_POSTFIELDS, $post_string);
                        curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length:
                        '.strlen($post_string) ));
                        curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
                        curl_setopt($soap_do, CURLOPT_HTTPHEADER, array(
                        'Content-type: text/xml'
                        ));
                        $result = curl_exec($soap_do);

                        $err = curl_error($soap_do);
                        // print_r($result);

                        $soap = simplexml_load_string($result);
                        $soap->registerXPathNamespace('ns1', 'http://tempuri.org/');
                        $test = (string) $soap->xpath('//ns1:PaymentRequestResponse/ns1:PaymentRequestResult/ns1:paymentURL[1]')[0]; 

                        $response = array('status'=>true, 'data' => array('message' => 'ad uploaded successfully', 'ad_id' => $ad_id, 'url' => $test));
                    }
                    else
                    {
                        $response = array('status'=>false, 'data' => array('message' => 'something wents wrong'));
                    }
        } 
        else
        {
            $response = array('status'=>false, 'data' => array('message' => 'Please upload Video or Image'));
        }
        
    }
    else
    {
        $response = array('status'=>false, 'data' => array('message' => 'some data is missing'));
    }
    header('Content-type: application/json');
    echo json_encode($response);
    die;
}

Help me. i am stuck in this problem from 10-15 days.