Mimi Chan Mimi Chan - 6 months ago 41
PHP Question

Browser crash after var_dump a variable

When i take content of a picture i try to dump it like that:

$filename = '(900).jpg';
$im = file_get_contents($filename);

when the picture is under 1mb everything works, but if it is more than 1mb browser crash can you tell me why is that a browser ishue or some limitation of file_get_contents() function?


The only limitation of file_get_contents might be the memory which is allowed for PHP to use. And the default is about 128 MB.

It is a browser "issue" if you want to call it that. Outputting so much debug information to the browser is not a good idea as you can see. Additionally there is no benefit in viewing a binary file as text.

If you want to find out if the variable is set, you can use functions to check the size of the (binary) string e.g. mb_strlen().

A better way would be this

$filename = '(900).jpg';
$im = file_get_contents($filename);
// check if the file could be loaded
if ($im !== false) {
    // start your processing

But this does not check what kind of file you have loaded into the string. If you must store the file into the database - which is considered very evil - you can either store the binary string into a BLOB type row or encode the binary string with base64_encode() and store it into a text type. Both of this solutions are also not recommended!

If you need to store image information into the database, you should think about using references to the files - e.g. the file path. Your primary objective is to secure that the database information and the filesystem information is always synchronized.