0

I want to convert a image to Base64 and put in into my database. I know how to encode a image to Base64, but I dont know how to use a file from a user.

So the user can "upload" a file with <input type="file" /> But how can I approach that file without download the file and store it at my server?

So i encode the file actually localy on the user his/her computer

Thanks

Thom
  • 591
  • 4
  • 12
  • 30
  • In short: you can't. This is a security limitation built into the http servers. Each file uploaded that way will get saved to a temporary file, there is nothing you can do against that and no other way than accessing that temporary file to handle the file data. This is by purpose. – arkascha Apr 24 '14 at 06:17
  • Why do you want to encode on the _client_ side? Since the users probably are not willing to understand and do this you'd have to do it automatically on script level (javascript), but _why_? – arkascha Apr 24 '14 at 06:18
  • @arkascha That's not done by the web server, it's the php module that saves the uploaded files into a temporary path. – Ja͢ck Apr 24 '14 at 06:34
  • @Jack: the php module is, as its name "module" already says, executed inside the scope of the http servers processes. Think of the php module as a library, or better a plugin to the http server. So yes, the active instance "doing" things here _is_ the http server. – arkascha Apr 24 '14 at 06:37
  • @arkascha No, the active instance that does the work *is* the php module; the file upload gets read from its designated input stream; this is also how `is_uploaded_file()` knows which file was uploaded in the current request. – Ja͢ck Apr 24 '14 at 06:40
  • @Jack Sorry, but this is phrase splitting. I think you understood my point of view. – arkascha Apr 24 '14 at 06:45
  • @arkascha No, your point was that it's a security limitation built into the http servers and that's clearly not the case, though some servers may choose to cache the request body if it gets too big in order to save memory; that said, it's possible to pipe a file upload straight into MySQL without ever writing to an intermediate file. – Ja͢ck Apr 24 '14 at 06:48

3 Answers3

0

The BLOB datatype is best for storing files.

if you do not want to save the file to the database, but just read it, use:

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$content here has the file content

Community
  • 1
  • 1
Guns
  • 2,678
  • 2
  • 23
  • 51
  • The BLOB datatype is not suitable for this project. because I want to keep my database as small as possible (in storage). – Thom Apr 24 '14 at 06:24
  • 1
    @user3322384 Base64 encoded files take even more space; if you want to keep your database as small as possible, *don't* store images in it. – Ja͢ck Apr 24 '14 at 06:35
0

To encode an image to base64 you can use file_get_contents to read the image file:

$imageBinaryString = file_get_contents('/image/file/path.png');

http://us1.php.net/file_get_contents

Then base64 encode:

$base64_imageString = base64_encode($imageBinaryString);

You can then store in database in a column with type text or varchar.

rmcfrazier
  • 444
  • 4
  • 8
0
<input type="file" name="icon" />

Now try with below code.

 $base  =  $_REQUEST['icon'];
 $binary   =  base64_decode($base);
 header('Content-Type: bitmap; charset=utf-8');
 $file  = fopen('upload/imagename.png', 'wb');
 $imagename = 'path_to_image_/images.png';
 fwrite($file, $binary);
 fclose($file);
Mitul Shah
  • 1,556
  • 1
  • 12
  • 34