PHP ssh2_scp_send remote file name enclosed in single quotes - php

I'm writing a code to SCP a file from Solaris 10 machine to a router (couldn't be simpler).
Here is the code:
<?php
$src = "test.txt";
$trg = "test.txt";
echo "Connecting...\n";
$connection = ssh2_connect('myrouter',22);
echo "Authenticating...\n";
ssh2_auth_password($connection, 'mylogin', 'mypassword');
echo "Sending...\n";
ssh2_scp_send($connection, $src, $trg);
?>
This works just fine but the remote filename is screwed up:
10/11/2012 10:57p 15 'test.txt'
Note the single quotes around the file name test.txt.
When I use Solaris SCP everything works fine. Here is the command:
solaris10$ scp test.txt mylogin#myrouter:test.txt
The remote file name looks like:
10/11/2012 11:10p 15 test.txt
So the problem seems to be limited to PHP.
Did anybody have similar issue? Where should I start looking?
The remote filesystem is FAT32.
Thank You,
PN.

I had the same problem when using ssh2_scp_send(). However, I switched to using ssh2_sftp() and fwrite() and the problem went away. See examples in the comments on the http://www.php.net/manual/en/function.ssh2-sftp.php page.

The bottom comment on this page http://www.php.net/manual/en/function.ssh2-scp-send.php refers to a problem you might be having;
Can you set $trg to the full remote server path?
$trg = "/var/www/test.txt";
or
$trg = "/test.txt";

Related

file_get_contents on local wamp server not working

I simply cant wrap my head around why this simple code won't work on my local wamp server where i have other sites running.
<?php
$file = file_get_contents('C:\Users\Computer\input.txt', true);
echo $file;
?>
The file is where it should be.
I get the following error:
Warning: file_get_contents(C:\Users\Computer\input.txt): failed to open stream: No such file or directory in C:\Users\Computer\Dropbox\htdocs\ny\www\php\day2\code.php on line 3
But the file is exactly where it should be.
The file has all possible permissions to true (read, write ect.) in windows except for "special permissions".
What is not working correctly?
Regards,
Patrick
The solution was the extension. It was not correctly parsed. This was checked by using #VolkerK suggesting to check
Does the output of var_export(glob('C:\Users\Computer*')); contain
input.txt? Or is it false/null?
Thanks to #VolkerK for the help solving the problem.
Since I incorrectly said that it might be because of backslash escaping, I guess I need to redeem by saying something useful :)
So try:
$file = 'C:\Users\Computer\input.txt';
if(is_file($file))
{
$string = file_get_contents($file);
}
else
{
die("Uh oh! Seems like $file has got problems!");
}
Also, you are passing the second argument as true for searching the include path. Since your filepath is absolute, I suspect if you need this argument to be passed at all.

accessing text file from client getting different data than accessing it on server

I have this code on the server
<?php
$file = fopen("data.txt","w+") or die("Couldn't locate file");
fwrite($data);
fclose($file);
?>
and this one on the localhost on my local machine
<?php
$file = fopen('http://x.x.x.x/data.txt','r');
echo fgets($file);
fclose($file);
?>
The problem I face - and haven't been able to figure out - is: whenever I change the data in the text file on the server, it takes a while to read the change on localhost side.
Now the database option had a huge delay so it's not an option and i have tried mostly everything i can think of, so i was wondering what i am doing wrong and if their is a better way.
Well i dont know what was the problem but it worked for some reason, maybe something was wrong with the server itself.

PHP fwrite() not working

I'm writing a function in php, client side I have a canvas image which I use toDataUrl() along with a file name to save the image on the server. The here's the code:
<?php
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$data = json_decode($imageData, true);
$file = $data["file"];
$image = $data["data"];
$filteredData=substr($image, strpos($image, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'image/' . $file , 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
?>
The thing is that this code works. And for two out of three of the pages I used it on it works fine. The problem is when I copy and pasted it a third time to implement it again, for some reason the file is made on the server except that no data get's written into the file. I don't think it's a problem client side because I write in a debug alert message in the javascript and a debug echo into the PHP and both are able to print out the data fine. I made this short debug file:
<?php
$fp = fopen('data.txt', 'wb');
if(is_writable('data.txt')){
echo "file is writable<br>";
}
if(fwrite($fp, 'test') == FALSE){
echo "failed to write data<br>";
}
fclose($fp);
?>
And the output is
file is writable
failed to write data
I've tried using chmod and setting everything, the folder, the text file before I write to it to 0777 and I still get the same result; the file is made but no data is written into it. Is there anything I'm missing or any other approaches that might help. I haven't found anything on google and am still baffled as to why the same code worked exactly as expected twice before suddenly stopping for no apparent reason.
Thanks in advance.
I know this is an old post, but I had a very similar problem and found a solution (for me at least)! I ran out of disk space on my server, so it could create a 0 byte file, but wouldn't write to it. After I cleared out some space (deleted a 13gb error.log file) everything started working again as expected.
If fopen works but fwrite mysteriously doesn't, check your disk space. 'df -h' is the command to check disk space on a linux server.
instead of $fp = fopen('data.txt', 'wb'); give $fp = fopen('data.txt', 'w'); and try
Changed "wb" to "w"
When you write $fp = fopen('data.txt', 'w'); for your domain website.com having root at /var/www/website/ and if the php file is located at /var/www/website/php/server/file/admin.php or something similar, it will actually create a file at /var/www/website/data.txt
Try giving absolute path or path relative to your domain root to create files like,
$fp = fopen('php/server/file/data.txt', 'w');
Try the find command to see if the file is created anywhere else in the folder directory by using the following in Ubuntu,
find /var/www/website/ -name 'data.txt'
I had this issue, probably can help you solve if you have similar issue.

Server side printing in PHP 5

How can i print my html file via php script? I just want to run it in background without any prompt. I have read other posts regarding this but still didnt find anything working.
I tried this one :
<?php
$dir = "temp"; // the folder that you are storing the file to be printed
$file = "file.html"; //change to proper file name
$file_dir = $dir.$file;
$server = "home_computer"; //name of the computer you are printing to on your network
$printer = "HP"; //printers shared name
$command = "print $file_dir /d:\\$server\\$printer";
exec($command) or die("File failed to print");
?>
got this example here http://www.phpfreaks.com/forums/index.php/topic,207946.0.html
here is what i got working :
$html = "testing print";
$handle = printer_open();
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, $html);
printer_close($handle);
We require php_printer.dll php extension to make this work in php5. :)
You can't print html pages with php. Php is a server-side language, it runs on the server.
The printer is on the client's machine. Meaning you'll need a client-side language to accomplish this.
If you want to print the source code then it should be be doable by writing a program that prints the passed string and then calling it via a system call. On windows it appears to have an extension for that.
If you want to print a rendered version, then you have to know that for that you need some kind of a rendering engine. While not impossible its probably more work than what you want to get into.

Gettin error when running simple php script

I'm trying to run this simple script:
<?php
$PHP_PATH = "c:\Program Files (x86)\PHP\\";
$CLIENTPATH = dirname(__FILE__)."\Client.php";
$SERVER = "http://localhost:8080/mobile/Server";
$handle = popen("C:\WINDOWS\system32\cmd /C start ".$PHP_PATH." -f ".$CLIENTPATH." ".$SERVER, 'r');
?>
But I always get this Windows messagebox error:
Windows cannot find c:\program. Please make sure you typed the name correctly and then try again.
Searching on google I also find this thread about this error, but the meassures are I litlle drastic I guess.
So the problem its in my code ? Or there can be something else wrong ?
Thanks.
You need to escape the whitespaces in $PHP_PATH = "c:\Program Files (x86)\PHP\\";!

Categories