I am attempting to write to an image file from a blob.
if($_POST['logoFilename'] != 'undefined'){
$logoFile = fopen($_POST['logoFilename'], 'w') or die ("Cannot create ".$_POST['logoFilename']);
fwrite($logoFile, $_POST['logoImage']);
fclose($logoFile);
}
In the previous code snippet, $_POST['logoImage']
is a BLOB. The file is correctly written to the root directory, however the file cannot be opened. In ubuntu 11.04 I receive the following error:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61).
The BLOB does correctly display if I create an img and set its src=blob
Included below is the first snippet of the BLOB:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAgGBgcGBQgHBwcJCQ
Your "Blob" is really a Data URI:
Since you only want the decoded data part, you have to do
But since PHP natively supports data:// streams, you can also do (thanks @NikiC)
If the above doesnt work, you can try with GDlib:
If it's really a blob, you might want to trying using mode "wb" as the second parameter to your fopen() call.
EDIT: You might also consider just using file_put_contents(), which is binary-safe.
If it's a file upload control,$_POST
won't contain the information. You're looking for handling file uploads with$_FILES
. (And more specifically, move_uploaded_file)Given the new update, try the following:
This function will save data uri to file:
Usage in your case: