How do I auto download .jar files with Php - php

I have looked for an answer for to long, I have the auto download and redirect done, I just need it to make the .jar file download properly. It keeps getting an error when you download and open it.
It says "Error: Invalid or corrupt jarfile C:/path/Final frontier(Pre-Alpha 0.3).jar"
<?php
header("Content-Type: 'application/jar', 'true'");
header('Content-Disposition: attachment; filename="Final frontier (Pre-> Alpha 0.3).jar"');
header("Content-Length: " . filesize("public_html/paid/game/Final frontier (Pre-Alpha 0.3).jar"));
$fp = fopen("public_html/paid/game/Final frontier (Pre-Alpha 0.3).jar", "r");
fpassthru($fp);
fclose($fp);
?>

"Thanks you so much!!! That worked! – TJGames 1 min ago"
Comments to answer, since it solved the question.
This fopen("public_html/ seems like it may be playing tricks on you. I'd either remove it fopen("paid/ if the script's running from the root, or fopen("../paid/ as an added example, or use a full server path. I.e. fopen("/var/usr/you/public_html/paid/
Do the same thing for filesize("public_html/

Related

Readfile is best solution to download external files?

I need to get a remote file and give it to user without saving it to my server disk (for hiding original URL) and found a lot of posts about download external files with various functions like file_get_contents or readfile. Already I'm using this one:
function startDownload($url){
if($this->url_exists($url))
{
//get filename from url
$name=$this->getFileName($url);
//first flush clear almost output
ob_end_flush();
//final clear
ob_clean();
//set headers
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $name . "\"");
//send file to client;
readfile($url);
//exit command is important
exit;
}
else JFactory::getApplication()->enqueueMessage(JText::_('URL_NOT_FOUND'), 'error');
}
And that's working but there is a problem! For a file with 200 MB size it takes ~ 10 seconds to start download in client browser. I think it's because readfile first downloads whole file to my server buffer and then give it to user. Is that right?
And is it possible to make it faster? for example download be started before fetch ended or it isn't possible technically?
In fact I don't know that this method is optimised or not. Any technical advice would be appreciated.
Note :
I know that this function should be changed for big files and that's not my concern now.
I consider to buy the external server in the same datacenter to make this download faster.
Target is that [File server] be separate than the file [online shop].
I tested curl method that mentioned by #LawrenceCherone. It worked nicely but when moved it to my project the result was the same as readfile (white screen for a few seconds).
So suspect to readfile() function. Separate my previous code to a single PHP file and result was amazing! Download starts immediately.
So I think my guess wasn't right and problem was not related to readfile function.
After a little search found a minor modification. I added below line :
while (ob_get_level()) ob_end_clean();
before the :
readfile($url);
And now download starts before whole file fetched in my server.

How to download a file without an extension in PHP

I have a file with no extension on it, but I know it's a tiff. I want to be able to download this file via PHP.
I created a page with a link to another php page, which has the following content:
<?php
$imgPath = 'http://server/23700-b074137f-eb5c-45d6-87c2-13c96812345b';
header("Content-disposition: attachment; filename=invoice.tiff");
header("Content-type: image/tiff");
readfile($imgPath);
?>
When I click the link, I get a prompt to download invoice.tiff, but it's 0 bytes.
However, if I rename the file on the server to 23700-b074137f-eb5c-45d6-87c2-13c96812345b.tiff (and change the $imgPath), it works.
How do I accomplish this without renaming the file to include the extension?
It's possible the 'tiff' extension is registered as a known file type on the server, so when you rename and request the tiff it's permissions will allow you to open it. However, with no extension, the security is probably stopping you from reading it, as mentioned by 'Mike B' above. To check this try just entering the file name in your browser address bar and see if it opens, both with and without the 'tiff' extension. There is no workaround for getting past the security issue, short of changing the severs security which would be very bad.
You are retrieving the file from a URL, therefore activating the 'fopen wrappers' in readfile. In general, you should not do this, especially when working locally since it invokes a lot of unnecessary overhead and (in this case) unwanted 'magic' behaviour.
Just use readfile on the local path to the file, and it'll be fine, or use die(file_get_contents($imgPath)) instead of the last line to circumvent PHP's native behaviour.
It works for me:
$imgPath = 'http://server/23700-b074137f-eb5c-45d6-87c2-13c96812345b';
$f = fopen($imgPath, "r");
header("Content-disposition: attachment; filename=invoice.tiff");
header("Content-type: image/tiff");
fpassthru($f);
You should also add the content-length header like so:
// untested code
header('Content-Length: '.strlen(stream_get_contents($imgPath)));

Error 324 EMPTY_RESPONSE while echo file_get_contents

I'm trying to make something like ftp. I have a home made server with aplication set on wamp.
And i don't want to keep all my files in wamp folder so i also acces local files from this application.
Already read a lot of staff that i can't do this and no browser let me do something like this. But I managed it somehow.
This is the code i use to download files:
function getFile($name,$path) {
if(file_exists($path.$name)) {
$name = urldecode($name);
$fsize = filesize($path.$name);
header("Content-disposition: attachment; filename=\"".$name."\"");
header("Content-type: application/force-download");
header("Content-length: ".$fsize);
echo file_get_contents($path.$name);
}
And it works for a small files no matter what extension they've got but i also have to download some .exe files over 200Mb.
i have already set memory_limit in php.ini couse this was the reason of first troubles i've got but now i get the ERROR:324.
Any ideas how to get over it?
file_get_contents in first read file in memory and then return it.
For file size of 200MB it need more than 200MB memory for one script.
For fix it you should read file by blocks and output it.
For example
$fp = fopen($path.$name, "rb");
if ($fp) {
while(!feof($fp)) {
$str = fread($fp, 1024);
echo $str;
}
fclose($fp);
}

Using php to force download a pdf

Im trying to get a website to have a button that forces a download of a pdf.
Heres the html of the button:
<a href=scripts/download.php>
<input type="image" src="images/download.gif" alt="Submit button"/>
</a>
And the php script so far:
<?php
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename=documents/ECM_IT_ResumeDownload.pdf');
readfile('documents/ECM_IT_ResumeDownload.pdf');
?>
This seems to download the file fine but when I go to open it i get this error:
"Adobe Reader could not open 'documents_ECM_IT_ResumeDownload.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
Any help would be greatly appreciated.
EDIT
Opened the pdf in a text editor and got this message:
"
Warning: readfile(documents/ECM_IT_ResumeDownload.pdf) [function.readfile]: failed to open stream: No such file or directory in html/scripts/download.php on line 4
"
The document is definitely there though. in html/documents/ECM_IT_ResumeDownload.pdf
$file_url = www.example.com/pdffolder/$pdfname;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=".$pdfname);
readfile($file_url);
Try removing the path to the file and just leave the file name in the content:
header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=ECM_IT_ResumeDownload.pdf');
Have you tried getting rid of the closing PHP tag (the ?>) at the end? It will treat the page as a pure PHP page, removing any possible new lines that might accidentally get appended to the end of the output. This helped me when I was dynamically creating excel files for download, and they were downloading as corrupted. Check out this page for more information:
http://www.php.net/manual/en/language.basic-syntax.phptags.php
From your edited question, it seems like PHP is unable to find the file. Try using an absolute path to the file like so: "c:\blah\de\blah\bloo.pdf" or "c:/blah/de/blah/bloo.pdf". If one of those paths works and downloads correctly, your relative path is incorrect in some way.
I always use Gowon Patterson's download script, it also has hotlink protection:
http://by.gowondesigns.com/getfile/
By the way, a bit late, but to identify the problem properly here:
Your download script is at scripts/download.php and the file you want to download is at documents/[...].pdf.
Therefore, your readfile() function should be traversing to the parent directory (outside of scripts/), e.g. readfile('../documents/[...].pdf');.

Image file download problem

I am facing problem in downloading any image file from the server.
I am able to upload them successfully and can open them from the location where they are uploaded and stored.
When i download using my function the image files get downloaded fully, file size is also correct but when i open them i get an error No image preview !!!
$fileString=$fileDir.'/'.$fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
$instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
}
// make sure the file exists before sending headers
if(!$fdl=#fopen($fileString,'r'))
{
die("Cannot Open File!");
}
else
{
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
header("Content-length:".(string)(filesize($fileString)));
sleep(1);
fpassthru($fdl);
}
I am using IE as browser.
I am using this code snippet to download the file and not to show on the browser. The script executes and i get prompted on whether i want to open / save the file. When i save the file the size is also correct but the image doesn't show up. When i right click and see the summary of the file, it says the summary is unavailable.
Thanks in advance. Kindly help.
It's not clear from your code, are you using this PHP snippet to serve the image on a web page, such as:
<img src="my-php-script.php" alt="blah blah blah" />
If so, your content-type is incorrect. You would need to use an image MIME type, such as image/gif, image/jpeg, image/png or image/tiff, whichever is most appropriate.
This line: header("Content-type: application/octet-stream"); seems fishy to me. You might want to try giving the actual mime type and see if that helps
The problem is with the MIME type. Please have a look here:
http://www.daniweb.com/forums/thread122055.html
This is if you want to view images in the browser.
If you want to download them as binaries then leave your mime type.
Also please change file open mode from "r" to "rb"

Categories