Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Php Question

Posted on 2010-09-11
9
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 84

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 84

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Author Comment

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

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

610 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