Download a PowerPoint just created in PHP

2.8k views Asked by At

I am trying to add the PHPPowerPoint to one of my tools. I add all the file that PHPPowerPoint needs and write the download link to the right page but when I try to download it, it say to me that I don't have the permit to access to the file. I tried to change the permit manually but nothing change, also beacuse everytime PHP create a new file with the default permit. I tried to use chmod on it but nothing change. I tried also chgrp and chown to change the owner (that is "daemon").

It's weird because when I use it out from the tool, with only the code to create the PP file everything works also with this permit.

The tool where I want to add the PP file download was coded with codeigniter.

3

There are 3 answers

1
Otto On

you can use the download helper

The Download Helper lets you download data to your desktop.

Loading this Helper

This helper is loaded using the following code:

$this->load->helper('download');

The following functions are available:

force_download('filename', 'data')

Generates server headers which force data to be downloaded to your desktop. Useful with file downloads. The first parameter is the name you want the downloaded file to be named, the second parameter is the file data. Example:

$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name, $data); If you want to download an existing file from your server you'll need to read the file into a string:

$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);
1
Sachin Endait On

I done the functionality for downloading .pdf file as it is in core php, Here is a sample code for it

$path = "path of your file"; 
$fullPath = $path.$_GET['fnm'];

if ($fd = fopen ($fullPath, "r")) {
   $fsize = filesize($fullPath);
   $path_parts = pathinfo($fullPath);
   $ext = strtolower($path_parts["extension"]);
   switch ($ext) {
      case "pdf":
         header("Content-type: application/pdf"); // add here more headers for diff. extensions
         header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
         break;
    default;
         header("Content-type: application/octet-stream");
         header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
   }
   header("Content-length: $fsize");
   header("Cache-control: private"); //use this to open files directly
   while(!feof($fd)) {
       $buffer = fread($fd, 2048);
       echo $buffer;
   }
}
fclose ($fd);
exit;

Just change file extension pdf to ppt(wherever pdf extension occurs).

3
amphetamachine On

Here's how to write a PHPPowerPoint object to the browser and have the user download it. It doesn't ever write to the file system, so there's no need for write permissions.

$ppp = new PHPPowerPoint;
// create the powerpoint, adding slides, content, etc.
// ...
// done

// set up the writer
$pppwriter = PHPPowerPoint_IOFactory::createWriter($ppp, 'PowerPoint2007');
// tell the browser a powerpoint is coming
header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation; charset=binary');

// make sure it downloads as $filename
header("Content-Disposition: attachment; filename={$filename}");

// output the powerpoint file data
$pppwriter->save('php://output');