0

Given this one a few hours research and thought but I'm not getting anywhere.

I have managed to get an image file to upload through AJAX using a FormData object. When the file reaches the php code I am able to access its info such as 'name' and 'type' by using:

$_FILES['newFile']['name'];
$_FILES['newFile']['type'];

And so on, which means that the file must be uploading as intended, I just can't seem to save it to a file from there.

I have tried:

$file = file_get_contents($_FILES["newFile"]['tmp_name']);
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);

But then imagejpeg gives me the error "Expects paramater1 to be resource, string given." So I tried:

$file = imagecreatefromstring(file_get_contents($_FILES["newFile"]['tmp_name']));
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);

I now receive the error: /"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."

Can someone explain how I get the image from the array to a file on disk?

3 Answers 3

1

You have to use the move_uploaded_file function like this:

move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $pathfilename)
Sign up to request clarification or add additional context in comments.

2 Comments

I had tried this also, I get the same error: "Failed to open stream. No such file or directory." with an additional error: "Unable to move 'C://xampp/tmp/something.tmp' to '/img/uploads/image.jpg'" Could there be an issue with my output path?
Yes, it could be that you don't have permissions or some other error. But using the move_uploaded_file() function is the way. Stay on that track.
0

Use php function move_uploaded_file("$_FILES['newFile']['tmp_name']","Location_where_you_want_to_save");

Comments

0

Figured it out! Obviously "/img/uploads/" was the issue. PHP doesn't recognize a forward slash as the root directory! My fault!

move_uploaded_file($_FILES["newFile"]['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/uploads/' . $_FILES["newFile"]['name']);

Using $_SERVER['DOCUMENT_ROOT'] before the new file path worked perfectly.

Comments

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.