5 Replies - 407 Views - Last Post: 05 August 2012 - 03:20 AM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

If function when overwriting file

Posted 04 August 2012 - 10:57 PM

Hey i know how to overwrite a file on a server and delete a file but how do i change this code so that the string overwritten onto the file changes according the string which is already in the file
For example -
If there is a file named Hello.txt in the server and in side hello.txt the numerical 1 is present
So when i click the "change value" button i want my code to check the numerical which is allready there in hello.txt and change according to that.
So if 1 is already present in hello.txt when i click the "change value" button i want the numerical value in hello.txt to change to 2
And keep changing by +1 there after until it reaches 10 and then if hello.txt numerical value is 10 i want a specific file on my server deleted
$myFile = "hello.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "1";
fwrite($fh, $stringData);
fclose($fh);


Thats to overwrite a file

#example "/files/example.exe" 
$DelFilePath = $setup["serverWebrootPath"] . $fileName;
# delete file if exists
if (file_exists($DelFilePath)) { unlink ($DelFilePath); }

Thats to delete a file if it exists

Is This A Good Question/Topic? 0
  • +

Replies To: If function when overwriting file

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: If function when overwriting file

Posted 05 August 2012 - 01:21 AM

That means first you have to read the file, get the value present, and use if condition. If the value is less than 10, increment it and overwrite the file with the new value, else delete it.
Was This Post Helpful? 0
  • +
  • -

#3 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

Re: If function when overwriting file

Posted 05 August 2012 - 02:52 AM

Firstly, thanks smohd for clarifying his question :)

View Postsmohd, on 05 August 2012 - 10:21 AM, said:

That means first you have to read the file, get the value present, and use if condition. If the value is less than 10, increment it and overwrite the file with the new value, else delete it.


Secondly, I think this is what you need:

<?php
function readNumber($myFile){
	$fh = fopen($myFile, "r") or die("can't open file");
	$counter = fread($fh, filesize($myFile)); // read value
	fclose($fh);
    return (int)$counter; // convert to int & return value
}

function writeNumber($myFile, $val){
	$fh = fopen($myFile, "w") or die("can't open file");
	fwrite($fh, $val);
	fclose($fh);
}

$file = "hello.txt"; // your file should exist and have an initial value = 0
$file_to_be_deleted = "test.txt";

$c = readNumber($file); // get numerical value
if($c == 10){ 
	if(file_exists($file_to_be_deleted)) unlink($file_to_be_deleted); // delete file
	writeNumber($file, 1); // reset file value to 1
}else writeNumber($file, $c+1); // update file value
?>



Hope it helps ;)
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: If function when overwriting file

Posted 05 August 2012 - 03:12 AM

StefanOnRails we dont write code for people here. Let them try things for themselves so they can understand concepts and get to know the staffs.
Was This Post Helpful? 0
  • +
  • -

#5 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 305
  • Joined: 19-January 12

Re: If function when overwriting file

Posted 05 August 2012 - 03:17 AM

View PostStefanOnRails, on 05 August 2012 - 02:52 AM, said:

Firstly, thanks smohd for clarifying his question :)

Secondly, I think this is what you need:


Thank You so much this helped a lot and with a bit of tweaking i managed to make it just the way i need it to be :D
You sir are cool ;)
Was This Post Helpful? 0
  • +
  • -

#6 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 105
  • Joined: 31-July 12

Re: If function when overwriting file

Posted 05 August 2012 - 03:20 AM

View Postsmohd, on 05 August 2012 - 12:12 PM, said:

StefanOnRails we dont write code for people here. Let them try things for themselves so they can understand concepts and get to know the staffs.


Oh, OK I was trying to be helpful, but I think you're right :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1