Jump to content

[SOLVED] Copys one file but not the rest


mike12255

Recommended Posts

So my code is messy, i apologize, but to the point. Basically im creating a backup of a textfile so that there will be three previous versions and if they edit it more then three times then the fourth of firth ect... gets deleted so i have the function you see below problem is it only creates the $file.1 not $file.2 or $file.3 i cannot figure out why it will not create those files because i say if it doesnt exsist then create it...I put a die after the while statment they got created in the variables of the file names all echoed out prober so im not sure why its not working...here is my code:

 

 

<?php
function nc_save_content ($name, $data)
{	
//see if there is a file to create history for
//next steps 1. Copy the info in that file, paste it into the new file then find out how to move everything. up a number
if (file_exists('content/'.$name)){
$file ='content/'.$name;
//Check to see if all three backups exists and if not create them 
$x =3;
while($x != 0){


//if the files doesnt exists (starting at 3 and going down)
if (!file_exists($file.$x)){
//create it
$ourFileHandle = fopen($file.$x, 'w') or die("can't open file");
fclose($ourFileHandle);
}
$x--;
}
//end of backup checking and creating 
$i=2;

while ($i != 0){
//create the next number 
$z= $i + 1;
//copy from that number to one higher (1 =>2 or 2=>3)
if (copy($file.$i,$file.$z)){
echo "Error in file system on line 21 could copy file!!";
}
//minus i to go to the next lower file
$i--;
}//end while statment

if(!copy($file,$file."1")){
echo "Error in file system on line 27 could copy file!!";
}


}


// Save away.
$path = NC_BASEPATH.'/../content/'.$name;
$fh = fopen('content/'.$name, 'w') or die(nc_report_error("Could not open file: ".$name.". Make sure that this server has read and write permissions  the /nc-cms/content folder."));
fwrite($fh, $data);
fclose($fh);

//Start of history creation





}?>

Link to comment
Share on other sites

I run your code on my linux server.

("mm.txt" ,"this is the content")

 

For the first time, mm.txt is created and the content of mm.txt is all right.

 

For the second time, I got the error message:

"Error in file system on line 21 could copy file!!" TWICE.

mm.txt is exists.

mm.txt2 and mm.txt3 are both created, but they are empty  , because while copying for mm.txt3 , mm.txt2 is not exists.

while copying for mm.txt2, mm.txt1 is not exists.

 

mm.txt1 is created and the content of mm.txt1 is right.

 

 

if (copy($file.$i,$file.$z)){

 

should be  if (!copy($file.$i,$file.$z)){

 

Link to comment
Share on other sites

Ok thanks for posting mate, i fixed the !copy and instead of using fopen to open/create the file i now use touch as you can see below, problem is, i still am not getting files created....

 

<?php

//
//	nc_save_content()
//
//	Check content file to see if it exists, then save data.
//
function nc_save_content ($name, $data)
{	
//see if there is a file to create history for
//next steps 1. Copy the info in that file, paste it into the new file then find out how to move everything. up a number
if (file_exists('content/'.$name)){
$file ='content/'.$name;

//Check to see if all three backups exists and if not create them 
$x =3;
while($x != 0){
//if the files doesnt exists (starting at 3 and going down)
if (!file_exists($file.$x)){

//create it
touch($file.$x); 
}
$x--;
}

//end of backup checking and creating 
$i=2;
while ($i != 0){
//create the next number 
$z= $i + 1;
//copy from that number to one higher (1 =>2 or 2=>3)
if (!copy($file.$i,$file.$z)){
echo "Error in file system on line 21 could copy file!!";
}
//minus i to go to the next lower file
$i--;
}//end while statment

if(!copy($file,$file."1")){
echo "Error in file system on line 27 could copy file!!";
}


}


// Save away.
$path = NC_BASEPATH.'/../content/'.$name;
$fh = fopen('content/'.$name, 'w') or die(nc_report_error("Could not open file: ".$name.". Make sure that this server has read and write permissions  the /nc-cms/content folder."));
fwrite($fh, $data);
fclose($fh);

//Start of history creation





}
//
//	nc_load_create_content()
//
//	Check content file to see if it exists, if not, create it. Open it and return data.
//
function nc_load_create_content ($name)
{	
// Load content if file exists
$path = NC_BASEPATH.'/../content/'.$name;
nc_check_content ($path, 'Edit me! ('.$name.')'); // Make sure content file exists
$fh = fopen($path, 'r') or die(nc_report_error("Could not find file: ".$path));
$data = fread($fh, filesize($path)) or die(nc_report_error("Could not read file: ".$path.". Make sure that this server has read and write permissions to the /nc-cms/content folder."));
fclose($fh);

return $data;
}
//
//	nc_check_content - USED INTERNALLY
//
//	Check content file to see if it exists. And if it doesn't, create it. $path contains the file path, $default contains the default text to go in the file if it is new.
//
function nc_check_content ($path, $default)
{
// If file doesn't exist yet or is of 0 length, create and write something in it.
if (!file_exists($path) || !filesize($path)) 
{
   $fh = fopen($path, 'w') or die(nc_report_error("Could not write file: ".basename($path).". Make sure that this server has read and write permissions to the /nc-cms/content folder."));
   fwrite($fh, $default) or die(nc_report_error("Could not write file: ".basename($path).". Make sure that this server has read and write permissions to the /nc-cms/content folder."));
   fclose($fh);
}

clearstatcache(); // Clear status cache (so filesize() will do its work again)
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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