Pregunta

In PHP im creating a tool to open txt file with an integer in it, increment the number, save the number to a variable and then save and close the file. this is the code i have for it and it doesnt seem to work when i have it on my test server. Can anyone clue me in as to why this isnt working properly?

//opens pclnumber.txt to $handle, saves number to $number, Increments number in text file, saves and closes file
$handle = fopen("pclnumber.txt", "w+");
$number = fread($handle);
fwrite($handle, $number+1);
fclose($handle);
¿Fue útil?

Solución

over all you must set reading permission on your file, and than you can use the follow code:

$filename = "pclnumber.txt";

//read file content
$handle = fopen($filename, "r");
$number = fread($handle, sizeof($filename));
fclose($handle);

//update file content
$handleWrite = fopen($filename, "w+");
fwrite($handleWrite, $number+1);
fclose($handleWrite);

Bye, Marco

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top