Why data is changed after uploading xml file PHP - php

i have the code to upload xml file using dom.
if (!$dom->load($folderName . "/" . $fvalue)) {
echo "<br>".$fvalue." file is corrupt or Tags are not closed properly ";
}
it uploads the file BUT after uploading, there are 3 whitespaces in the start of the file before any xml Tag. Problem is we download uploaded file and upload again. but with whitespaces in the start of the file, dom dont allow to upload the file as it is corrupted.(i check the validation of xml using xmlvalidation.com) which returns error because there are spaces at the start. if i remove space manually than no error.
How i can upload the file without adding spaces at the start ?
I checked to remove spaces from file etc. but i dont want to do that. i want to upload the file without changing.

problem was with downloading the file. after download there was some spaces in the start of the file. code was
header("Content-type: application/force-download; charset=utf-8");
header("Content-type: application/force-download");
header("Content-disposition: attachment;filename=" . $downFileName);
readfile($filename);
i changed to this and it's working fine now.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downFileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);

Related

Sending a .docx file to the Browser with PHP

I am struggling to send a Word .docx file to the browser. The Word file opens fine, if I create a link to the Word file, it also opens fine.
However when using this code:
// output the file to the browser
if ($fileToOutput) {
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
//header('Content-Length: ' . filesize($fileToOutput)); // this is causing the file download to fail
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($fileToOutput) . "\"");
ob_clean();
flush();
readfile($fileToOutput);
exit;
}
I get the following:
Followed by the dreaded "Word experienced an error trying to open the file.":
Like I said, the Word file opens fine, and downlaods from a link fine - I only get these errors when trying to output the Word file with the above PHP code.
Where is this PHP code going wrong?

blank page when using php readfile() in wordpress template

I have a form which the user fills out with their details & serial number, the software installer is then downloaded, named as their input. e.g. serial.exe
Once the form is submitted I check the file exists and then attempt the file download:
$directory = get_template_directory();
$file = $directory . '/filename.exe';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$Serial.'.exe"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
This causes the loading to freeze, nothing below this code is loaded but i can't see any errors & no file is generated.
If i change the file to a .png the code works.
I've also copied the same code to a php page outside of wordpress, the same code runs & downloads the file as desired.
Is it possible that Wordpress is somehow blocking the readfile() request because its an .exe?
EDIT:
It appears to be a problem with the size of the file, rather than the type of file.
A small .exe file has worked & is downloaded correctly.

Retrieving a file outside root, avoid cutting off filename which contain spaces

I have uploaded files to a folder outside my webroot folder and use some php code to retrieve the file after being authenticated by database. My code below seems to work, i.e. retrieves the file, the only issue I have is, the pdfs uploaded contain filenames with spaces and the code below seems to only retrieve the first word before the space.
The example filenames are
My Trip To London.pdf
Wildlife Photography Part 1.pdf
Apart from going through the database and phuycical files and substituting each file to contain an underscore instead of a space, is there a way I can alter the code below so it retrieves the whole filename?
I thought I could substitute the filename with with %20 if it detected a space but that doesn't work.
/* Done some processing above to retrieve the $filename for the pdf */
$file = 'h:'.$filename;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
the pdfs uploaded contain filenames with spaces and the code below seems to only retrieve the first word before the space.
Quote the name:
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
or in clearer form:
header(sprintf('Content-Disposition: attachment; filename="%s"', basename($file)));

readfile downloads corrupt files

I am facing a problem when i am trying to download a certain file from my localhost. Everytime i download a file, either a .docx, a .pdf or a .png it is always corrupt. I have spend multiple hours on finding a solution but nothing seems to work.
This is the script im running :
$file_download="loonadministratie/{$loon_id}/{$file}";
if (file_exists($file_download)) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream/loonadministratie/{$loon_id}/");
header('Content-Disposition: attachment; filename='.basename($file_download));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_download));
readfile($file_download);
exit;
}
Thanks in forehand
Try
<?php
if (ob_get_level()) ob_end_clean();
?>
It may be the issue

PHP Force download not working

Here is my problem. I am trying to download a file using header. Here is my code:
$content_type = mime_content_type('uploads/MyBBIntegrator_v1.3.1.zip');
$file = 'uploads/MyBBIntegrator_v1.3.1.zip';
header("Cache-Control: public");
header('Content-type: application/octet-stream');
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="MyBBIntegrator_v1.3.1.zip');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile('uploads/MyBBIntegrator_v1.3.1.zip');
However, the only result of this is that the page displays the contents of the file (it is a text file) or a string of strange symbols if the file is image/zip/exe etc
What should I do to solve this problem?
First of all, mime_content_type() is deprecated, you should try another method to fetch the MIME value.
I have checked your code and it works fine on my server, and it works fine for me. You should check for INI directives which might block the download. Try a fresh install server.
Also, there should be no output generated by the script before the snippet you put into your question.

Categories