Updating a text file

I want to make a gallery.
Consider my gallery images folder contain 3 images named 1.jpg,2.jpg and 3.jpg. And also there is a text file which only contains the text “3”.(It shows the number of images uploaded).

Now I am going to upload an image. Can you please give me PHP script for:

Read the number in the text file
Upload the image.
Rename the image to the next number in the text (say 4.jpg, because this is the 4th image)
Edit the textfile and increment the number by one. (increment the 3 to 4).

Can you please help me?