Jump to content


Photo

PHP Problems :(


  • Please log in to reply
3 replies to this topic

#1 mydog

mydog
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 August 2006 - 09:53 PM

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.

<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>

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 ;)

#2 Woolf

Woolf
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 18 August 2006 - 09:55 PM

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.

#3 mydog

mydog
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 August 2006 - 10:12 PM

Wow..didn't know that thanks!  ;D

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,015 posts

Posted 18 August 2006 - 10:49 PM

If it helps, this is how I handle downloads.

1 ) create a file "download.php"
<?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);
    }
}
?>

In another page, list the download option for the user

<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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users