Solved

Php Question

Posted on 2010-09-11
9
330 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
 
LVL 5

Author Comment

by:Aanvik
ID: 33653977
Sorry.... That didn;t helped.
Error.JPG
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now