Lassie Posted June 11, 2007 Share Posted June 11, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/ Share on other sites More sharing options...
Wildbug Posted June 11, 2007 Share Posted June 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-272679 Share on other sites More sharing options...
Lassie Posted June 12, 2007 Author Share Posted June 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273030 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 Why not just unlink() (delete) the file once it's been downloaded? Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273180 Share on other sites More sharing options...
Lassie Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks a good thought .Ok. How would I do that. Add code after the fpassthru? I am not familiar with unlink. I can still log the directory etc so i can record that there was a download so that would work. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273253 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 Yes, after you output the file, delete it. See the PHP manual for unlink(). You may need to sleep for a second or something after fpassthru -- if the download doesn't work at first because the file was deleted already or something. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273258 Share on other sites More sharing options...
Lassie Posted June 12, 2007 Author Share Posted June 12, 2007 Right i have tried the unlink but no result. i added the following code sleep(1); fpassthru($fdl); sleep(1);#new code unlink($fd1); Have i misunderstood the use of unlink? Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273266 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 unlink (PHP 4, PHP 5) unlink — Deletes a file Description bool unlink ( string $filename [, resource $context] ) Filename, not descriptor. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273273 Share on other sites More sharing options...
Lassie Posted June 12, 2007 Author Share Posted June 12, 2007 Ok sorry. What result would I expect. eg the link not work and/or the file in the directory deleted. At present I used $file to no avail. so I need to work on this. Thank you for your patience. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273314 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 Eh, I think you need $mainpath, looking at your code. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273323 Share on other sites More sharing options...
Lassie Posted June 13, 2007 Author Share Posted June 13, 2007 Thanks for coming back. $mainpath does nothing also. I will try and debug it. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273750 Share on other sites More sharing options...
Lassie Posted June 13, 2007 Author Share Posted June 13, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-273756 Share on other sites More sharing options...
Lassie Posted June 14, 2007 Author Share Posted June 14, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-274502 Share on other sites More sharing options...
joshi_v Posted June 14, 2007 Share Posted June 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-274529 Share on other sites More sharing options...
Lassie Posted June 14, 2007 Author Share Posted June 14, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/55077-problem-updating-counter/#findComment-274560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.