Jump to content

Renaming files-input to specify where to begin??


new2code

Recommended Posts

I know the subject heading might be misleading, However, Im new to php and english.

 

here is my question I have a script which takes a file and breaks in to smaller files with filenames part.date and a sequential integer (1,2,3,4 ....)

 

NVISION-part.01-01-2010-1.txt

NVISION-part.01-01-2010-2.txt

NVISION-part.01-01-2010-2.txt

 

I have several input files and every time I have a new input file it will start naming the out put files from 1,2,3 and so on.

 

I was wondering is there a way I can have a prompt at the beginning so I can say which number to start from when renaming the files?.

 

code:

<?php

class filesplit{
/**
     * Constructor
     * @access protected
     */
function filesplit(){

}

/**
 * File to split
 * @access private
 * @var string
 **/
    var $_source = 'logs.txt';

/**
 * 
 * @access public
 * @return string 
 **/
    function Getsource(){
	return $this->_source;
}

/**
 * 
 * @access public
 * @return void
 **/
function Setsource($newValue){
	$this->_source = $newValue;
}

/**
 * how much lines per file
 * @access private
 * @var integer
 **/
    var $_lines = 1000;

/**
 * 
 * @access public
 * @return integer 
 **/
    function Getlines(){
	return $this->_lines;
}

/**
 * 
 * @access public
 * @return void
 **/
function Setlines($newValue){
	$this->_lines = $newValue;
}

/**
 * Folder to create splitted files with trail slash at end
 * @access private
 * @var string
 **/
    var $_path = 'logs/';

/**
 * 
 * @access public
 * @return string 
 **/
    function Getpath(){
	return $this->_path;
}

/**
 * 
 * @access public
 * @return void
 **/
function Setpath($newValue){
	$this->_path = $newValue;
}

/**
 * filesplit::configure()	Configure the class
 * 
 * @param string $source
 * @param string $path
 * @param string $lines
 * @return 
 **/
function configure($source = "",$path = "",$lines = ""){
	if ($source != "") {
	    $this->Setsource($source);
	}
	if ($path!="") {
	    $this->Setpath($path);
	}
	if ($lines!="") {
	    $this->Setlines($lines);
	}
}

    
    /**
     * Run the FileSplit process
     * @access public
     * @return void
     **/
    function run(){
        $i=0;
        $j=1;
        $date = date("m-d-y");
        unset($buffer);
        
        $handle = @fopen ($this->Getsource(), "r");
        while (!feof ($handle)) {
          $buffer .= @fgets($handle, 4096);
          $i++;
	  
	  if ($i >= $this->_lines) {
	  
		  $fname = $this->Getpath()."NVISION-part.$date.$j.txt";
		  
		   if (!$fhandle = @fopen($fname, 'w')) {
				print "Cannot open file ($fname)";
				exit;
		   }

		   if (!@fwrite($fhandle, $buffer)) {
			   print "Cannot write to file ($fname)";
			   exit;
		   }

		   fclose($fhandle);
		   unset($buffer,$i);
		   $j++;
		}
        }

	if (isset($buffer)) {
		  $fname = $this->Getpath()."NVISION-part.$date.$j.txt";
		  
		   if (!$fhandle = @fopen($fname, 'w')) {
				print "Cannot open file ($fname)";
				exit;
		   }

		   if (!@fwrite($fhandle, $buffer)) {
			   print "Cannot write to file ($fname)";
			   exit;
		   }

		   fclose($fhandle);
		   unset($buffer,$i);
	}
        fclose ($handle);
    }
    
    /**
     * Delete all generated part files
     *
     */
    function delete(){
	$d = dir($this->Getpath());
	while (false !== ($entry = $d->read())) {
	   if (ereg("^NVISION-part",$entry)) {
	   	unlink($this->Getpath()."$entry");
	   }
	}
	$d->close();
	return;
    }
}

?>

You could just start it with an html form input to initialize the code to begin with and do it that way.

 

<html>
<body>

<form action="start.php" method="post">
Intiate Number Please: <input type="text" name="start_num" />
<input type="submit" />
</form>

</body>
</html>

click on the submit button, the form data is sent to a PHP file, called "start.php"

$intiated_start = $_POST["start_num"];
// this then goes at start of your program

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.