I've written some PHP code to generate an ICS file for a user to download and add to their own calendar. It works fine on desktop Safari (macOS 12.6), and it also works fine if I airdrop the downloaded ICS file to my iPhone. However, if I try to download the ICS file directly on mobile safari (iOS 16.1.2) I get the message "Safari cannot download this file".
Do I need to sign the file somehow? or is the PHP 'readfile' command not allowed on iOS?
Thanks!
`<?php
// Get the event details from the form data
$result_sched = json_decode($_POST['result_sched'], true);
$returnedCountSched = json_decode($_POST['returnedCountSched'], true);
// Open the ICS file for writing
$icsFile = fopen("events.ics", "w");
// Write the ICS file header
fwrite($icsFile, "BEGIN:VCALENDAR\n");
fwrite($icsFile, "PRODID:-//Your Company//Product//Language\n");
fwrite($icsFile, "VERSION:2.0\n");
fwrite($icsFile, "METHOD:PUBLISH\n");
// Iterate through the loop and write each event to the ICS file
for ($j = 0; $j < $returnedCountSched; $j++) {
// Get the date and time strings from the result_sched array
$date_string = $result_sched['response']['data'][$j]['fieldData']['service_date'];
$start_time_string = $result_sched['response']['data'][$j]['fieldData']['service_start_time'];
$end_time_string = $result_sched['response']['data'][$j]['fieldData']['service_end_time'];
// set time zone to los angeles
date_default_timezone_set('America/Los_Angeles');
// Format the date and time strings
$date = date("Ymd", strtotime($date_string));
$start_time = date("His", strtotime($start_time_string));
$end_time = date("His", strtotime($end_time_string));
// Combine the date and time strings and convert to a timestamp
$start_timestamp = strtotime("$date $start_time");
$end_timestamp = strtotime("$date $end_time");
// Format the timestamp as an ICS-formatted DTSTART string
$dtstart = gmdate("Ymd\THis\Z", $start_timestamp);
$dtend = gmdate("Ymd\THis\Z", $end_timestamp);
$location = $result_sched['response']['data'][$j]['fieldData']['service_venue_name'];
$summary = $result_sched['response']['data'][$j]['fieldData']['service_title'];
$description = $result_sched['response']['data'][$j]['fieldData']['rehearsal_order_summary'];
// Write the event to the ICS file
fwrite($icsFile, "BEGIN:VEVENT\n");
fwrite($icsFile, "DTSTART:$dtstart\n");
fwrite($icsFile, "DTEND:$dtend\n");
fwrite($icsFile, "LOCATION:$location\n");
fwrite($icsFile, "SUMMARY:$summary\n");
fwrite($icsFile, "DESCRIPTION:$description\n");
fwrite($icsFile, "END:VEVENT\n");
}
fwrite($icsFile, "END:VCALENDAR\n");
fclose($icsFile);
// Output the ICS file as a download
header('Content-Description: File Transfer');
header('Content-Type: text/calendar; charset=utf-8');
//header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=events.ics');
// header('Content-Transfer-Encoding: binary');
// header('Expires: 0');
// header('Cache-Control: must-revalidate');
// header('Pragma: public');
// Output the ICS file
readfile('events.ics');
?>`
I've also tried this in Chrome on the desktop and I get 0 byte download files, whereas I get the expected ICS file when download on desktop Safari. I must be doing something wrong!
I feel like it has something to do with how I'm setting the headers, but I'm at a loss.
Related
Using PHP_XLSXWriter, if I write an .xlsx file to the server, download and then open it in Excel all works well :-
include_once("xlsxwriter.class.php");
$filename = "example.xlsx";
$rows = array(
array('year','month','amount'),
array('2003','1','220'),
array('2003','2','153.5'),
);
$writer = new XLSXWriter();
$writer->writeSheet($rows);
$writer->writeToFile("/path/to/file/$filename");
However if I output the file direct from the browser, opening the downloded file gives me the error message "Excel cannot open the file 'example.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
include_once("xlsxwriter.class.php");
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: base64');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('year','month','amount'),
array('2003','1','220'),
array('2003','2','153.5'),
);
$writer = new XLSXWriter();
$writer->writeSheet($rows);
$writer->writeToStdOut();
This is running on a Ubuntu 20 server running Apache2 and PHP 7.2
Any ideas what is going wrong?
Many thanks
I found the problem....I had a single space outside the tags in the xlsxwriter.class.php file. Infuriating.
On button click I'm downloading a text file via PHP in AJAX.
Here is my code. I only get an alert of file contents instead of downloading the file.
<?php
$cpy1 = $_POST['cpy1'];
file_put_contents("download.txt",$cpy1);
$file ="download_link.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Get a date and timestamp
$today = date("F j, Y, g:i a");
$time = time();
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=download_link.txt");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
?>
Help please.
I got the desired OP by below code. Was created a hidden download btn (hyperlink of text file) and on success made a click on it
success: function(data){
swal("Download List", data, "success");
// alert(data);
$('#getfile')[0].click();
}
This gave popup with text file download
I guess it just because of your third part application (e.g. IDM). Please disable/uninstall it first then your code should work as you want.
<?php
// // $bin = pack("S", 65535);
// // $ray = unpack("S", $bin);
// $ray = pack("H*hex", $data);
// print_r($ray);die();
// echo "UNSIGNED SHORT VAL = ", $ray[1], "\n";
$file = file_get_contents("C:\\Users\\qwerty\Downloads\\image001.jpg", true);
// $file = file_get_contents("C:\\Users\\qwerty\\Downloads\\request.pdf", true);
$data = '0x'.unpack('H*hex', $file)['hex'];
header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: Binary");
// header('Content-Disposition: attachment; filename="request.pdf');
header('Content-Disposition: attachment; filename="image001.jpg');
header('Cache-Control: must-revalidate');
header('Content-Length: 80685');
echo $data;
?>
I want am trying to create File from unpacking the same file. I am recreating this from my other function that unpacks a file stores the data in database and recreate the file again using the data. The above is my sample demo for the bigger function. What it does is unpack file content then echo again. Problem is the downloaded file (image or pdf) cant be opened. Any idea is appreciated
I think I saw the problem when I opened the file in notepad it has a newline before the image data how can I make sure there will be no new line in before the data?
Zip damaged after set file size header
I have this code to limit download speed in php :
function readfile_chunked($filename, $retbytes = TRUE) {
$download_rate = 85; global $filename ;
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: filename=file.zip');
// flush content
flush();
// open file stream
$file = fopen($filename, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
usleep(200);
}
}
When i set header for content length like this:
header('Content-Length: '.filesize($filename));
and open the downloaded file show me an alert that file is damaged . Not just the zip files i try on jpg file and its the same .
BUT WHEN I REMOVE THE FILE SIZE HEADER THE FILE OR IMAGE OPEN WITHOUT ANY ERROR
I #solved the problem by adding ob_clean() in the begin of function. I was trying to solve it for 3 months This need a party ;)
function party(forVisitors){
Drink();
Play();
HaveFun();
GoHome();
}
i have developed reporting system for one website.i have developed script such that my client can export sales data in XLS file with all detail, every thing was working fine, but now i noticed that every generating xls file has blank row at the top,i have not make any change in code, before few days it was not happening. how to remove that blank line from the top
i am using following code to generate xls file:
$str = "No\tOrderId\tFirst Name\tLast Name\tOrder Date\tTime\tAddress\tCity\tState\tZip\tCountry\tPhone\tEmail\tProductId\tProductName\tProductQty\t";
$output = array();
$output[] = $str;
while($row = $res->fetch())
{
$datetime = new DateTime($row['DateTime']);
$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');
$Country = getCountryName($row['Country']);
$stroutput= $cnt."\t".$row['OrderId']."\t".stripslashes($row['FirstName'])."\t".stripslashes($row['LastName'])."\t".$date."\t".$time."\t".$row['Address1']."\t".$row['City']."\t".$row['State']."\t".$row['Zip']."\t".$Country."\t".$row['Phone']."\t".$row['Email']."\t".$row['ProductId']."\t".$row['ProductName']."\t".$row['ProductQty']."\t";
$output[]=$stroutput;
$cnt++;
}
$filename="CustomReport_".$date33."_".$date55."_".date("mdy-His");
$data = implode("\n", $output);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename.".xls;");
header("Content-Type: application/ms-excel");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";