Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cosminc's avatar

Laravel adds weird characters at the beginning of a saved .html file

Hello,

Using Laravel 5.7 here and I'm facing a weird issue: I'm uploading some .html files via the application that I'm building and I want to process those files later using DOMDocument.

The upload is pretty basic, I'm saving the file using:

$request->source_file->store('files');

But when I read the uploaded file later on I get this two weird characters at the beginning:

��<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

I'm retrieving the file like so:

Storage::get('files/' . $this->source_file');

Also tried with the following code, but got the same result:

$source = file_get_contents(storage_path() . '/app/files/' . $this->source_file);

Any idea how those characters get there?

I'm also uploading CSV files and there's no such issue there.

Thanks.

0 likes
4 replies
manojo123's avatar

file_get_contents sometimes does not work as expected with special characters.

Can you please try this workaround?

$source = mb_convert_encoding($source, 'HTML-ENTITIES', "UTF-8");
1 like
cosminc's avatar

@manojow I tried with something similar yesterday but it didn't solve the issue. Using your code exactly produces a tag at the beginning, probably after converting those two special characters:

<feff><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
manojo123's avatar

This is the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' Hex. Its probably a white space that is getting generated for some reason and you probably need to check your source to find why.

But if this is the only unwanted substring in your string you can use this hack to get rid off

$source = str_replace("<feff>", "", mb_convert_encoding($source, 'HTML-ENTITIES', "UTF-8"));
1 like
Snapey's avatar

Its likely that the character exists in the original file. There are not many applications that will show it.

We have a problem with these in links we get from Solarwinds - it seems to add these after so many characters in a long URL to allow it to wrap on screen.

I wrote a little tool that you could paste the content of the original file into to see if the code is added outside or inside your application

https://dj.microlise.net/zeroban

1 like

Please or to participate in this conversation.