Jump to content

Archived

This topic is now archived and is closed to further replies.

mydog

PHP Problems :(

Recommended Posts

I've been trying to get this to work for some time actually.. And it doesn't want to work it seems. OH and I am very new to this site, and PHP. I know the basics of it though.

[code]<head>
<title>Downloading $file</title>
<?php
/*make sure you change all links to this page to download.php?file=none or it will try to find a file*/
$id=$_GET['id']; //this will get the file in the url
switch (id){
case 0:$dl=minigame.exe;break;
};

{
//redirect the page to the file in 1 second
echo '<meta http-equiv="refresh" content="1;url=$dl";>';
//this will add 1 to the downloads
$f=fopen('downloads.txt',"r");
$num=fread($f,filesize('downloads.txt'));
fclose($f);
$num++;
$f=fopen('downloads.txt',"w");
fwrite($f,$num);
fclose($f);
}
?>
</head>[/code]

I have tried many variations and I put in the switch statement myself to see if it would work.. and it didn't. The problem here is that when it goes to get the file, it returns $dl in the address bar. When it should be the file. Thanks in adv. If I'm breaking any rules sorry I'm new ;)

Share this post


Link to post
Share on other sites
Change this line: echo '<meta http-equiv="refresh" content="1;url=$dl";>';
to: echo "<meta http-equiv=\"refresh\" content=\"1;url=$dl\";>";

You need to know the difference between single quotes and double quotes. When echoing with single quotes ('), it will output the statement EXACTLY as shown. It will not fill in variables. However, when echoing with double quotes (") it will replace any variable (e.g. $dl) with the actual value. Note, however, that when echoing with double quotes, any double quote within the double quote must be forwarded with a slash, as you can see above.

Share this post


Link to post
Share on other sites
If it helps, this is how I handle downloads.

1 ) create a file "download.php"
[code]<?php
if (isset($_GET['file'])) {
    $fname = $_GET['file'];

    $path = 'c:/downloads/'; # path to download files on server - change as required #

    if (is_file($path.$fname)) {
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$fname");
        header("Content-length: " . filesize($path.$fname));
        $fp = fopen($path.$fname, 'rb');
        fpassthru($fp);
    }
}
?>[/code]

In another page, list the download option for the user

[code]<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Downloads</title>
</head>
<body>
<p>Select file to download </p>
<ul>
<li><a href='download.php?file=file1.zip'>File1.zip</a>
<br>Description of file1</li>
<li><a href='download.php?file=file2.zip'>File2.zip</a>
<br>Description of file2</li>
</ul>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.