Jump to content

Simple script help (newbie)


Alex87

Recommended Posts

Hi there, I'm new here, and new to PHP. I've got some programming experience, but I'm totally new to PHP.

 

I was just wondering if someone could help me. I need a script where I basically have a txt file on my server with a number (defaulting to 0), and basically, every time I execute the PHP script, the number in the txt file increments by 1.

 

So in other words, all the php script should do is read the number in the txt and replace the number with number+1.

 

Is that hard to do? If I could get someone to show me how to do this, it would really help a lot.

 

Thanks in advance :)

Alex.

Link to comment
https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/
Share on other sites

Table 1. A list of possible modes for fopen() using mode

 

mode Description

'r' Open for reading only; place the file pointer at the beginning of the file.  

'r+' Open for reading and writing; place the file pointer at the beginning of the file.  

'w' Open for writing only; 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.  

'w+' 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.  

'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.  

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.  

'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.  

'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.  

Ah okay, makes perfect sense now.

 

So in a nut shell, (ill use the first code as an example) the scripts is doing this:

 

<?php

$fileToRead = "counter.txt";

$fp = fopen($fileToRead, "w+");

fwrite($fp, intval(fread($fp, 1024)) + 1);

fclose($fp);

?>

 

The PHP header starts

Create variable storing the name of the file (for reading and writing)

Create a variable for storing the opened file through the file name variable.

Then write (to the file, an integer(read( the opened file, no idea what the 1024 means)) + 1

Close the opened file

End the PHP script.

 

I'm unsure of what the 1024 stands for. Does it mean that the maximum amount to read is from 0 to 1024 bytes?

$contents = fread($handle, filesize($filename));

------------------------------------------------------

fread
(PHP 3, PHP 4, PHP 5)

fread -- Binary-safe file read
Description
string fread ( resource handle, int length )


fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when up to length bytes have been read, EOF (end of file) is reached, (for network streams) when a packet becomes available, or (after opening userspace stream) when 8192 bytes have been read whichever comes first. 

Returns the read string or FALSE in case of error. 



<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>  

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.