file_put_contents uploads 6KB zip which is damaged - php

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);

Related

Word document Base64 encoded string downloads as zip file

I have a base64 encoded string converted with the php function below.
public function convert_image_to_base64($file_path){
$mime = get_mime_by_extension($file_path);
$data = file_get_contents($file_path);
return 'data:'. $mime . ';base64,' . base64_encode($data);
}
I want to download the file in a browser with the below html
Download
When I click, the link downloads a zip file.
This is the start of the resulting base64 string
data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQABgAIAAAAI
I want to download the exact .docx or .doc file uploaded
I searched on stackoverflow and other forums but none of them address this exact problem. I also don't want to decode the file before submitting the html.
So the problem was the file extension of the downloaded file was not added in the base64 file downloaded. I solved the problem by forcing the name of the downloaded file using the html below from this link. Reference here
Download

Codeigniter force_download fails on large Zip files

When using force_download to download a zip file my code works for a zip file that is 268Mb (31 MP3 files) but not for a zip file that is 287Mb (32 MP3 files), the difference being 1 extra MP3 file added to the zip. The download attempts to start and appears as though it keeps starting over and over a couple of times and shows as failed with Chrome indicating that the zip file is incomplete. Windows reports the zip file which is only 61Kb is invalid when trying to open it.
The zip file gets created and MP3 files added to it by another area of code.
I have increased the memory_limit up to 1024M but its no different.
Below is the code I want working:
$this->load->helper("download");
$lastbasket = "uniquefilename.zip";
$zipdlpath = base_url()."uploads/zipped/".$lastbasket;
$fileContent = file_get_contents($zipdlpath);
force_download($lastbasket, $fileContent);
I have also tried using the following code:
$this->load->helper("download");
$lastbasket = "uniquefilename.zip";
$zipdlpath = FCPATH."uploads/zipped/".$lastbasket;
force_download($zipdlpath, NULL);
Providing a direct link to the zip file works fine (so I know the issue isnt with the zip file itself) but the force_download function in the controller appears to have an issue with larger files or is there a setting I am missing somewhere that is forcing a limit somehow?
PHP 7.1.33
CodeIgniter 3.1.9
Try to increase memory limit by adding this code:
ini_set('memory_limit','1024M');
increase memory limit and use fopen, fread
try this
$this->load->helper("download");
$lastbasket = "uniquefilename.zip";
$zipdlpath = FCPATH."uploads/zipped/".$lastbasket;
force_download($zipdlpath, NULL);
if (is_file($zipdlpath))
{
$chunkSize = 1024 * 1024;
$handle = fopen($zipdlpath, 'rb');
while (!feof($handle))
{
$buffer = fread($handle, $chunkSize);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);
exit;
}
I've tried with the following custom download helper, may it will work for you.
Ref Link - https://github.com/bcit-ci/CodeIgniter/wiki/Download-helper-for-large-files

read contents of zipped files through file_get_contents in codeigniter in .srt format and upload to server

I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP

How to extract file name and extension from a decoded Base64 string in php

I'm uploading files to my PHP webservice by encoding them to Base64 and sending them via Json.
I'm decoding the data on the server using this code: $file_content = base64_decode($file,true);
Now I want to extract file name and extension from the $file_content variable.
How can I do that?
UPDATE: here is the code I used to decode the file:
include '../conn.php';
include '../functions.php';
$entityBody = file_get_contents('php://input');
$decodedJson = json_decode($entityBody);
$user_email = $decodedJson->{'email'};
$user_token = $decodedJson->{'token'};
$file = $decodedJson->{'file'}; //This is the encoded content of the file from JSON
$file_content = base64_decode($file,true);
if(!checkStudentAuthorizationOrHigher($user_email,$user_token,$conn))
{
die();
}
//now I want to get $file_content type (extension or MIME type) and
//file name from the $file_content
//I tried the below code but it only gets file path as input parameter and doesn't work this way
echo mime_content_type($file_content);
Only the file content is given to my PHP webservice through JSON (file name or extension is not sent to my service). If it helps, the client side of my application is both a C# Windows Forms app and an Android app.
I want to save the file on the server with the same file name as the client side. Also I have to check the file type to make sure it is allowed on the server. (the file can be .pdf , .jpg , .jpeg or .zip)
Now is there a way to extract file info from $file_content variable?
I used thw Intervention/image library https://github.com/Intervention/image
to deal with base64 encoded images. THis library got many other features as well.
$imageManager = new Intervention\Image\ImageManager();
$imageObject = $imageManager->make($base64EncodedImageSource);
$imageInfo = getimagesize($base64EncodedImageSource);
$this->mimeType = $imageInfo[2];
$extension = image_type_to_extension($this->mimeType);
$width = $imageInfo[0];
$height = $imageInfo[1];
$tempFilePath = '/path/to/file';
$imageObject->save($tempFilePath, 100);
Alternatively, if you have mime type of the file, you can use function image_type_to_extension to get file extension

How to download xml file through in php?

I created xml file in php. This file is successfully saved in my server directory.
Now, I want to download this file only through php, not javascript.
How can I save this file?
Try file_get_contents() and file_put_contents().
<?PHP
$xml = file_get_contents("http://yoursite.com/yourxml.xml"); // your file is in the string "$xml" now.
file_put_contents("/path/to/file/yourxml.xml", $xml); // now your xml file is saved.
?>

Categories