Jump to content


Photo

Download Script


  • Please log in to reply
11 replies to this topic

#1 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 November 2005 - 04:56 AM

I'm having some trouble with a download script I cooked up to serve downloads and log the number of them. I have no experience using the file operations with php, as I'm just learning it, but I can't find errors in my script...

Here's the php:
   $add = 1;
    $file = $_GET['file'];
    $current = fopen('downloads/log/' . $file . '.txt', 'r');
    fclose($current);
    $del = fopen('downloads/log/' . $file . '.txt', 'w');
    fclose($del);
    if($current < 1){
   	 $add = 1;}
   	 else{
   	 $add = $current + 1;}
    $fh = fopen('downloads/log/' . $file . '.txt', 'w') or die("unknown");
    fwrite($fh, $add);
    fclose($fh);
    $n = fopen('downloads/log/' . $file . '.txt', 'r');
    fclose($n);
 <b><?php echo $file; ?></b><br>
 	 Your download of <?php echo $file; ?> will begin in five seconds. If it does not, click the link below.<br><br>
 	 <a href="downloads/<?php echo $file; ?>">Download</a><br>
 	 This file has been downloaded <?php echo $n ?> times.
For some reason the end result looks like:

test.zip
Your download of test.zip will begin in five seconds. If it does not, click the link below.

Download
This file has been downloaded Resource id #4 times.

In the log itself the number is always 2...

#2 Kravvitz

Kravvitz

    [X]HTML, CSS, & JS/DOM Adept

  • Members
  • 443 posts
  • Location:USA

Posted 26 November 2005 - 05:54 AM

For one thing you need to use fread() or something.

fopen() returns a file handle, not the contents of the file.

#3 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 November 2005 - 06:16 AM

Thank you very much!
Its working now. ^_^

#4 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 26 November 2005 - 06:21 AM

^What he said, use one of the following:

$counter = intval(fgets($current));

$counter = intval(fread($current, 8)); // 8 characters should be enough for a loooong time

$counter = fscanf($current, '%d');

$counter = intval(file_get_contents('/path/to/file'));

Edited by DCoder, 26 November 2005 - 06:22 AM.

Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#5 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 November 2005 - 06:37 AM

Well it seems to be working with this code, and I'm slightly worried about messing it up.
   $file = $_GET['file'];
    $log = 'downloads/log/' . $file . '.txt';
    $logfh = fopen('downloads/log/' . $file . '.txt', 'r');
    $current = fread($logfh, filesize($log));
    fclose($logfh);
    $new = $current + 1;
    $del = fopen('downloads/log/' . $file . '.txt', 'w');
    fclose($del);
    $fh = fopen('downloads/log/' . $file . '.txt', 'w');
    fwrite($fh, $new);
    fclose($fh);
    $logread = fopen('downloads/log/' . $file . '.txt', 'r');
    $n = fread($logread, filesize($log));
    fclose($logread);
Do I even need the whole $del segment? Its only there to delete the log's contents, but will $fh do that anyways?

Edited by DeathRay2K, 26 November 2005 - 06:40 AM.


#6 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 26 November 2005 - 06:55 AM

If your host supports PHP 5, you can crop all that down to
$fn = '/path/to/file';
 $num = intval(file_get_contents($fn));
 $num++;
 file_put_contents($fn,$num);
And $num will contain the counter value for when you want to output it.
PHP says file_*_contents is much faster than fopen fread fclose.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#7 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 26 November 2005 - 07:10 AM

Unfortunately my host doesn't support PHP5. ^_^
That was the first thing I tried, but it didn't work.

#8 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 05 January 2006 - 06:21 PM

Well, I thought I had it sorted, but now I have new problems. The code is adding 2 every time, and I can't figure out why.
  	$log = $dir . '/log/' . $file . '.txt';            	//The location of the log file, as $log
  	if(file_exists($log)){
    $logfh = fopen($log, 'r');          //Creates a filehandle called $logfh
    $current = fread($logfh, filesize($log));            	//$current gets the contents of the current log for the specified file
    fclose($logfh);                    //Close the $logfh filehandle
  	}
  	else{
    $current = 0;
  	}

  	$new = $current + 1;                  //Adds 1 to $current a $new

  	$fh = fopen($log, 'w');          //A new filehandle for writing a new log
  	fwrite($fh, $new);                  	//Write $new into the log
  	fclose($fh);                    //Close the filehandle for writing the new log
  	$logread = fopen($log, 'r');        	//New filehandle called $logread for reading the end result of the log
  	$n = fread($logread, filesize($log));              //This actually reads the new log as $n
  	fclose($logread);                  	//Close the $logread filehandle
All it does is read the log, add 1 to it, and write it back into the log...

Edited by DeathRay2K, 05 January 2006 - 06:23 PM.


#9 DCoder

DCoder

    One of the Few

  • Members
  • 331 posts
  • Location:Lithuania, Central Europe
  •  Old Grumpy Bastard

Posted 06 January 2006 - 06:15 AM

You wouldn't happen to be calling that twice per page by any chance?

Btw, function_exists() is a pretty useful tool to simulate file_get/put_contents from PHP 5 and whatnot :p Right now, your code seems quite repetitive with all those reads/writes IMHO.

if(!function_exists('file_get_contents'))
{
 function file_get_contents($fname)
 {
  // function body goes here
 }
}

P.S. some of those are pretty useless comments... Comments should explain the tricky parts of the code, not every single line.
Ares - How do you want to improve YR today?
What's stopping you from migrating to Ares? We can't implement it unless you tell us!

ModEnc - C&C Modding Encyclopedia | Modders' Quote Database | Yet Another C&C Modding Forum

#10 Guest_ImmoMan_*

Guest_ImmoMan_*
  • Guests

Posted 06 January 2006 - 11:27 AM

LOL! :p All those comments would make a good example for 'how not to code'...

$new = $current + 1;                  //Adds 1 to $current a $new
Classic! :lol:

Edited by ImmoMan, 06 January 2006 - 11:27 AM.


#11 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 06 January 2006 - 02:39 PM

LOL! :lol: All those comments would make a good example for 'how not to code'...

$new = $current + 1;                  //Adds 1 to $current a $new
Classic! :lol:

View Post

Yeah, I know, and now its really hard to do anything with it...:DE012: I'm going to remake it once I get my website running on PHP5, but in the meantime, do you have any suggesstions? :p
That part of the code I've actually already changed. ;)

Edited by DeathRay2K, 06 January 2006 - 02:41 PM.


#12 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 09 January 2006 - 12:40 PM

Sorry for the double post, but I have switched to a server that supports PHP5! :) So, I'm going to start from scratch using much improved code. :grin:
(And no, I wasn't calling that twice per page. ;))

Edited by DeathRay2K, 09 January 2006 - 12:43 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users