if ($_SERVER['REQUEST_METHOD']=='GET' && $_GET['download']==='1')
{
$handle = fopen('lastdownload.txt','rw');
$date = #fread($handle,filesize('lastdownload.txt'));
if (time() - 30 * 60 > $date)
{
fwrite($handle,time());
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="dwnld_'.date('d_m_Y_H_i',filemtime('download.zip')).'.zip"');
readfile('download.zip');
}
exit;
}
Hi everyone, i have a problem about limiting download count.
I want to limit my download count.
If someone request the file with ?download=1
It checks the current time and the time inside the file
If 30 minutes passed before the last download, it lets you download again, else it just exits.
Any help please?
Thank you.
Unless your still using PHP4, I would just use file_put_contents() and file_get_contents().
"rw" is not a valid mode for fopen. You should use "r+" or "x+" and rewind the file pointer after reading:
$handle = fopen('lastdownload.txt','r+');
$date = #fread($handle,filesize('lastdownload.txt'));
rewind($handle);
if(fileatime("lastdownload.txt")>=300)
{
//Access OR File Download Code Here
}
Related
I'm trying to make a download counter in a website for a video game in PHP, but for some reason, instead of incrementing the contents of the downloadcount.txt file by 1, it takes the number, increments it, and appends it to the end of the file. How could I just make it replace the file contents instead of appending it?
Here's the source:
<?php
ob_start();
$newURL = 'versions/v1.0.0aplha/Dungeon1UP.zip';
//header('Location: '.$newURL);
//increment download counter
$file = fopen("downloadcount.txt", "w+") or die("Unable to open file!");
$content = fread($file,filesize("downloadcount.txt"));
echo $content;
$output = (int) $content + 1;
//$output = 'test';
fwrite($file, $output);
fclose($file);
ob_end_flush();
?>
The number in the file is supposed to increase by one every time, but instead, it gives me numbers like this: 101110121011101310111012101110149.2233720368548E+189.2233720368548E+189.2233720368548E+18
As correctly pointed out in one of the comments, for your specific case you can use fseek ( $file, 0 ) right before writing, such as:
fseek ( $file, 0 );
fwrite($file, $output);
Or even simpler you can rewind($file) before writing, this will ensure that the next write happens at byte 0 - ie the start of the file.
The reason why the file gets appended it is because you're opening the file in append and truncate mode, that is "w+". You have to open it in readwrite mode in case you do not want to reset the contents, just "r+" on your fopen, such as:
fopen("downloadcount.txt", "r+")
Just make sure the file exists before writing!
Please see fopen modes here:
https://www.php.net/manual/en/function.fopen.php
And working code here:
https://bpaste.net/show/iasj
It will be much simpler to use file_get_contents/file_put_contents:
// update with more precise path to file:
$content = file_get_contents(__DIR__ . "/downloadcount.txt");
echo $content;
$output = (int) $content + 1;
// by default `file_put_contents` overwrites file content
file_put_contents(__DIR__ . "/downloadcount.txt", $output);
That appending should just be a typecasting problem, but I would not encourage you to handle counts the file way. In order to count the number of downloads for a file, it's better to make a database update of a row using transactions to handle concurrency properly, as doing it the file way could compromise accuracy.
You can get the content, check if the file has data. If not initialise to 0 and then just replace the content.
$fileContent = file_get_contents("downloadcount.txt");
$content = (!empty($fileContent) ? $fileContent : 0);
$content++;
file_put_contents('downloadcount.txt', $content);
Check $str or directly content inside the file
I wrote some code below, at the moment I'm testing so there's no database queries in the code.
The code below where it says if(filesize($filename) != 0) always goes to else even though the file is not 0 bytes and has 16 bytes of data in there. I am getting nowhere, it just always seems to think file is 0 bytes.
I think it's easier to show my code (could be other errors in there but I'm checking each error as I go along, dealing with them one by one). I get no PHP errors or anything.
$filename = 'memberlist.txt';
$file_directory = dirname($filename);
$fopen = fopen($filename, 'w+');
// check is file exists and is writable
if(file_exists($filename) && is_writable($file_directory)){
// clear statcache else filesize could be incorrect
clearstatcache();
// for testing, shows 0 bytes even though file is 16 bytes
// file has inside without quotes: '1487071595 ; 582'
echo "The file size is actually ".filesize($filename)." bytes.\n";
// check if file contains any data, also tried !==
// always goes to else even though not 0 bytes in size
if(filesize($filename) != 0){
// read file into an array
$fread = file($filename);
// get current time
$current_time = time();
foreach($fread as $read){
$var = explode(';', $read);
$oldtime = $var[0];
$member_count = $var[1];
}
if($current_time - $oldtime >= 86400){
// 24 hours or more so we query db and write new member count to file
echo 'more than 24 hours has passed'; // for testing
} else {
// less than 24 hours so don't query db just read member count from file
echo 'less than 24 hours has passed'; // for testing
}
} else { // WE ALWAYS END UP HERE
// else file is empty so we add data
$current_time = time().' ; ';
$member_count = 582; // this value will come from a database
fwrite($fopen, $current_time.$member_count);
fclose($fopen);
//echo "The file is empty so write new data to file. File size is actually ".filesize($filename)." bytes.\n";
}
} else {
// file either does not exist or cant be written to
echo 'file does not exist or is not writeable'; // for testing
}
Basically the code will be on a memberlist page which currently retrieves all members and counts how many members are registered. The point in the script is if the time is less than 24 hours we read the member_count from file else if 24 hours or more has elapsed then we query database, get the member count and write new figure to file, it's to reduce queries on the memberlist page.
Update 1:
This code:
echo "The file size is actually ".filesize($filename)." bytes.\n";
always outputs the below even though it's not 0 bytes.
The file size is actually 0 bytes.
also tried
var_dump (filesize($filename));
Outputs:
int(0)
You are using:
fopen($filename, "w+")
According to the manual w+ means:
Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
So the file size being 0 is correct.
You probably need r+
Sorry I know this question is closed but I am writing my own answer so it might be useful for someone else
if use c+ in fopen function ,
fopen($filePath , "c+");
then the filesize() function return size of file
and you can use clearstatcache($filePath) to clear the cache of this file.
notice: when we use c+ in fopen() and then use the fread(), function reserve the file content and place our string at the end of file content
I'm new in php and I have question on you.
For now, I'm creating one php script where I want use this:
$file = fopen('php://memory','w');
Then, I use fputcsv where I add few arrays to fill into this document.
In the final part, I said that I want make downloadeble link
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="service.csv"');
My method look like this:
public function exportToCsv($Id){
$file = fopen('php://memory','w');
$someDetails = $this->some_model->getSomeDetail($Id);
$details = array(
array("Subject", "Start Date", "Start Time", "End Date", "End Time", "Where"),
array("test subject","test date","test time","test end date","etc..."));
foreach ($details as $detail) {
fputcsv($file, $detail);
}
fseek($file,0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="file.csv"');
fpassthru($file);}
When my browser download this file and when I open it, everything is great, but at the beginning of file, there is one empty line and I dont understand why it happend and how can I remove this line.
Thanks for describe.
Marek
1 ) You should check if there is any blank line / space before the PHP Tags ( <?php ?> )
2) Try adding ob_end_clean(); just before your fpassthru($file); , this will clear the buffer and you'll be sure there's nothing left in it.
I have written a php script which parses this text file
http://www.powerball.com/powerball/winnums-text.txt
Everything is good, but I wish to control the amount that is download i.e. I do not need every single result maybe max the first 5. At the moment I am downloading the entire file (which is a waste of memory / bandwidth).
I saw the fopen has a parameter which supposed to limit it but whatever value I placed in has no effect on the amount of text that is downloaded.
Can this be done? Thank you for reading.
Here is a small snippet of the code in question which is downloading the file.
<?php
$file = fopen("http://www.powerball.com/powerball/winnums-text.txt","rb");
$rows = array();
while(!feof($file))
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
}
fclose($file);
?>
Thanks everyone this is the code which just downloads the first row of results
while(!feof($file))
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
if(count($rows)>1)
{
break;
}
}
fclose($file);
You can break whenever you don't need more data. In this example when count($rows)>100
while(!feof($file)) {
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
if (count($rows)>100)
break;
}
The issue is that your while condition is only met once you've read through to the end of the file. If you only want to get the first N lines you'll need to change that condition. Something like this might help get you started:
$lineCountLimit = 5;
$currentLineCount = 0;
while($currentLineCount < $lineCountLimit)
{
$line = fgets($file);
$date = explode("Draw Date",$line);
array_push($rows,$date[0]);
$currentLineCount++;
}
Please try the following recipe to download only a part of the file, like 10 KBytes first, then split to lines and analyze them. How to partially download a remote file with cURL?
I have a backgrounded ffmpeg process which is outputting a audio file, I want to push this file to user web-browser while ffmpeg continues to write upon the file. I tried the below but this send 0 byte files.
// open the file in a binary mode
$fp = fopen($fname, 'rb');
// send the right headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fname));
ob_end_clean();
fpassthru($fp);
exit;
ffmpeg cannot be launched from PHP/Python and output captured here.
The fpassthru function won't do the job here. In addition, there is going to be an issue of knowing when the file is complete.
File reading functions stop when the end of the file is reached. If there is a concurrent writer increasing the length of the file, then it's indeterminate how far along a reader will get before seeing EOF. In addition, there's no clear way - through the file operations - to know whether the file writer is done.
It may be feasible to attempt to read with timeouts using a loop like this (psuedo-code):
LOOP
READ bytes
IF count read == 0
THEN
SLEEP briefly
INCREMENT idle_count
ELSE
SET idle_count = 0
WRITE
END IF
UNTIL ( idle_count == 10 )
I can put that into PHP code if it helps.
Here is PHP code that does this with two files, in.dat and out.dat:
<?php
$in_fp = fopen("./in.dat", "r");
$out_fp = fopen("./out.dat", "w");
$idle_count = 0;
while ( $idle_count < 10 )
{
if ( $idle_count > 0 )
sleep(1);
$val = fread($in_fp, 4192);
if ( ! $val )
{
$idle_count++;
}
else
{
$idle_count = 0;
$rc = fwrite($out_fp, $val);
if ( $rc != strlen($val) )
die("error on writing of the output file\n");
}
}
Note the odd location of the sleep is to prevent sleeping one second after the last attempt to read.
I recommend setting the idle timeout limit higher than 10 for this purpose.