-1

I have this line:

move_uploaded_file($_FILES["img"]["tmp_name"], "uploads/" . "$img_name");

That used to work when I received the image through a direct POST (without JSON.stringify the content). Now I'm sending the ajax with the image inside of an array stringified, so I'm getting the contents of the array in the PHP file in this way:

$unstringified = json_decode(file_get_contents("php://input"), true);

And then I use:

$title = $unstringified["title"];
$content = $unstringified["content"];
$img = $unstringified["img"];

The problem is that now the move_uploaded_file stop working (seems to be no error but the image doesn't appear saved in the folder anymore). I tried some options like these, but didn't work.

move_uploaded_file($unstringified["img"]["tmp_name"], "uploads/" . "$img_name");
move_uploaded_file($_FILES[$unstringified["img"]]["tmp_name"], "uploads/" . "$img_name");

Any idea to solve this? Should I "convert" the image in any format until putting it in the array I will stringify? or I have to get the image in another way in the PHP file?

Thanks a lot in advance. Leandro.

1 Answers1

0

I wanted to pass the image inside a JSON because always that I've tried to pass and image with other string variables with formdata, I've always got an error, even playing with all posible variables of dataType, contentType , Cache, etc, and researching through lot of answers here. I couldn't find a solution to use form data to pass all togheter.

What I found I can do is to pass all inside within a JSON object without getting errors, so that's why I wanted to receive the image in PHP under this format and save it in this way.

But after trying lot of ways, again, and couldn't find a solution for this, I gave up, and use 2 separate sends, one json object with all the strings inside, and one formdata with only the image (if I don't add another variables like strings, there's no problem).

I'm sure it's not the best way, but it's what I can do.

Thanks all!