|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20400 Doing: base64_encode on JPG & GIF images. Error: The function ends prematurely.
Submitted: 2002-11-12 14:25 UTC Modified: 2002-11-12 15:45 UTC
From: dromar84 at msn dot com Assigned:
Status: Not a bug Package: *Graphics related
PHP Version: 4.2.1 OS: win32
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: dromar84 at msn dot com
New email:
PHP Version: OS:


 [2002-11-12 14:25 UTC] dromar84 at msn dot com
The following PHP script is stable on *NIX platforms but unstable on Win32 platorms.

$LineLength = 50;

$uploadfile = 'PATH TO IMAGE';
$uploadfile_name = 'PATH TO IMAGE';

$fd = fopen ($uploadfile, "r");
$size = filesize ($uploadfile);
$c = substr ("$uploadfile_name", strrpos ($uploadfile_name, ".") + 1 );
$cont = fread ($fd, $size);
fclose ($fd);
$encimg = base64_encode ($cont);
$imgcode = chunk_split ("$encimg", $LineLength, "'.'"); 
print $imgcode;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-12 14:30 UTC]
Please try using this CVS snapshot:
For Windows:
 [2002-11-12 14:31 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

On windows you need to open binary files with 'rb' not 'r'. 
If you still experience the problem even after changing the fopen flags try using the latest CVS snapshot.
 [2002-11-12 14:31 UTC] dromar84 at msn dot com
I intend to use this script to store mutiple image files in a database or in one compressed file. If anyone knows of a better way to accomplish this task, I would appriciate it if you could tell me.

p.s. I am aware that using base64_encode will not result in a compressed file.
 [2002-11-12 15:45 UTC] dromar84 at msn dot com
The problem is fixed by using 'rb'(read binary) instead of 'r'(read) inside of the fopen flag on Win32 platforms.

Just another 3-bit mistake by the script writer.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 14 05:01:28 2024 UTC