Why codeigniter force download() incorrectly transforms the string of the archive to be downloaded?

Hi Experts!

Could you explain why codeigniter code:

 force_download($arquivo, "Ebook");

Open in new window

Tranforms the string:
$arquivo = "http://localhost/codeagro/arquivos/cesans/e_pub/Livro_melhor_alimento_ebook_2015.epub"

And produces this download (putting  "_ "  incorrectly in the string), accordingly with the picture
What must be done to correct it ?

Thanks in advance!
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Maybe you can only download from the host that you're on, try removing the 'http://localhost/' part of the string.
$arquivo = "codeagro/arquivos/cesans/e_pub Livro_melhor_alimento_ebook_2015.epub"

Open in new window

Julian HansenCommented:
force_download($name, $data);

The first parameter is what you want the document to be called when it is saved on the other end

$arquivo = "http://localhost/codeagro/arquivos/cesans/e_pub/Livro_melhor_alimento_ebook_2015.epub"

Open in new window

Is full of illegal characters that are not valid in most filesystems - so the helper is "helping" you by replacing those non-valid characters with '_' so that when you save the file the OS does not choke on the file name.

$arquivo = 'sample_filename.epub';

Open in new window

Having said that - your $data element does not look right - your download (if I read the docs correctly) is going to comprise a file (with that long and ugly modified name) with the content Ebook. If you want the file to be the actual EBook yhou would need to do this
$data = file_get_contents('codeagro/arquivos/cesans/e_pub/Livro_melhor_alimento_ebook_2015.epub');
$name = 'sample_ebook.epub';
force_download($filename, $data);

Open in new window

I suspect you read the docs
force_download('filename', 'data')
To mean the first parameter was the path to the file - when in fact the second parameter is for the data - while the first parameter is for the name you want the file to be saved as on the other side.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Very, very good assistanece!

Problem solved.

Thank you very much, Julian!
Julian HansenCommented:
You are most welcome Eduardo,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.