2

This is the basic file upload I am trying using angular and php. When I choose the file in UI, I can see the file details.

But, when I execute it I am getting empty array as response in network tab.

What might be the issue?

I have checked php.ini file and everything seems fine to me.

(I also tried to print using print_r ($_FILES); It gives empty array.)

I have attached Angular(.html, .ts) and .php file here:

<?php
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Headers: *");
        echo ($_POST['file']);
?>
    <form [formGroup]="myForm" (ngSubmit)="submit()" method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label for="file">File</label>
            <input 
                formControlName="file"
                id="file" 
                type="file" 
                class="form-control"
                (change)="onFileChange($event)">
        </div>
        <button class="btn btn-primary" type="submit">Submit</button>
    </form>
    myForm = new FormGroup({
      file: new FormControl('', [Validators.required]),
      fileSource: new FormControl('', [Validators.required])
    });
        
    constructor(private http: HttpClient) { }
      ngOnInit(): void {
    }
    
    get f(){
      return this.myForm.controls;
    }
       
    onFileChange(event) {
      if (event.target.files.length > 0) {
        const file = event.target.files[0];
        console.log('File', file);
        this.myForm.get('fileSource').setValue(file);
      }
    }

    submit(){
      const formData = new FormData();
      formData.append('file', this.myForm.get('fileSource').value);

      this.http.post('http://localhost/]api/document/SaveContact.php', formData)
        .subscribe(res => {
          console.log(res);
          // alert('Uploaded Successfully.');
        })
      }
    }
11
  • Tried to print using print_r ($_FILES); It gives empty array. Commented Nov 2 at 10:40
  • 2
    $_POST['file'] will never work for a file upload, it'll always be in $_FILES if it's there. Does the network tool show the file being successfully sent in the request payload? Commented Nov 2 at 13:38
  • 1
    [Just wonder] Are you sure there is a "]" in this URL ? http://localhost/]api/document/SaveContact.php Commented Nov 3 at 0:14
  • 2
    Check the network tab to see if a request was made and if it had the file in the payload. Also check your server logs and error files. Commented Nov 3 at 5:03
  • 2
    I think i found problem. this.myForm.get('fileSource').value is file path but FormData.append second parameter is actual value to send with request. use this.myForm.get('fileSource').files[0] as File or create FormData by const formData = new FormData(this.myForm) if this.myForm is HTMLFormElement. Commented Nov 4 at 10:07

1 Answer 1

-1

In your PHP code:
echo ($_POST['file']);

You're trying to read the uploaded file using $_POST['file'].

But uploaded files do not go into $_POST.

They go into the $_FILES superglobal array in PHP.

So, even if everything is correct on the Angular side, $_POST['file'] will always be empty.

Use the given PHP Code:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");

if (isset($_FILES['file'])) {
    $file = $_FILES['file'];
    echo json_encode([
        'name' => $file['name'],
        'type' => $file['type'],
        'size' => $file['size'],
        'tmp_name' => $file['tmp_name']
    ]);
} else {
    echo json_encode(['error' => 'No file uploaded', 'files' => $_FILES]);
}
?>

That will correctly show you what PHP receives.

Sign up to request clarification or add additional context in comments.

3 Comments

OP already stated that $_FILES is also empty, so it's unclear how this answer helps
probably LLM generated
Also why not just do echo json_encode($file); then you see the whole array entry - incuding the error code, which might be useful if something has gone wrong! Don't think this answer has been thought through. I agree it does smell like it might be AI slop - if not then it's a bit careless. Either way it's at best redundant, at worst not as helpful as it could be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.