Solved

Php Question

Posted on 2010-09-11
9
348 Views
Last Modified: 2012-08-13
Hello,
I am trying to create a txt file (executable) from the php.. Its working fine but seems like some compile error... can anyone help?

The test file is the final output I am needing...

Line 30 to 63 in the php code is responsible to write this script output but having an error.
sftp-sendFiles.php
Test
0
Comment
Question by:Aanvik
  • 5
  • 4
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33653893
I believe most of your slash-n are being converted to linefeeds by PHP.  You have to escape the '\' you want to exist in your text like "\\" so it would be like "\\n" which you did on a couple of lines.  But not all.
0
 
LVL 5

Author Comment

by:Aanvik
ID: 33653904
can you pls correct it?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33653947
Try this.  I think I got them all.
<?php
require("/var/www/html/include/mailfunction.php");
include("/var/www/html/dbIncl.php");

$email = "aanvik@yahoo.com";
$email_subject = "SFTP Log";

$samba_rdy = "/var/www/html/HTInc/samba/Results/rdy/";
$samba_rdy_arch = "/var/www/html/HTInc/samba/Results/rdy/Archive/";

$batch_dir = "/var/www/html/HTInc/batch/";

$sftp_user = 'fts';
$sftp_password = 'fts';
$sftp_dst = '/home/fts/';

// check folder on new files
$arr_host = array();
if ( is_dir($samba_rdy) && ( $dh = opendir($samba_rdy) ) ) {
	while ( false !== ( $filename = readdir($dh) ) ) {
		if ( $filename == "." || $filename == ".." || substr($filename, -3, 3) != 'csv' ) continue;

		$arr_host[substr($filename, 15, 6)][] = $filename;
	}

	closedir($dh);
}
if ( count($arr_host) == 0 ) exit;

// create expect-script
$fh_main = fopen($batch_dir.'main', 'w+');
fwrite($fh_main, "#!/usr/bin/expect\n");
fwrite($fh_main, "set timeout 50\n");
$arr_copy = array();

	fwrite($fh_main, "spawn sftp ".$sftp_user.'@'.$host."\\n");
	fwrite($fh_main, "expect \\n");
	fwrite($fh_main, "expect {");
	fwrite($fh_main, "expect \"".$sftp_user.'@'.$host."'s password: \"  {send \"fts\n\"}");
    	fwrite($fh_main, "\"sftp> \" {}");
    	fwrite($fh_main, "-re Connecting {}");
    	fwrite($fh_main, "\n send \\n");
    	fwrite($fh_main, "}");

foreach ( $arr_host as $host => $files ) {

	foreach ( $files as $file ) {
		fwrite($fh_main, "expect \"sftp> \"\n");
		fwrite($fh_main, "send \"put ".$samba_rdy.$file." ".$sftp_dst."\\n\";\n");

		$arr_copy[] = $file;
	}

	fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"ls -1 ".$sftp_dst."\\n\";\n");
	fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"bye\\n\";\n");
	fwrite($fh_main, "expect eof");
}

fclose($fh_main);
chmod($batch_dir.'main', 0755);

// run sftp
$main_stdout = array();
exec($batch_dir.'main', $main_stdout);

var_dump($main_stdout);
// check files copied on remote host
$stdout_size = count($main_stdout);
for ( $i = 0; $i < $stdout_size; $i++ ) {
	if ( substr($main_stdout[$i], 0, 11) == 'sftp> ls -1' ) {
		unset($main_stdout[$i]);
		break;
	} else {
		unset($main_stdout[$i]);
	}
}

$arr_arch = array();
$str_offset = strlen($sftp_dst);
foreach ( $main_stdout as $line ) {
	if ( substr($line, 0, 5) == 'sftp>' ) break;
	if ( ( $key = array_search(substr($line, $str_offset), $arr_copy) ) !== FALSE ) {
		$arr_arch[] = $arr_copy[$key];
		unset($arr_copy[$key]);
	}
}

// copy files in achive folder
foreach ( $arr_arch as $file ) {
	copy($samba_rdy.$file, $samba_rdy_arch.$file);
	unlink($samba_rdy.$file);

	//doquery($dblink, "DELETE FROM sftp_log WHERE filename='$file'");
}

if ( count($arr_copy) == 0 ) exit;

// write logs about not copied files
$arr_files = array();

$res = doquery($dblink, "SELECT filename, count(*) FROM sftp_log GROUP BY filename");
while ( ( $log_rec = dofetch($res) ) ) {
	if ( ( $key = array_search($log_rec[0], $arr_copy) ) !== FALSE && ( (int) $log_rec[1] ) == 4 ) {
		$arr_files[] = $log_rec[0];
		unset($arr_copy[$key]);
	}
}

//foreach ( $arr_copy as $file ) doquery($dblink, "INSERT INTO sftp_log (filename) VALUES ('$file')");

// send message on email
if ( $email != '' && count($arr_files) > 0 ) sendEmail($email_subject, 'Did not sent files: '.implode(", ", $arr_files), $email);
?>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Author Comment

by:Aanvik
ID: 33653977
Sorry.... That didn;t helped.
Error.JPG
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33654025
Try changing line 42 to this.
fwrite($fh_main, "\\n send \\n");

Open in new window

0
 
LVL 5

Author Comment

by:Aanvik
ID: 33654044
same error
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33654196
Ok, this test file generates the same text as your 'Test' file above.  I can't test the whole file since I don't have all the rest of the resources.  But you should be able to First: test this file and see that it generates the right text and Second: use it to fix that part of the main file.
<?php
$samba_rdy = "/var/www/html/HTInc/samba/Results/rdy/";
$samba_rdy_arch = "/var/www/html/HTInc/samba/Results/rdy/Archive/";

$sftp_user = 'fts';
$sftp_password = 'fts';
$sftp_dst = '/home/fts/';
$host = 'jlc031';
$file = 'AQR01jlc_00031_jlc031_0138_DELETE-DELETE.csv';

// create expect-script
$fh_main = fopen('sftp-test2.txt', 'w+');
fwrite($fh_main, "#!/usr/bin/expect\n");
fwrite($fh_main, "set timeout 5\n");
fwrite($fh_main, "spawn sftp ".$sftp_user.'@'.$host."\n");
fwrite($fh_main, "expect \\n\n");
fwrite($fh_main, "expect {\n");
fwrite($fh_main, "\"".$sftp_user.'@'.$host."'s password: \" {send \"fts\\n\"}\n");
fwrite($fh_main, "\"sftp> \" {}\n");
fwrite($fh_main, "-re Connecting {}\n");
fwrite($fh_main, "\\n {send \"No idea\\n\"}\n");
fwrite($fh_main, "}\n\n");

fwrite($fh_main, "expect \"sftp> \"\n");
fwrite($fh_main, "send \"put ".$samba_rdy.$file." ".$sftp_dst."\\n\"\n");


	//fwrite($fh_main, "expect \"sftp> \"\n");
	//fwrite($fh_main, "send \"ls -1 ".$sftp_dst."\\n\";\n");
	//fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"bye\\n\"\n");
	fwrite($fh_main, "expect eof\n");

fclose($fh_main);

?>

Open in new window

0
 
LVL 5

Author Closing Comment

by:Aanvik
ID: 33654474
Thank You
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33654486
You're welcome.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question