In Imagick I am trying to do the following:
When accessing .php already begins the download of .jpg, I do not even need a preview just the multiple download because this .pdf in question will always have two pages, with that fopen I can download only the first page, for some reason it does not download the second page.
pdf_file = './programming/'.$date.'.pdf';
$mpdf->Output($pdf_file);
$img = new imagick();
$img->setResolution(200,200);
$img->readImage($pdf_file);
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->setImageFormat('jpg');
$img->setImageCompressionQuality(85);
$pages = $img->getNumberImages();
if ( $pages ) {
foreach($img as $i => $img_file){
$img_file->writeImage('./programming/('.$i.') '.$date.'.jpg');
$img_path = './programming/('.$i.') '.$date.'.jpg';
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="('.$i.') '.$date.'.jpg"');
header("Content-Length: " . filesize($img_path));
$fp = fopen($img_path, "rb");
fpassthru($fp);
fclose($fp);
}
} else {
echo '<h2>Not Found.</h2>';
}
$img->clear();
$img->destroy();
If anyone can guide me, I may have missed something!
Thank you in advance for your attention!
Note: The images that represent page 1 and 2 of the .pdf are already saved in ./programming/ folder, the code works well in that case, then the foreach did what I expected, created two images and stored them but only download one with fopen
Related
I am trying to download files from server and it works fine for pdf files, I am wondering is there a way to download any type of files such as doc, zip,.. etc.
My code:
<?php
$doc = $sqlite->readDoc($documentId);
if ($doc != null) {
header("Content-Type:" . $doc['mime_type']);
header('Content-disposition: attachment; filename="something"');
print $doc['doc'];
} else {
echo 'Error occured while downloading the file';
}
?>
You'll want to identify the file - maybe using this function:
http://php.net/manual/en/function.mime-content-type.php
and then you can create a switch to indicate the content-type and appropriate disposition. There's a decent example here: PHP - send file to user
A simple function that can be used to download any file formate form path.
function DownloadFile($strFilePath)
{
$strContents = file_get_contents(realpath($strFilePath));
if (strpos($strFilePath,"\\") > -1 )
$strFileName = substr($strFilePath,strrpos($strFilePath,"\\")+1);
else
$strFileName = substr($strFilePath,strrpos($strFilePath,"/")+1);
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"${strFileName}\"");
echo $strContents;
}
Usage Example
$file = $_GET['file_id'];
$path = "../uploads/".$file; // change the path to fit your websites document structure
DownloadFile($path);
I am trying to add functionality to my website where users can download multiple image files via a single .zip folder. Currently I have this code executing but when i open the downloaded zip file it extracts another zip file my-archive (3) 2.zip.cpgz and every time I try to open it, it extracts yet another zip file.
Here is my basic code using php native zip feature.
$image1 = "http://cdn.screenrant.com/wp-content/uploads/Darth-Vader-voiced-by-Arnold-Schwarzenegger.jpg";
$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";
$files = array($image1, $image2);
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
EDIT
I am trying hard to get the provided answer to work. I just tried the most recent edit and got this error
]
You should download external files and then archive them.
$image1 = "http://cdn.screenrant.com/wp-content/uploads/Darth-Vader-voiced-by-Arnold-Schwarzenegger.jpg";
$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";
$files = array($image1, $image2);
$tmpFile = tempnam('/tmp', '');
$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
// download file
$fileContent = file_get_contents($file);
$zip->addFromString(basename($file), $fileContent);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=file.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
In example above I used file_get_contents function, so please enable allow_url_fopen or use curl to download the files.
Hi guys above code worked perfectly. Images download worked only on live server (only if we use https or http). But it is not worked in local (if we use https or http)..
If you use local follow this -> Only change below lines.
For local use below img's
$image1 = "C:/Users/User/Pictures/Saved Pictures/chandamama.jpg";
$image2 = "C:/Users/User/Pictures/Saved Pictures/beautifull.jpg";
Do not use below img's for local.
$image1 = "http://cdn.screenrant.com/wp-content/uploads/Darth-Vader-voiced-by-Arnold-Schwarzenegger.jpg";
$image2 = "http://cdn.screenrant.com/wp-content/uploads/Star-Wars-Logo-Art.jpg";
if (file_exists($path)) {
$fileContent = id3_getImage($path);
$fileMime = id3_getMimeOfImage($path); // is set to image/jpeg
if ($fileMime != "") {
header('Content-Type: '.$fileMime);
print($fileContent);
die;
}
}
So the above code does not work in the browser, however when I make a image with image
$img = imagecreatefromstring($fileContent);
imagejpeg($img, 'test.jpg');die;
the image is created and I can view it on my computer. So I have no clue what I am doing wrong :(
*Could it be a setting in the apache conf? I honestly have no clue on this one
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($img);
You need send header before. But if you want to create the image and show, you need create, and read this for the browser.
readfile($filename);
You can read the man here:
http://php.net/manual/en/function.readfile.php
I use smartReadFile.php from Google Groups to display any type of file.
https://jplayer.googlegroups.com/attach/f308294ddea52f6c/smartReadFile.php?view=1&part=4
It's really smart. =)
<?php
require "smartReadFile.php";
$dir = 'images/';
$filename = 'someimage' . '.jpg';
$location = $dir.$filename;
smartReadFile($location, $filename);
?>
This one is working for me when reading from DB
$image = imagecreatefromstring([path/DBfiels]);
header('content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
I am having problems with jPlayer. Audio is playing fine in the latest version of Safari on Lion but I cannot get it to work in Safari 5.0.6.
I have the audio located outside of httpdocs for security, so have implemented a script to download the audio.
function playSong(id,title){
$("#jquery_jplayer").jPlayer("setMedia", { mp3: "/download.php?id=" + id });
$("#jp_container .track-name").text(title);
$("#jquery_jplayer").jPlayer("play", 0);
}
Download.php currently looks like this, and is a solution I found from another user on stackoverflow.
// array of support file types for download script and there mimetype
$mimeTypes = array(
'doc' => 'application/msword',
'pdf' => 'application/pdf',
'mp3' => 'audio/mpeg'
);
// set the file here (best of using a $_GET[])
$file = "../song_files/mp3/$_GET[id].mp3";
// gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
$fsize = filesize($file);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('Accept-Ranges: bytes');
header('Content-Range: bytes');
header("Content-Length: ".$fsize);
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
// closes file after whe have finished reading it
fclose($fp);
I understand that this may be something to do with the headers I am sending and the older version of Safari isn't processing them correctly, however, I can successfully download the file if I browse to download.php?id=1.
Any idea how I can see what jPlayer is doing, I.E. returning an error such as cannot find file? If somebody can point me in the right direction that would be appreciated.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP list all files in directory
ive got a cron job running creating .xls files every 24hrs, they are creating them in a directory on my site called www.mysite.com/sheets
what i want to do is make a webpage where i can go to and see all the .xls files in that directory and download the ones i was instead of having to download them using an ftp client.
Is there a name for this sort of thing ? what would i write it in ? i was thinking i could do it in php by echoing the folders contents, would that work ?
cheers
You could activate directory listing within your webserver, or you can use a function like glob("*.xls") to list the files and echo their path on the webserver or you can send a file to the browser like this:
<?php
$filename = "path/to/your/file.xls";
$real_filename = "file.xls";
header('Content-Transfer-Encoding: none');
header('Content-Type: application/octet-stream; name="' . $real_filename . '"');
header('Content-Disposition: attachment; filename=' . $real_filename);
$size = #filesize($filename);
if ($size > 0) {
header('Content-length: '.$size);
} else {
header('Content-length: '.#strlen(#file_get_contents($filename)));
}
readfile($filename);
exit;
?>
If you want to list a directory use this:
$handle = opendir("./");
while (false !== ($file = readdir($handle))) {
$fileinfo = pathinfo($file);
if (strtolower($fileinfo[extension]) == "xls") {
echo $file;
}
}
closedir($handle);
Or this:
foreach (glob("*.[xX][lL][sS]") as $filename) {
echo $filename;
}
Use glob() plus some filtering for xls files.
glob("*.xls")
Potentially useful resources (in order of preference):
Manual
http://php.net/filesystemiterator
http://php.net/glob
http://php.net/scandir
http://php.net/opendir
http://php.net/function.dir