Jump to content

PHP help (again)

Guest
Go to solution Solved by mariushm,

You can use fopen and fread and fclose  or you could just use file_get_contents and file_put_contents

Something like this

 

<?php
// __DIR__  is the folder where this script is saved

$path = __DIR__ .'/forbidden/counter.txt';

$content = file_get_contents($path);
if ($content===FALSE) {
	die("Fail: file $path not found.\n");
}
$value = intval($content);
$value++;

$result = file_put_contents($path,$value);
// output something here if you want to
// header('Content-Type: application/vnd.api+json');
// echo json_encode(array('counter'=>$value, 'success'=>TRUE));

?>

 

Need a PHP script that does this, It Finds a txt file (localhost:443/forbid/count.txt

 

When the PHP file is Called, it will Get the Value of the Current number lie "1" and Change it to "2", "3", and so on.

 

How can i go about this?

 

Running PHP 7.4 with Apache (and the PHP file will be called from a javascript ajax request)

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can use fopen and fread and fclose  or you could just use file_get_contents and file_put_contents

Something like this

 

<?php
// __DIR__  is the folder where this script is saved

$path = __DIR__ .'/forbidden/counter.txt';

$content = file_get_contents($path);
if ($content===FALSE) {
	die("Fail: file $path not found.\n");
}
$value = intval($content);
$value++;

$result = file_put_contents($path,$value);
// output something here if you want to
// header('Content-Type: application/vnd.api+json');
// echo json_encode(array('counter'=>$value, 'success'=>TRUE));

?>

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×