Jump to content

problem updating counter


Lassie

Recommended Posts

I need to update a counter when a file is downloaded.

The purpose is to know when all the files have been downloaded and then delete the directory.

The program lists the files to be downloaded and each download link goes to the start of the program to retrieve the file.

My problem is that my counter is not updating.

 

The code is:


# If a download has already been selected,
#----------------------------------------- 
If ($_GET['file']){

# Get the filename to be downloaded
#----------------------------------
$file = $_GET['file'];
$file = stripslashes($file);
$file = htmlspecialchars($file);
$mainpath = "$path$file";

# Increment download count
#-------------------------
$filename = "./file_info/descriptions/$file.0";
$newfile = fopen($filename,"r");
$content = fread($newfile, filesize($filename));
fclose($newfile);
$fileinfo = explode ("|",$content);
$fileinfo[0] ++;

# Save the new file count
#------------------------
$content = implode("|", $fileinfo);
$newfile = fopen($filename,"w");
fwrite($newfile, $content);
fclose($newfile);

# Translate file name properly for Internet Explorer
#---------------------------------------------------
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
	$file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
}

# Make sure the file exists before sending headers
#-------------------------------------------------
if(!$fdl=@fopen($mainpath,'rb')){
   die("Cannot Open File!");
} else {

ini_set("zlib.output_compression", "Off");

# Send the headers then send the file
#------------------------------------
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=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath)));

sleep(1);
fpassthru($fdl);

$counter++;     #this is the counter to register the file has been downloaded.
$to="xxxxxxxxxxx";
	$subject="counter count";
	$msg="all files downloaded for folder $hash and counter equals $counter and files equals $_SESSION[num] ";
	mail($to,$subject,$msg);

if ($counter==$_SESSION[num]) 
{
	$to="xxxxxx";
	$subject="download count";
	$msg="all files downloaded for folder $hash";

	mail($to,$subject,$msg);

	$counter=0;

	recursive_remove_directory($path.$hash);

	session_destroy();
	exit;

}
  

this is the link code

# Display the File Name.
			#-----------------------
			if ($display_filename == 1){
				print "$display_filename_tag<font size=\"$display_filename_size\" face=\"$display_filename_font\">\n";
				print "<a href=\"./index.php?file=$fileattr[2]\">\n";
	        		print "$fileattr[1]</a>$display_filename_tag_end<br>\n";
			}

Link to comment
Share on other sites

Nice, neat code.

 

I don't see you initializing $counter anywhere in your code before $counter++.  It looks like a file can be downloaded multiple times, though, if I've interpreted the first three sections properly.  But then you want a "global" counter to be incremented once when a file is downloaded and when it equals the number of files, delete them?  Where are you saving this $counter value?  I don't see a session_start() or anything.

Link to comment
Share on other sites

Thanks for coming back.

I have to say the neat code is not all mine. I am adapting a program for my purpose.

I have a session start at the start of the code. Since the code is 700 lines I have not posted it all.

The first part of the script sets up the conf varaibles.

The file(s) are the downloaded.

If no file is selected as in first time around the files to be downloaded are retrieved from a download directory and displayed in a table.

When a file link is clicked the script returns to the start and download is detected and the file downloaded.

As it is the links can be clicked forever, so I am trying to detect when all the available files have been downloaded,

hence the counter and then remove the directory and end the session and script.

I havnt intialised the counter as in $counter=0; I am not sure where to put this without it resetting to zero each time the script runs through.

I hope this explains what I am doing and you can suggest a way forward.

Link to comment
Share on other sites

Just to say that $mainpath does of course hold the path and file in this format

./033469a51bcc2be67ae5bd67c32e4ad1/Salad Recipes.pdf

Using unlink do I need to just address the file and if so could you indicate how please.

Thanks

Link to comment
Share on other sites

If anyone is following this thread I need help on unlink.

I amended my code to delete the file after download but the function does not work.

I have tried

unlink("$mainpath");

and system("del $mainpath") as i read that unlink may not work on plder windows systems. I am however running XP

Does anyone have any suggestios as to how to fix this please?

Link to comment
Share on other sites

Me too working on windows XP and unlink is working perfectly for me.

 

this is my piece of code.

 

<?php
$filepath = "/tmp/test.txt";

if (! unlink ($filepath)) {
   echo ("Couldn't delete file");
} else {
    echo ("Removed $filepath");
}

?>

 

 

 

In your case, check whether your $mainpath contains the correct path and file name.

 

Otherwise post u r code here.

 

 

 

Link to comment
Share on other sites

Thanks for your reply.

$mainpath has the correct directory and file as follows  in the form ./dir/filename.ext

The relvant code is

 

# If a download has already been selected,
#----------------------------------------- 
If ($_GET['file']){

# Get the filename to be downloaded
#----------------------------------
$file = $_GET['file'];
$file = stripslashes($file);
$file = htmlspecialchars($file);
$mainpath = "$path$file";

# Increment download count
#-------------------------
$filename = "./file_info/descriptions/$file.0";
$newfile = fopen($filename,"r");
$content = fread($newfile, filesize($filename));
fclose($newfile);
$fileinfo = explode ("|",$content);
$fileinfo[0] ++;

# Save the new file count
#------------------------
$content = implode("|", $fileinfo);
$newfile = fopen($filename,"w");
fwrite($newfile, $content);
fclose($newfile);

# Translate file name properly for Internet Explorer
#---------------------------------------------------
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
	$file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
}

# Make sure the file exists before sending headers
#-------------------------------------------------
if(!$fdl=@fopen($mainpath,'rb')){
   die("Cannot Open File!");
} else {

ini_set("zlib.output_compression", "Off");
$_SESSION[counter]++;	

# Send the headers then send the file
#------------------------------------
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=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath)));

sleep(1);
fpassthru($fd1);
sleep(1);
unlink("$mainpath");

	$to="hawkesley_2jags@yahoo.co.uk";
	$subject="counter count";
	$msg="all files downloaded for folder $hash and counter equals $_SESSION[counter] and files equals $_SESSION[num]and file is $mainpath ";
	mail($to,$subject,$msg);

if ($_SESSION[counter]==$_SESSION[num]) 
{
	$to="hawkesley_2jags@yahoo.co.uk";
	$subject="download count";
	$msg="all files downloaded for folder $hash";

	mail($to,$subject,$msg);

	unset($_SESSION[counter]);


	$_SESSION = array();
	session_destroy();
	exit;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.