Reputation: 187
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?
Upvotes: 0
Views: 1286
Reputation: 187
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.
Upvotes: 0
Reputation: 2115
You have to use the move_uploaded_file function like this:
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $pathfilename)
Upvotes: 1
Reputation: 19
Use php function move_uploaded_file("$_FILES['newFile']['tmp_name']","Location_where_you_want_to_save");
Upvotes: 0