I have an <a href> link on a page that points to a video file (mp4). When the visitor clicks the link, the video file opens in the browser window.
If you want to download the file, you have to "right click", and then "save link as..."
I want that so that on a single click file will download (so that I don't have to right click).
edit: What's the problem here?
<?php
header('Content-Description: File Transfer');
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename=file');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
?>
Set the Content-Disposition header to attachment. For example:
Content-Disposition: attachment; filename=lolcats42.mp4
Related
I'm trying to insert image from Yii2 into exported Excel;
This is the code :
<?php
$logo = Html::img("#web/img/Logo.jpg", ['width' => '60px', 'height' => '50px', 'margin-top' => '-10px']);
$downloadedTime = $downloadTZ->format('Y-m-d');
$bulan = \Yii::t('app', $month);
$filename = 'Report'.'xls';
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: application/force-download");
header("refresh:0;url=../master/index");
echo $logo;
echo "Name\t";
echo "Class\t";
echo "Age\t";
echo "\r\n";
?>
if I echo $logo; the printed in browser, but in Excel it will print the code not the image, just like this
Why does this happen? and How do I can solve this?
Thanks
I do that with PHPExcel class and then calling PHPExcel_Worksheet_Drawing;
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('img/logo.png');
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
like this.
I have a table in MySQL database. It consists of many columns. The last column is BLOB column and it is storing pdf files. I want to call particular columns of the table. The BLOB column is the last one. I want the <td> elements to say Download and on click of the anchor tag I want the pdf to get downloaded. Somebody please help. The values of the Copy Column is going to be pdf files.
$result= mysql_query("SELECT Name, Type, No, Copy from table1 Join table2 where username='{$_SESSION['username']}' AND table1.UserID=table2.UserID") or die(mysql_error());
$fields_num = mysql_num_fields($result);
echo "<table class='table table-hover'>";
echo "<th>Name</th>";
echo "<th>Type</th>";
echo "<th>Number</th>";
echo "<th>Copy</th>";
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>".$row['Name']."</td><td>".$row['Type']."</td><td>".$row['No']."</td><td>"."<a href=''>Download</td></tr>";
}
}
echo "</table>";
Usually I save my files in a folder and just adding the path into a column in my database and to download I use the following PHP code.
I call it downloadPop_Up.php:
<?php
$data = $_REQUEST['data'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
//header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
readfile($data);
?>
I have a script like this which sends html emails. I am trying to include images into my html emails. Is this possible ? File has to use this function. I'm not looking for some quick one liner instead of my function because of file manipulations I have done in my script. When I send out emails I back a [X] instead of my image.
Email function of my script.
#!/bin/bash
Email()
{
export MAILTO="ADC#aol.com"
export CONTENT="file"
export SUBJECT="Report"
(
echo "Subject: $SUBJECT"
echo "To : $MAILTO"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat "$CONTENT"
) | /usr/sbin/sendmail $MAILTO
}
Email
File I am sending "file"
<html>
<body>
<img src="/home/admin/Afriendlycow.png">
</body>
</html>
I attempt to try just the file name instead of the path such as Afriendlycow.png but it does not work also.
You are not attaching any images. A proper HTML email with images is a multipart/related containing one text/html part and one or more image/* parts. They should have a Content-Id: header with a unique identifier as its value; the HTML refers to them using <img src="cid:identifier">.
I have a small website with several PDFs free for download. I use StatCounter to observe the number of page loads. It also shows me the number of my PDF downloads, but it considers only those downloads where a user clicks on the link from my website. But what's with the "external" access to the PDFs (e.g., directly from Google search)? How I can count those? Is there any possibility to use a tool such as StatCounter?
Thanks.
.htaccess (redirect *.pdf requests to download.php):
RewriteEngine On
RewriteRule \.pdf$ /download.php
download.php:
<?php
$url = $_SERVER['REQUEST_URI'];
if (!preg_match('/([a-z0-9_-]+)\.pdf$/', $url, $r) || !file_exists($r[1] . '.pdf')) {
header('HTTP/1.0 404 Not Found');
echo "File not found.";
exit(0);
}
$filename = $r[1] . '.pdf';
// [do you statistics here]
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$filename\"");
readfile($filename);
?>
You can use a log analyzer to check how many times a file was accessed. Ask you hosting provider if they provide access to access logs and a log analyzer software.
in php, it would be something like (untested):
$db = mysql_connect(...);
$file = $_GET['file'];
$allowed_files = {...}; // or check in database
if (in_array($file, $allowed_files) && file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
mysql_query('UPDATE files SET count = count + 1 WHERE file="' . $file . '"')
readfile($file);
exit;
} else {
/* issue a 404, or redirect to a not-found page */
}
You would have to create a way to capture the request from the server.
If you are using php, probably the best would be to use mod_rewrite.
If you are using .net, an HttpHandler.
You must handle the request, call statcounter and then send the pdf content to the user.
I am uploading files(any type) in MySql tables's blob field. Now I am able to get binary data from that field and when I print it, it shows binary data in firbug console. But I want to download that file as it was uploaded.
How can I convert this binary data into orignal file? How to do it in zend?
Thanks
You need to set the headers at minimum you need
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\";");
echo $data;
exit;
?>
Or preferablly
<?php
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header ( "Content-Type: $filedatatype" );
header("Content-Disposition: attachment; filename=\"".$FileObj->name."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$filesize);
echo $data;
exit;
?>