I am new to PHP. I have a software downloads website. What I want is to
automatically add "-mysite.com" with every download filename. So that the actual
filename remain the same i.e "somesoftware.exe" but whenever someone download it, it should
be automatically renamed and downloaded with the filename "somesoftware-
mysitename.com.exe" to their computer. Here is the code for download link div on my site.
<!--===========================Download Div===============================-->
<div id="downlink-container">
<a href="http://www.mysitename.com/downloads/jetaudio16.0.0.435-mysitename.com.exe" id="downloadlink" style="visibility:visible">
Download Jet Audio
</a>
</div>
<!--===========================Download Div====================================-->
Can I use PHP to automatically add "-mysitename.com" with the downloaded file name
"somesoftware.exe" so that the actual filename on server folder remain the same(i.e.
somesoftware.exe) but whenever someone download it, it automatically become "somesoftware-
mysitename.com.exe". I tried using PHP variables for this but I can't get it done. Please
Help!
You set the following header:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
Where downloaded.pdf is your filename. Its just a matter of getting the extention and adding "- thenoblesite.com" to the original name.
Once user starts downloading a file, it's over - you have no control over it, no PHP, no anything. So the idea is to either rename the source file (which you don't want) or to actually stream the file using a different name. See it here.
Related
I'm outputting a PDF via PHP with the following code. $file is an object that contains data pertaining to the file being displayed.
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$file->name.'"');
#readfile($file->ServerPath());
My issue is that when I go to download the file in Chrome it will occasionally try to save the page instead of the PDF.
For example, say the URL that is displaying the file is mywebsite.com/file?file_id=1234. Most of the time it will try to save the file correctly as "file_name.pdf". However, sometimes chrome will try to save the file as "file" with no extension. This seems to happen randomly.
If it makes any difference the page displaying the file is being opened in a new tab. The issue happens regardless of whether I redirect via PHP or Javascript.
I really need to resolve this issue, as these PDFs will be accessible by users.
Thanks in advance.
First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?
Any thoughts guys/gals?
You just set the application type and file name in the headers, like so:
// This points to the file in question, note that it doesn't
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';
// You can make this whatever you like, it doesn't have to
// be the same as the file name on the disk! This is the name of the file your end
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf';
$data = file_get_contents($filePathOnDisk);
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;
See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.
Tested
You can use the following which will prompt the user to save the (PDF) file on their computer.
Notice the different file names.
One is the file that will be uploaded/prompted to the user download_example.pdf, while the other is the file without an extension as set in readfile('example');
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>
This seems like it should be simple. I have a set of files I have to store outside of the webroot and have an access script to call them. I also need to sometimes tell a PDF that must be called via this proxy script to open at a specific page. Releveant part of the script below:
header('Content-type: application/pdf');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="'.$file_name.'"');
readfile($file);
$file_name is just the basename() of the file, and $file is the path to the file, with #page=2, or #page=10, or whatever appended to it. If I remove the hashtag portion, the script works fine and the PDF opens with no errors. When the hashtag portion is there, all the programs tell me the PDF has been corrupted and can't be open.
I can't seem to find anything on here or Google as to what I need to do. Do I need to set an additional header to simulate the hash tag? Use exec() to call some command line code instead of using readfile()?
Any insight would be greatly appreciated.
You have to append the #page=2 (or whatever page you want to open) to the URL in the browser, not the filename in the proxy-script.
You currently try to open a file myFile.pdf#page=2 from the filesystem that does not exist as the filename is myFile.pdf
The feature to open a pdf-file on a specific page on the other hand is implemented in the browser or it's PDF-plugin. Therefore the information which page to open has to be given to the browser via the URL. So you should call your proxyscript like this: http://example.com/proxy.php?myFile.php#page=2
Update:
If you want to download the file and open it at a specific page every time the file is opened from the local file-system of the user, you will have to edit (or recreate) the PDF-File.
i want to implement code so that when user will download that file, name of the file should be changed
as example
$uploaddir="files/userid/";
$filename=rand(1000,9999).time().rand(1000,9999);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
suppose using this code file is uploaded it will be stored on server as name like this
23451232325654.pdf
but for user he/she will have logical name for it Like learn_php.php
when user want to download this file he/she will have this link to download
www.example.com/files/userid/23451232325654.pdf
but this file not stored on user's pc when downloaded as 23451232325654.pdf but i want to store it as their logical name as shown above
learn.php
You can do this no problem. You just need a download script that will first send the correct header. In this case, the header should be something like:
header('Content-Disposition: attachment; filename="learn_php.pdf"');
See example 1 in the php docs.
So instead of linking directly to the file (for example: http://website.com/content/129312.pdf), you would link to your download script (for example: http://website.com/download.php?file=129312.pdf).
And download would first send the headers, then the file contents.
Obligatory note about security: Using the filename directly from $_GET without sanitizing it opens up a huge security issue. If you do it this way you NEED to sanitize it.
I have a script which uploads files into an online directory and stores the file details in a database. The files when stored are renamed to the id of the entry in the database. Whenever a user requests a download, a simple SQL statement retrieves the file details from the database, the contents of the file are read from the database, and the file is prompted for download. The following is my code:
$one_file = $FILE_OBJECT->get($_GET['id']); // this is an object which just grabs the file details from the database
header("Content-type: ".$one_file['type']); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$one_file["filename"]."\""); // use 'attachment' to force a download
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$one_file["filename"]."\"");
readfile(_config('files_path').$_GET['id']);// reading the actual raw file stored in my online directory
Problem is that Im testing using a word document and its uploading perfectly - I've even checked the raw file being uploaded by manually changing its extension and it's uploading perfectly. The problem is that when it's downloaded using the code above, the Word file seems corrupted or something, because when I try to open it, it's all mumbled and jumbled. What's happening? I've used this snippet on a few other sites I've worked on, and they work perfectly fine... Help please!
By default PHP's header function will replace previous headers with the same name, so your first two headers are being overwritten by the second two. Delete the second two and see if that works.
See if this helps:
Webkit and Excel file(PHPexcel)
I was having the same problem: every time I downloaded a file, it was supposedly "corrupt". Turns out I had made a stupid directory path mistake, but the php error was being written into the downloaded file. Which, of course, made it "corrupt".
Actually I solved by reading Ian Wetherbee's comment about testing with a plain text file. Thanks Ian!