system call to gnuplot from php to generate plot? - php

So, I am using a php program to read a file, make some changes and then write it to a new file. After that, I call gnuplot, using a system call:
system('cat sarx.conf | /usr/bin/gnuplot');
sarx.conf has the gnuplot commands to generate the plot. The problem is if run my php from the command line (its on a linux server) it generates the image and stores it on the disk. But when I do the same thing by running the php on my browser it generates the image and tries to spit it out on the browser without actually storing it on disk.
Things i tried:
I though i might have had issues with permission settings but it didn't help.
I also hard coded the path where I want the image to be in sarx.conf. That didn't help either.
I also tried looking for it in the tmp directory --- no luck!!
Does anyone have any ideas on how can I get this to work? I need to store this image on disk so that my website can grab it to show the plot later. Is there any php stuff which can grab the image and write it to disk?

There is a great LGPL-licensed PHP interface to gnuplot here: http://www.liuyi1.com/PHP-GNUPlot/
Here is how you could do something similar:
$my_file = tempnam();
$handle = popen('gnuplot', 'w');
fwrite($this->ph, "Run some gnuplot commands here\n");
fwrite($this->ph, "set term png\n");
fwrite($this->ph, "set output ".$my_file."\n");
fwrite($this->ph, "replot\n");
flush($handle);
pclose($handle);
header('Content-Length: '.filesize($my_file));
header('Content-Type: image/png');
print file_get_contents($my_file);
unlink($my_file);

Related

Uploading a PHP generated CSV file to SFTP server via terminal

I have a PHP file which generates a CSV file using the code below:
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
I've not included the code which creates the content as it does it's job fine. What I need to do is use terminal to run this file and upload the results to an sftp server. I can connect to the server fine in terminal.
I have been using the php command in terminal to produce the resulting CSV. This however doesn't produce the CSV like it does when run in the browser. What it does do is produce the CSV as a string.
Is there a way to either produce the CSV as a file like the browser so I can grab it in terminal and upload it to the SFTP server? Alternatively is it possible to echo out the string produced from the PHP file and create the CSV myself using this kind of command:
echo "boo,to,you">file.csv
Just redirect the php output to a temporary file and upload the file.
php yourscript.php > /tmp/file.csv
echo "put /tmp/file.csv" | sftp user#example.com
If you want to do it without the temporary file, you cannot do with just the (OpenSSH) sftp as it cannot read contents to upload from the stdin. Neither the (OpenSSH) scp can.
You can of course produce the file from PHP code itself, if that suits your task better.
Just use the file_put_contents():
file_put_contents("/tmp/file.csv", $csv_output);
See How to write into a file in PHP?
The suggestion by #MarcB should work, if the remote server is *nix (understands the cat) and allows a shell access.
php yourscript.php | ssh user#example.com 'cat > file.csv'
Though obviously that's not "SFTP upload" anymore.
When producing a file, do not use the the header(). Headers make sense in webserver environment only, not when you use the PHP as a scripting language in a shell.

PHP Read/Write files on remote server

I'm making a utility that provides a GUI to easy edit certain values in a csv file on a remote server. My boss wants the utility in php running on the private webserver. I'm new to php, but I was able to get the GUI file modifier working locally without issues. The final piece now is rather than the local test file I need to grab a copy of the requested file off of the remote server, edit it, and then replace the old file with the edited one. My issue is uploading and downloading the file.
When I searched for a solution I found the following:
(note in each of these I am just trying to move a test file)
$source = "http://<IP REMOTE SERVER>/index.html";
$dest = $_SERVER['DOCUMENT_ROOT']."index.html";
copy($source, $dest);
This solution ran into a permissions error.
$source ="http://<IP REMOTE SERVER>/index.html";
$destination = $_SERVER['DOCUMENT_ROOT']."newfile.html";
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
This also had a permissions error
$connection = ssh2_connect('<IP REMOTE SERVER>', 22);
ssh2_auth_password($connection, 'cahenk', '<PASSWORD>');
ssh2_scp_recv($connection, '/tmp/CHenk/CHenk.csv', 'Desktop/CHenk.csv');
This solution has the error Fatal error: Call to undefined function ssh2_connect() which I have learned is because the function is not a part of the default php installation.
In closing, is there any easy way to read/write files to the remote server through php either by changing permissions, having the php extension installed, or a different way entirely that will work. Basically I'm trying to find the solution that requires the least settings changes to the server because I am not the administrator and would have to go through a round about process of getting any changes done. If something does need to be changed instructions on doing so or a link to instructions would be greatly appreciated.
Did you set the enable-url-fopen-wrapper in your php.ini?(only if your php version is older)
Please look # php remote files storing in example 2

PHP curl fails to download data when outputting to a file but ok when printing to stdout

This has been bugging me for literally hours already.
I can't seem to figure out why PHP cURL won't download data to a file. (CURLOPT_FILE is set to a local file.) I am not getting any data. I periodically check the file size of the destination file and it is always zero. To give you a background, I am downloading a 90kb jpeg file (for testing purposes).
This is working on my local computer (XP) but not in the website I am working on (Windows Server 2003).
I did several tests which made the scenario even weirder.
I disabled CURLOPT_FILE to print the data returned by curl into standard output, and the binary data printed.
Having experienced blocked websites before (since the server implements access control), I tried accessing the file from internet explorer and i was able to see it.
Having experienced blocked downloads before, I tried downloading the file from internet explorer and it was downloaded.
The file is created by fopen('', 'w') but the size remains 0. Despite this successful file creation, I thought maybe PHP has a problem with filesystem write privileges, I set the exe to be run even by non-admin users. Still no download.
Has this ever occured to anybody?
Any pointers will be appreciated. I am really stuck.
Thank you.
Here's the curl options I set:
$connection = curl_init($src);
// If these are not set, curl_exec outputs data.
// If these are set, curl_exec does not send any data to the file
// pointed to by $file_handler. $file_handler is not null
// because it is opened as write (non-existing file is created)
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $connection, CURLOPT_FILE, $file_handler );
PS: I'm doing these tests using the command line and not the browser.
you might not have permissions to write to the file.
I don't think you have to set CURLOPT_RETURNTRANSFER and if you are running from the command line be sure to run the php with admin rights. Not sure how it works in windows but in linux I always sudo every command line script I run.
Also if php safe mode is on be sure to also give the the directory the same (UID) owner as the php file. Hmh but since you can create the file (with 0 filesize) it might have nothing to do with rights... could you check the *open_basedir* php setting on your server? If it is set cUrl is not allowed to use file protocol... did you check the log files from your server? maybe there is an error.
You may need to figure out what user runs your php, if the user running the php script (the one that calls php ) is not authorized to write to the directory of the file, or to the /path/to/file , you may need to adjust your file permissions.

PHP: Problem using passthru to stream a zip on mac os x only

I'm trying to put together a zip streaming solution through the use of Unix's zip command and PHP's passthru function, but I've hit a snag.
The script looks something like this:
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachement; filename=myfile.zip");
passthru("zip -r -0 - /stuff/to/zip/");
exit();
?>
The zip command works OK and the output is received by the browser and saved as a zip file.
The zip can then be extracted fine on Windows and Unix, but on Mac OS X the build in extractor (BOMArchiveHelper) can't extract the file. Using other applications on OS X works fine though.
The error given by BOMArchiveHelper is the same it gives if a zip is password protected (not handled by the application). I used some kind of zip analyzer program and it indicated that some of the files in the zip archive were flagged as password protected.
Like I said though, no other extraction application pays attention to that apparently.
When examening the zip closer I found that the one generated by the PHP files is a few bytes larger than one generated directly by the zip command on the server.
It seems that the stream process with passthru adds something to the file that probably causes the problems with BOMArchiveHelper.
To test this, I used passthru to stream a zip I had already created on the server: passthru("cat stuff.zip")
That worked fine with BOMArchiveHelper.
So the problem seems to lie somewhere in the process where the passthru function takes the binary data generated on the fly by the zip command and passes that to the browser.
I've tried to eliminate all the sources where the extra bytes could be generated (setting zip command to quiet and so on), but the added data still remains.
A binary diff of the streamed zip and a pre generated zip shows that the extra data is scattered all over the zip, not just in the end or the beginning.
Anyone have a clue, or seen this problem before and decided it's impossible to solve?
NB: Since someone else has already encountered and very well described this issue before me without any answer I just copied/paste his message here and made sure that all his tests did effectively fail and neither any of mine passed ...
Apparently the only way to get this to work would be to ask people to use either unzip or suffitexpander ...
If you are using nginx then take a look at http://wiki.nginx.org/NginxNgxZip

Opening a notepad from PHP

i want to open a notepad from php file and the notepad should contain the text which i declare as string in php file. essentially a notepad should open with the text I pass from php file.
If the PHP file is executing on a web server you cannot cause a web browser to open a new process like that. I'm sure you can imagine what a security hole that would be!
If you're running a PHP file as a local script in CLI mode, you should be able to launch notepad like any other process, e.g. using backticks or exec etc.
However, if you really wanted to do this server side, the best you could do is have a PHP script which used a Content-Disposition header, e.g.
//tell client we're delivering text
header('Content-type: text/plain');
//hint that it's a downloadable file
header('Content-Disposition: attachment; filename="textfile.txt"');
//output our text
echo "The quick brown\nfox jumps over\nthe lazy dog.";
The user can then save this file and open in their editor of choice.
You can't get PHP to open a window on the user's machine, because PHP is run entirely on the server. By the time the output reaches the browser the script will generally have terminated - you can only do what you can ask the browser to do and what it will let you (using HTML / headers etc.). For security purposes the browser will not (or should not) let an arbitrary website do very much with your machine - e.g. it will not let you spawn new Windows processes.
The best I think you could do is something like this:
$string = 'a string';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="file.txt"');
echo $string;
This will send the relevant headers so that the browser will treat the content as a download called file.txt of type plain text. The browser should prompt them to download a file which will be likely to open in notepad, unless they have changed the file association for .txt .
However you won't be able to get any changes back that the user makes to the document unless you ask them to upload it, so I'm not sure this is a good solution to what you are trying to acheive.
It is possible to execute a program from php but only server-side.
So imagine the server runs Windows, it would start notepad server-side.
PHP gets executed on the server, and has nothing todo what's running client-side.
Technically, to do this you'd have to create a file and then execute a system with that file as a parameter. Something like this:
//String to show in notepad
$myStringToDisplay = "some text to show in notepad";
//Write this string to a file
$myFile = "somefile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $myStringToDisplay);
fclose($fh);
//Execute notepad with this file as a parameter
system("notepad.exe ".$myFile);
However, this is going to execute Notepad on the server running the PHP file (if system calls are even enabled on your server), which is probably not what you want to do. PHP cannot execute any code on the client machine, and certainly cannot make a system call to execute any program it wants on the client (thank god). It would be a huge, huge security breach.
If this does not accomplish the desired functionality, please tell us what you're trying to do and why. This does not sound like a very sensible request.
It is not possible to open a program from your php application. But you ca load the text file using a PHP text editor. You will also be able to load the values which you are talking about.
http://www.fckeditor.net/ is one such editor.
First you can "create" the file and fill it with text.
Execute shell command: echo $text >> $filename
then execute: notepad $filenameToOpen
Thats it.
To open a notepad from php script we will use command line inter phase.
Firstly we will create one php file in that we will write:
var_dump(popen('notepad','r'));
Then we will save that with some name like notepad.php
then open command prompat there we will give the path of our file to run our file
like:
d:/>wamp>www>php notepad.php
It will run our php file and it will oopen notpad.

Categories