Solved

Php Question

Posted on 2010-09-11
9
357 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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