Is there a way to add a base64 image string to the $_FILES
array (the array that you get when you upload a file)? Basically, I want to fake a file upload from a base64 string. The base64 string comes from an email attachment. I don’t wish to use file_get_contents()
, because I want to save the image in a database as a binary image. I don’t need to upload it, as such, but I do need it be a part of the $_FILES
array.

- 10,148
- 7
- 57
- 107

- 937
- 1
- 10
- 29
-
Where do you have the base64 string? on the client side in javascript? – jcsanyi Jul 23 '13 at 06:33
-
Please provide a bit more context - who/what is uploading the file? – jcsanyi Jul 23 '13 at 06:34
-
The base64 string is coming from an email attachment. So PHP. @jcsanyi – Marais Rossouw Jul 23 '13 at 06:34
-
So you've already got it in PHP - where are you uploading it to? A different server? – jcsanyi Jul 23 '13 at 06:35
-
Not from any server, its just a base64 string, all local, but i want to 'upload' it, so i need it in the files array. @jcsanyi – Marais Rossouw Jul 23 '13 at 06:36
2 Answers
Uploading is a way of sending the contents of a file from a client (usually a web browser) to a server. It sounds like you've already got the data you need on the server (in PHP), so you don't need to upload it.
You can use
base64_decode()
to convert it from base64 to the straight file contents.If you need it as a file on your server, you can use something like
file_put_contents()
to save it as a new file.If you're trying to do something else with the uploaded file in the
$_FILE
array, please provide more details. Whatever it is, you don't need to actually upload the file.
The $_FILES
array doesn't actually contain the contents of the file - it contains a filename where the contents have been saved.
If you must use the $_FILES
array (in order to re-use an existing class, for example), you'll need to use file_put_contents()
to save a new temporary file, and then manually add an entry the the $_FILES
array pointing to that temporary file (Just like modifying any other array). Don't forget to delete your temporary file when you're done - this is normally handled automatically by PHP, but if you create the file manually, you'll have to delete it manually too.
I'd highly recommend refactoring or providing an alternate interface to your class instead though. You shouldn't have to pretend that the file was uploaded just to save it to the database.

- 8,133
- 2
- 29
- 52
-
Like i said, i dont want it to file_put_contents... Because i need it to "upload" to a database, and not be in base64. So i went decode so its in binary/image. I want it to be in the $_FILES array. – Marais Rossouw Jul 23 '13 at 06:46
-
Neither the `$_FILES` array, nor uploading has anything to do with a database. The `$_FILES` array is just a way for you to access the contents of a file that was sent to you from the browser. You've already got the contents of the file, so you don't need the `$_FILES` array. – jcsanyi Jul 23 '13 at 06:49
-
Ive built a php class that add's files to the database from the $_FILES array, it creates a hash, and so forth. I know i dont need it, i just want it in the `$_FILES` array – Marais Rossouw Jul 23 '13 at 06:51
index.php
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
/* Simple */
function previewImage( image, preview, string )
{
var fileImage = image.files[0];
var preview = document.querySelector( preview );
var reader = new FileReader();
reader.addEventListener( "load", function() {
preview.style.height = "100";
preview.title = fileImage.name;
// convert image file to base64 string
preview.src = this.result;
/* --- */
document.querySelector( string ).value = this.result;
}, false );
if ( fileImage )
{
reader.readAsDataURL( fileImage );
}
}
document.querySelector( "#imageID" ).addEventListener( "change", function() {
previewImage( this, "#imagePreviewID", "#imageStringID" );
} )
/* Simple || */
<form method="post" action="process.php" >
Title: <input type="text" name="title" /><br />
File Upload: <input type="file" name="image" id="imageID" /><br />
Preview: <img src="#" id="imagePreviewID" /><br />
String base64: <textarea name="imageString" id="imageStringID" rows="10" cols="50" readonly="readonly" ></textarea>
<hr />
<input type="submit" name="submit" value="Submit" />
</form>
process.php
<?php
$data = array(
"post" => $_POST,
"files" => $_FILES
);
echo "<pre>";
// echo json_encode($data);
var_dump($data);
?>
<img src="<?= $_POST['imageString']; ?>" />

- 3,216
- 26
- 20