What is the best way to retrieve the parameter of ng2-file-upload using endback?

I am attempting to retrieve a parameter using PHP in order to save it into a folder. However, my code is not working as expected. Here is the code snippet:

Using the Ionic framework

this.uploader.onBeforeUploadItem = (item: any) => {
    this.uploader.options.additionalParameter = {
    folder : this.herb_id <---- the name of my parameter folder
};

In the backend (PHP)

<?php 
header('Access-Control-Allow-Origin', '*');
header('Access-Control-Allow-Methods', 'POST');
header('Access-Control-Allow-Headers', 'Content-Type, Content-Range, Content-Disposition, Content-Description,X-Requested-With');
header('Access-Control-Allow-Credentials', true);

if (isset($_FILES['file'])) {

   /------------- folder name --------------------------//
   $path = 'uploads/';
   $path .=$_FILES['options']['additionalParameter']['folder']; <---- folder name
   $path .= '/';

  $originalName = $_FILES['file']['name'];
  $ext = '.'.pathinfo($originalName, PATHINFO_EXTENSION);
  $generatedName = md5($_FILES['file']['tmp_name').$ext;
  //$filePath = $path.$generatedName;
  $filePath = $path.$generatedName;

  if (!is_writable($path)) {
    echo json_encode(array(
      'status' => false,
      'msg'    => 'Destination directory not writable.'
    ));
    exit;
  }

Answer №1

Here is the solution I came up with for the problem:

If you are working with Ionic, you can use this code snippet:

this.fileField.uploader.onBuildItemForm = (fileItem, form) => {
      form.append('folder', String(this.herb_id));
      return { fileItem, form };
    };

For PHP, you can utilize the following code:

//------------- Creating folder name --------------------------//
   $path = 'uploads/';
   $path .=$_POST['folder']; <---- folder name
   $path .= '/';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Remove Pictures -> codeigniter

I have modified my inquiry in order to receive a more targeted response. Apologies if my previous question was unclear: I am facing an issue with deleting a row from the database while also removing linked images from separate locations. I am unable to su ...

The encryption method of PHP XOR falls short

I have experimented with various solutions to implement XOR encoding on an uploaded file, but it consistently fails. The issue lies in the fact that the unencrypted file uploads smoothly to the MySQL database: $fp = fopen($tempPath, 'r'); $ ...

AfterViewInit is not being impacted by property binding in the view

I'm currently integrating Mapbox into my Angular project and I am facing a challenge in displaying information from my component.ts file in the corresponding component.html. My approach involves using mat-vertical-stepper, where each step represents ...

The session was left intact as Chrome closed without destroying it

// Begin session_start(); $_SESSION['a']=1; // Second page session_start(); echo $_SESSION['a']; Despite trying to use session_set_cookie_params(0); or session_cache_expire(1);, I still couldn't destroy the session when Chrome clo ...

How to shuffle elements in an array using PHP

Let's say I have an array like this: $a=array("a","b","c","d","e"); I am looking to randomize the items in my array. For example, I want my array to look like this: $a=array("d","a","b","e","c"); I tried using shuffle but it did not give me the de ...

Exploring the Implementation of Date/Time Service in Angular 2

My current project involves setting up a simple service in Angular 2. The objective is to have the current date and time displayed when a user clicks a button. However, upon implementing the code provided below, I encountered an error: Error during evalua ...

Is there a way to dynamically load a file on scroll using JavaScript specifically on the element <ngx-monaco-diff-editor>?

I've been attempting this task for over a week now in Angular without success. Would someone be able to provide guidance? The onContainerScroll() function isn't being triggered, and I'm considering using JavaScript instead. How can I achiev ...

Utilizing the Session for Optimal Results

I have implemented a login feature that stores the user's ID in $_SESSION['login_user']. Upon successful authentication, the user is directed to their home page. I want to retrieve the value from the session and use it in a query to fetch a ...

How to replicate a WordPress menu bar

Embarking on my coding journey, I've completed courses in HTML, PHP, CSS, JavaScript, and MySQL. Now, I've decided to dive into hands-on learning by creating a Wordpress child theme. My current challenge is figuring out how to overlay two differ ...

"Utilize FPDF to neatly wrap text within a cell in a

Is there a simple way to wrap text within cells of a table using FPDF? I've come across some solutions, but they seem quite hardcoded. Since I'm fetching data from my database, I need a more dynamic approach. Here's an image depicting the i ...

Having trouble passing a jQuery variable containing a string value to PHP through the jQuery AJAX function?

Below is the jQuery function code snippet: function processMessage() { if (textValue != "") { messageString='<div class="alert-box round"><p class="text-left">' + username + ':' + textValue + '</p>< ...

Angular Drag and Drop with Multiple Items (ng2-dragula)

Currently searching for a drag and drop library that can handle multiple drag capabilities. Unfortunately, none have been found specifically for Angular 2+. While ng2-dragula does meet some of our requirements, it falls short when it comes to supporting ...

What is the most efficient method for validating an exception in Protractor?

In recent code reviews within our team, a discussion arose about writing tests that assert on expected exceptions. While this is correct, the method used involved a try/catch block, which some believe may not be the most performant approach. This raises th ...

Encountered Typescript issue when utilizing typed forms in Angular

Below is the interface I am working with: export interface ILoginDto { email: string; password: string; } Here is a snippet of the relevant code from the component: import { FormBuilder, FormGroup, Validators } from '@angular/forms'; export ...

Requesting data through AngularJS2 HTTP call results in the return of complete PHP code

I am currently facing an issue where my http AJAX call to a local PHP file is echoing the complete PHP code. Since I am new to AngularJS2, I would appreciate some help. Here is the snippet of my AngularJS2 code: makeHttpCall():Promise<any>{ ret ...

Using PHP variables in CSS styling

Let me explain the situation. In my index.php file, I have a class called 'rectangles' which includes properties for color and size. I defined a variable named $rectangle($rectangle=new rectangles('red',25);). Additionally, I include ...

"Encountering a 500 internal server error while working with

Currently, I have set up an ajax functionality with two different contents retrieved from separate files. Files: my_site_id_one.php: <div class="id_one_content"> <?php echo do_shortcode('[shortcode_one]'); ?> </div> my_ ...

Utilizing a server for seamless communication between a mobile device and a website

Exploring a simple setup idea here: Imagine having a mobile app with a page that contains 4 lines of content (utilizing phonegap for development). The plan is to have a web page where data for those 4 lines can be inputted. Once the information is submitt ...

Error: Unable to inject UrlHandlingStrategy as no provider was found

I recently upgraded my application to Angular 14 and encountered a challenging error. Despite configuring RouterModule for Root and child with lazy loading, I am now facing a circular dependency issue related to the Router. I'm unsure how to further d ...

Using a template reference variable as an @Input property for another component

Version 5.0.1 of Angular In one of my components I have the following template: <div #content>some content</div> <some-component [content]="content"></some-component> I am trying to pass the reference of the #content variable to ...