1

I've uploaded a file using angular2 according to the method told in this question's answer. now I don't know how to receive this file in php(backend), can anybody tell how to receive it???

Community
  • 1
  • 1
Majid NWL
  • 317
  • 2
  • 3
  • 11
  • 1
    This looks like a very basic question to me. What have you tried to figure it out yourself? – Stephan Vierkant Feb 22 '17 at 11:49
  • in php I checked $_FILES and $_POST object, both haven't any data in it, I just want to know, in which object this file will be and how can I get this file. – Majid NWL Feb 22 '17 at 13:23

1 Answers1

2
<?php 

header("Access-Control-Allow-Origin: *");

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  echo json_encode(array('status' => false));
  exit;
}

$path = 'uploads/';

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

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

  if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
    echo json_encode(array(
      'status'        => true,
      'originalName'  => $originalName,
      'generatedName' => $generatedName
    ));
  }
}
else {
  echo json_encode(
    array('status' => false, 'msg' => 'No file uploaded.')
  );
  exit;
}

From here

hugsbrugs
  • 3,501
  • 2
  • 29
  • 36