Refer to Can an ASP.NET MVC controller return an Image?
, the answer suggest to return image file from controller in C#.
My question is:
Can I do the same thing or similar in PHP? What I want is to hide PDF path from URL.
Thanks
I think you are trying to hide the real local path of your pdf file using php. If this is the case, you can use something like this:
<?php
$localfilename = 'my_local_path/my_file.pdf';
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename='download.pdf'");
readfile($localfilename);
?>
I hope this helps you. Greetings!
Related
please I need help I'm trying to create a SEPA XML file so I downloaded the sepa-xml with coenter image description heremposer in my project then I make the same code in the documentation to test if work or not so when I enter image description hereadded the code and generate the file I get an empty file. but when I dump the variable I get a result .
I get this page
I referred here. Looks like you need to download the generated xml code.
[Git link][1].
Code will be like this.
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
echo $directDebit->asXML();
exit;
In this case the generated code will be saved as newfile.xml and download the file automatically. Hope it solves your problem
[1]: https://github.com/php-sepa-xml/php-sepa-xml/blob/master/doc/direct_debit.md
I am new to PHP development. I am creating excel.xls using the mentioned header information, whereas it is saving as xls at default download folder. Actually, when I click export link, this test.xls file should be saved to www/data/ folder instead of downloading it.
How can I give path for new location to save file using PHP dynamically?
code as follows:
$file = "test.xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=$file");
code end
Please help if possible.
Thanks in advance.
Best regards,
Balraj
You can use file_put_contents in PHP.
$file = "path/test.xls";
$data = "Keep your data into this variable";
file_put_contents($file,$data);
file_put_contents is wrapper for fopen,fwrite,fclose.For more information check here
We are trying to create a webpage in laravel where people are going to be able upload their codefiles to our server, so that other users can watch the code and download it in codefiles if they like it. We however can't figure out the best way to make this happen.
I tried to just let php get a file and echo out the content. this worked well fot html and css, but with php nothing got displayed what so ever. someone mentioned using eval(), however i've read that it is a really bad idea to do so. Another idea would be to stash the code in a database and fetch it from there, which we have tried before, but it sort of over complicated, and avoiding to do so would be prefereable, and instead go directly to i file.
So my question is, do anybody have an idea that might work safely, both for us and our server and for the users.
Something like this:
<?php
// read Codefile
$TheCode = file_get_contents($codefile);
// Print it...
echo htmlentities($TheCode);
?>
Save the php code in a flat file like one with a .dat extension.
then read the file.
$toechp = file(static.dat);
echo $toecho;
You can allow .dat files to be downloaded on browser using headers.
<?php
$file = "http://example.com/static.dat";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile ($file);
?>
and you are done.
test.php code:
$fileloc = 'audio.mp3';
header('Content-type: audio/mpeg');
header("Content-disposition: inline; filename=$filename");
header('Content-Length:'.filesize($fileloc));
readfile($fileloc);
html code:
<iframe src="test.php"></iframe>
here is the updated code that is working.
thanks hafichuk
if anyone knows a way to do this with
<embed> or <object> rather than <iframe>
please share your code or send me a link.
Try changing your content type to audio/mpeg3.
Update 1:
Since it's downloading the file as an attachment, you could also try changing the content-disposition to header('Content-Disposition: inline; filename=audio.mp3');
The reference I found for this was for swf files in the embed tag, however I would imagine that you should do the same.
I know this is old, but I found a simple way to accomplish this, yet it might not be the prettiest, but
here it is!
test.php:
<?php
if (isset($_GET['file'])) {
echo file_get_contents('music.mp3');
exit;
}
<audio src="test.php?file" controls></audio>
You simply extract the file using file_get_contents and output it, using the php file as the sound file, pretty neat :P
I have a php script that generates a pdf report. When we go to save the pdf document, the filename that Acrobat suggests is report_pdf, since the php script is named report_pdf.php. I would like to dynamically name the pdf file, so I don't have to type the appropriate name for the report each time that I save it.
Asking on a news group, someone suggested this, where filename="July Report.pdf" is the intended name of the report
<?
header('Content-Type: application/pdf');
header('Content-disposition: filename="July Report.pdf"');
But it doesn't work. Am I doing it wrong, or will this work at all? Is this a job for mod_rewrite?
So I've tried both
header('Content-disposition: inline; filename="July Report.pdf"');
and
header('Content-disposition: attachment; filename="July Report.pdf"');
( not at the same time ) and neither work for me. Is this a problem with my web host? For this url, here's my code:
<?
header('Content-disposition: inline; filename="July Report.pdf"');
// requires the R&OS pdf class
require_once('class.ezpdf.php');
require_once('class.pdf.php');
// make a new pdf object
$pdf = new Cpdf();
// select the font
$pdf->selectFont('./fonts/Helvetica');
$pdf->addText(30,400,30,'Hello World');
$pdf->stream();
?>
Try:
header('Content-Disposition: attachment; filename="July Report.pdf"');
or
header('Content-Disposition: inline; filename="July Report.pdf"');
Another option would be to use the $_SERVER['PATH_INFO'] to pass your "July Report.pdf" - an example link might be:
<a href="report_pdf.php/July%20Report.pdf?month=07">
That file should default to saving as "July Report.pdf" - and should behave exactly like your old php script did, just change the code that produces the link to the pdf.
Should be:
header('Content-disposition: attachment;filename="July Report.pdf"');
Based on https://github.com/rospdf/pdf-php/raw/master/readme.pdf (Page 19)
The stream-method accepts an array as parameter:
stream([array options])
Used for output, this will set the required headers and output the pdf code.
The options array can be used to set a number of things about the output:
'Content-Disposition'=>'filename' sets the filename, ...
This code works well for me
$ezOutput = $pdf->ezStream(array("Content-Disposition"=>"YourFileName.pdf"));
In my case I use ezStream() but I think stream() should give the same result.
For the name shown on the title tab in the browser
I had the same problem, then i found that it's metadata missing inside my .pdf.
I used a tools ("debenu PDF tools") for edit pdf property like author, title, etc...
I just change title from empty field to what title I want, upload the new pdf and now, with same code, same script the browser show the right name!
For the name when u ask to save document
is what u specify in header filename=
Did you try to remove spaces from file name using hyphens? So, I think its name must be like this; filename=July-Report.pdf