Empty line in PHP file (CodeIgniter) download

On my site based on CodeIgniter (member management system), it is possible to create files with direct debit. They are downloaded by setting the headers as described here: http://www.richnetapps.com/the-right-way-to-handle-file-downloads-in-php/ . However, for some reason, an empty string is always output before my own output. I tried replacing all the lines of the newline in the line that I was returning, without success. The output is an XML file, and my bank does not accept the file as valid XML because of this empty string.

I have already found posts that say that this is most likely due to the closure of PHP tags in the files before the current file. This may be the reason, but several third-party libraries are loading, and manually deleting all closing PHP tags in each file is canceled if you still want to keep the ability to update your libraries. Smarty seems to love these closing tags.

Direct access to the file itself is also not an option, because CodeIgniter does not allow this by default, and because this method poses a rather serious security problem (public files with bank account data are big no-no),

Therefore, I come to you: do you know another possible solution to this problem?

Edit: This is the code used to download.

function incasso_archive($creditor, $date, $time, $extension) { $date = str_replace("_", "-", $date); $fn = $this->incasso->incasso_file($creditor, $date, $time, $extension); $contents = file_get_contents($fn); $name = "Incasso $date.$extension"; header('Content-Type: application/octet-stream'); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename="'.$name.'"'); echo $contents; } 
+6
source share
4 answers

If the $contents in your function does not have a new line, try using the output buffer functions.

At the beginning of the file, call ob_start(); before including any other files ob_start(); . Then, inside your function, before echo $contents; add ob_end_clean(); . This ensures that none of the results from other scenarios are submitted.

+3
source

The Vi (or vim) editor leaves an extra line of new line at the end of the edited file. If you used vi for any included file, this should be a problem.

The following answer describes removing newlines at the end of files in a simple way:

How to delete a new line if this is the last character in the file?

This command can be used together with linux find to remove all trailing lines in php files.

 for i in `find /path/to/project -name *.php` do perl -i -pe 'chomp if eof' $i done 
0
source

I didn’t understand your problem exactly, but since your $ content is a string, you can simply use

 echo trim($content) 

To delete new lines

0
source

This works for me.

 header('Content-type: text/xml'); print_r ($content); 
0
source

All Articles