Jump to content

Recommended Posts

Hi everyone

 

I have a problem with my probe_harvest.php code.  All it needs to do is read .csv files from the home folder, create new .csv files which would look something like this - G0_0806241403_60017_0_000.CSV (the file/files to be read) and G1061_0806180923_0_000.csv (file/files to be created).  Now the newly created files should be moved to a specific folder.  Each probeid (G1061...etc) belongs to a client, which only that client should be able to see using ftp username and password. Here is the code -

<?

 

//include('include.php');

 

/*

 

1. get a list of files in the directory

2. check each file and see if it has 1 or more lines of data

3. if many lines of data split each into an array with filename as key and data as value

4. if not need to split then just record this for later use in an array

5. go through array from step 3 and create the files on disk and put data into then from array value, also had filename to data files array

6. go through array of file that did not need splitting and put names into datafile array

7. delete original files that was used to extract the data in step 3

8. loop though datafiles array and get the file contents for each file and extract data and insert into db

9. for each file in the loop in step 9 move to another directory

whoaa that a lot of stuff

*/

 

 

$datadir = "/usr/home/aquach/";

$moveto = "/usr/home/aquach/farm1/";

$rawdir = "/usr/home/aquach/rawdata/";

define("LOGFILE", "/usr/home/aquach/logfile.txt");

 

$datetime = Date("Y-m-d H:i:s");

$datafiles = array();

logit("~~|$datetime|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

$dh = opendir($datadir );

while (false !== ($file = readdir($dh))) {

    if (!is_dir("$datadir/$file")) {

    if ( strtoupper( substr(strrchr($file, "."), 1)) == "CSV") {

 

    copy("$datadir$file","$rawdir$file");

      $datafiles[] = $file;

 

    }

  }

}

closedir($dh);

 

if (sizeof ($datafiles) > 0 ) {

 

logit("FOUND FILES: " . sizeof ($datafiles) ." files");

$tempdfiles = $datafiles;

$datafiles =  array();

$filestosplit = array();

$filesnosplit = array ();

$filestoremove = array();

 

for ($i = 0 ;$i < sizeof ($tempdfiles) ; $i++) {

if (checkforsplit($datadir, $tempdfiles[$i], $filestosplit )) {

$filestoremove[] = $tempdfiles[$i];

} else {

$filesnosplit[]=$tempdfiles[$i];

}

}

 

// vprint($filestosplit);

//exit;

if (sizeof($filestosplit) >0 ) {

echo "size is: ".sizeof($filestosplit)."<br>\n";

foreach ($filestosplit as $filename => $linedata) {

 

//get probeid

$ppos = strpos($filename,"?");

$modemdata = substr($filename,$ppos+1,5);

$filename = substr($filename,0,$ppos);

//end

 

//echo "create $filename </br>";

logit("CREATE FILE: $filename");

 

$datafiles[]= $filename;

//file_put_contents("$datadir$filename",$linedata);

$weirddate = date("ymdHi");

 

$foundfile = "";

$found = false;

$dh = opendir($moveto );

while (false !== ($file = readdir($dh))) {

 

if (stristr($file,$filename) && $found == false) {

        $found = true;

$foundfilename = $file;

 

}

}

closedir($dh);

 

if ($found) {

$filename = $foundfilename;

} else {

$filename = $filename."_" . $weirddate . "_".$modemdata.".csv";

echo "going to add to to file $datadir$filename </br>\n";

}

if ($fh = fopen("$moveto$filename",'a')) {

echo "doing write";

fwrite($fh,$linedata);

fclose($fh);

}

 

}

}

if (sizeof($filesnosplit) >0 ) {

foreach ($filesnosplit as $filename ) {

//echo "create $filename </br>";

logit("NOCHNG FILE: $filename");

 

$datafiles[]= $filename;

}

}

 

if (sizeof($filestoremove) >0 ) {

foreach ($filestoremove as $filename ) {

//echo "delete $filename </br>";

logit("DELETE FILE: $filename");

unlink("$datadir$filename");

}

}

}

 

if (sizeof($datafiles) > 0 ) { //file have been split and created now we import and run move them

foreach ($datafiles as $filename) {

logit("EXTRAC FILE: $filename");

echo "</br></br> Extracting $filename </br></br>";

 

    //extractinfo("$datadir$filename");

//copy("$datadir$filename","$moveto$filename");

//unlink("$datadir$filename");

}

}

$datetime = Date("Y-m-d H:i:s");

logit("~~|$datetime|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

 

function checkforsplit($datapath,$datafile,&$filestosplit) {

$tempdatafiles = array();

$rawdata=file_get_contents("$datapath$datafile");

 

$modemdata = split("_",$datafile);

 

$lines = split("\n",$rawdata); //split into lines

$numberoflines = sizeof($lines) ;

 

if ($lines[$numberoflines-1] == "") { //check if last element of array is blank

$lines = array_splice($lines,0,$numberoflines-1); // remove last element

}

 

if ((sizeof($lines)) >= 1) { // has extra array option

logit("SPLITN FILE: ".sizeof($lines). " lines found for $datafile" );

foreach ($lines as $line) {

$linedata = split(",",$line);

$probeid = $linedata[1];

$datetime = $linedata[2];

//$newfilename="G$probeid".'_'."$datetime.csv";

 

$newfilename="G$probeid?$modemdata[3]_$modemdata[4]";

$filestosplit[$newfilename].=$line."\n";

//$filestosplit[$newfilename]=$line;

}

return true;

} else {

logit("SINGLE FILE: 1 line found for $datafile");

return false;

}

 

return $tempdatafiles;

}

 

//function extractinfo($datafile) {

// global $cropdb;

// $rawdata=file_get_contents($datafile);

//

// $linedata = split(",",$rawdata); //split into lines

//

// $probeid = "";

// $ctime = "";

// $sms1 = "";

// $sms2 = "";

// $sms3 = "";

// $sms4 = "";

// $sms5 = "";

// $sms6 = "";

// $sts1 = "";

// $sts2 = "";

// $sts3 = "";

// $sts4 = "";

// $sts5 = "";

// $sts6 = "";

//

// $probeid = $linedata[1];

//

// //get the date into a unix epoch time

// $datetime = $linedata[2];

// $year = "20".substr($datetime,0,2);

// $month = substr($datetime,3,2);

// $day = substr($datetime,4,2);

// $hour = substr($datetime,6,2);

// $minute = substr($datetime,8,2);

// $ctime = mktime($hour, $minute, 0, $month, $day, $year) ;

//

// // soil moisture sensores

// $smsdata = $linedata[3];

// $smsdata = str_split($smsdata,4);

// $sms1 = $smsdata[0];

// $sms2 = $smsdata[1];

// $sms3 = $smsdata[2];

// $sms4 = $smsdata[3];

// $sms5 = $smsdata[4];

// $sms6 = $smsdata[5];

//

// $sql = "insert into probedata (probeid,ctime,sms1,sms2,sms3,sms4,sms5,sms6,sts1,sts2,sts3,sts4,sts5,sts6) values('$probeid','$ctime','$sms1','$sms2','$sms3','$sms4','$sms5','$sms6','$sts1','$sts2','$sts3','$sts4','$sts5','$sts6') ";

// logit("INSERT SQL : $sql");

//

// //echo $sql;

// $cropdb->insert($sql);

//

//

////site1.gprs.co.za/farm1

//

//}

//

//

function logit($log) {

if(defined("LOGFILE")) {

// file_put_contents(LOGFILE,"$log\n",FILE_APPEND);

if($fh = fopen(LOGFILE,'a')) {

fwrite($fh,$log."\n");

fclose($fh);

}

 

}

}

 

 

?>

 

As you can see, the newly created files are being moved to "/usr/home/aquach/farm1/".  Which I dont want,  if client 1 has probeid - G1046,G1061,G1059 then I want it to be moved to "/usr/www/users/aquach/client 1/" and if client 2 has probeid -G1071,G1062 then I want it to be moved to "/usr/www/users/aquach/client 2/". 

 

Can anybody help me with this?  Please email me at odium2008@hotmail.com if you need more information on this.

 

Thanks hope to hear from you guys soon

 

Link to comment
https://forums.phpfreaks.com/topic/112152-solved-file-moving/
Share on other sites

Please in future use the Code BB tags.

 

If you want to move them to different directories based on their ID, either get the ID from a database and store it in a variable and use a switch statement, or if you already have the dynamic ID's use a witch statement still.

 

switch($uid) {
  case 'G1061':
    $homeDir = 'client1';
    break;
  case 'G1059':
    $homeDir = 'client2';
    break;
  default:
    print 'Invalid user ID!';
    break;
}

Link to comment
https://forums.phpfreaks.com/topic/112152-solved-file-moving/#findComment-575768
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.