fwrite or file_get_contents not copying full file - php

The file in the file path contains no JavaScript; it's just plain HTML or PHP scripts. They are also local. I'm trying to convert any file in the main path to UTF-8 before it is stored as a link in a menu. I have no control of the files that are uploaded except that they are either PHP or HTML.
An example is that the file size of one file is changed from 6,808 to 572 with the following:
$filesize = filesize($filepath);
$filecontent = file_get_contents($filepath,FALSE,NULL,0,$filesize);
$utf8content = iconv(mb_detect_encoding($filecontent, mb_detect_order(),
true), "UTF-8", $filecontent);
$fp = fopen($filepath, 'w');
fwrite($fp, $utf8content);
fclose($fp);

Related

file_put_contents uploads 6KB zip which is damaged

I am receiving zip url with Ajax to my php script. If I directly put that url into browser tab then it downloads file successfully but If I use this into my php script then file_put_contents generates 6KB file but the actual file size is 48KB.
Also when I try to extract, it shows error that file is damaged.
Here is my php code:
$url = "http://localhost/web/?_task=mail&_action=get&_mbox=INBOX&_uid=4&_token=LimZJJeCew7mbTJm2qEN29Vpd3YT2m7t&_part=1&_download=1";
$content = file_get_contents($url);
$name = 'mark.zip';
file_put_contents($name, $content);

readfile('filename.exe') and decode base64?

I'm using php in order to stream a file as a download. A part of this uses readfile:
<?php
// headers etc...
// ...
readfile('file.exe');
?>
This is a binary file, but for various reasons (that are not relevant to this question) I need to store this file as base64 or similar.
How do I stream a file with readfile that is stored encoded with base64?
I guess there are many ways that lead to success here, but I'm looking for the best & most convenient.
Using stream filters should help:
$inFile = 'file.exe';
$outFile = 'php://output';
$inHandle = fopen($inFile, 'r');
$outHandle = fopen($outFile, 'w');
stream_filter_append($inHandle, 'convert.base64-decode');
stream_copy_to_stream($inHandle , $outHandle);
fclose($inHandle );
fclose($outHandle);
If the file is not too big you can read it using file_get_contents and then decode it with base64_decode:
<?php
$content = file_get_contents('file.exe');
echo base64_decode($content);

Create sitemap.xml.gz with PHP

I have a problem with compressing sitemap.xml.gz. I use this code:
// Name of the file we're compressing
$file = '/path/'.'sitemap-'.$i.'.xml';
// Name of the gz file we're creating
$gzfile = 'sitemap-'.$i.'.xml'.".gz";
// Open the gz file (w9 is the highest compression)
$fp = gzopen ($gzfile, 'w9');
// Compress the file
gzwrite ($fp, file_get_contents($file));
// Close the gz file and we're done
gzclose($fp);
When I click on link example: www.example.com/sitemap/sitemap-1.xml.gz and when I open it, I get this: IMAGE. If I wrote only www.example.com/sitemap/sitemap-1.gz I get correct file.

PHP get raw data of PDF to POST using CURL

I have been tryign to get the raw contents of a PDF file using PHP to POST to an API using CURL. I have tried using file_get_contents (though the resulting file is not readable) and I have tried
$filename = "/files/example.pdf";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Then I post contents and again the file is not readable by a PDF reader
Any idea's?
Thanks

characters added when reading temp file

when i do a direct input feild as text the input comes through fine but when i change the input feild type to "FILE" and then read the temp contents stored on the server it adds extra characters that are "unknown"
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
fclose($fp);
echo $content;
Its most probably to do with secuirty but how do i change the content so it reads normal? In firefox it comes up with square boxes for most characters with the letters:
Here is an example:
Its meant to say:
"Dirty Rocker" but instead comes out like this: "D�i�r�t�y� �R�o�c�k�e�r"
Hope you can help!
Set data type in form to multipart/form-data. Set encoding of output page to utf-8 through header and meta tag.
Check real contents of uploaded file. Compair original file and its copy on the server. Try to upload file manually (though ftp, for example) and read it by php.

Categories