0

When i do json_encode() for uploaded file data,value of $_FILE['tmp_name'] is passed with double back slashes,and i assume thats reason why function move_uploaded_file() doesn`t work.Is there any solution for that? Here is code example

UI:

    $scope.upload = function (file) {

    Upload.upload({
         url: '../classes/uploadPhoto.php',
         data: {file: file, 'username': $scope.usernameFile}
    }).then(function (resp) {

        $scope.uploadedPhoto = resp.data;

    });
};

PHP:

   public function __construct(){

    $fileObject = new stdClass();

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

         $fileObject  ->  name  = $_FILES['file']['name'];
         $fileObject  ->  type  = $_FILES['file']['type'];
         $fileObject  ->  tmp   = $_FILES['file']['tmp_name'];
         $fileObject  ->  error = $_FILES['file']['error'];
         $fileObject  ->  size  = $_FILES['file']['size'];

         echo json_encode($fileObject);
  }
}

And final part:

    $target_path = 'C:/xampp/htdocs/PDP/admin/ui/img/'.$request -> imageName;
        move_uploaded_file($request -> tmPath, $target_path);

Everything working fine except move_uploaded_file().

Nebojsa Masic
  • 53
  • 1
  • 8
  • Can you provide en example? Also, why do you _encode_ tmp_name? – ksimka Nov 27 '15 at 10:15
  • Ok, so you `echo` json encoded array just for debugging, double slashes are simply escaping, it's ok. Then, you didn't show what `$request` is. You have to look into your logs to get exact error that you get during `move_uploaded_file`. I bet on file permissions issue. – ksimka Nov 27 '15 at 10:28
  • 2
    Possible duplicate of [json\_encode() escaping forward slashes](http://stackoverflow.com/questions/10210338/json-encode-escaping-forward-slashes) – Supamiu Nov 27 '15 at 10:54

1 Answers1

0

The double slashes is escaping the slash character. If you need to just have one slash. You could use str_replace().

$_file['tmp_name'] = str_replace('//', '/', $_file['tmp_name']);
$_file['tmp_name'] = str_replace('\\', '\', $_file['tmp_name']);

When uploading content it is recommended that you use add enctype="multipart/form-data" to the form element.

Check file and folder permissions. As that can also be the culprit.

References:

Daniel Samson
  • 718
  • 4
  • 18