output from GDAL ogr2ogr - php

I want to convert a .shp file to .geojson format using ogr2ogr, which I can do from the (linux) command line.
However, rather than a command line conversion, I want to invoke the conversion from a php script (using e.g. exec(..) ) and send the output directly to a variable (as a character string) instead of writing to a file.
Is this possible?

You can use shell_exec() method available in PHP.
public function shptogeojson($shpfilepath,$output,$srsno,$srsold){
$query="ogr2ogr -f GeoJSON -t_srs EPSG:$srsno -s_srs EPSG:$srsold $output.shp $shpfilepath";
shell_exec($query);
}

Related

How to execute C code through PHP by prompting terminal

I have a C code that I have to execute through PHP,
I have used exec('./sys'), sys is my executable file.
I have also tried system(), passthrough(), shell_exec() and they are not giving output.
When I executed exec('who'); it gives the output.
What can I do to execute sys?
Each of those methods you reference will execute your sys file, but you need to make sure you are executing the correct path. Your working path is determined by what script is actually executing PHP. For example, if you're executing your code through apache or the command line your working directory may be different. Lets assume this file structure:
+ src/
| + script.php
| + sys
I would recommend using PHP's __DIR__ magic variable in your script.php to always reference the current file's directory, and then work from there:
// script.php
exec(__DIR__ . "/sys");
Retrieving output can be done a couple different ways. If you want to store the output of the script in a variable, I would recommend using exec according the the manual:
Parameters ΒΆ
command
The command that will be executed.
output
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
exec will return the first line of output, but if you want more than that you need to pass a variable by reference:
// script.php
$output = array();
exec(__DIR__ . "/sys", $output);
$output will then contain an array of each line of output from the command. However if you want to run your sys script and directly pass through the output then use passthru(__DIR__ . "/sys"); For example, if you wanted to execute a command that required input on the command line, passthru would be the best option.

PHP system() keeps echo even though

So I need to found out if an upload from a user is images/ video and what type. I currently use
"filetype"=>system("file -i -b ".$_FILES['file']['tmp_name'])
which is inserted into my MongoDB collection via this
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
$collection = static::db()->media;
$datetime = time();
$mediaupload = array("owner"=>$_SESSION['user_information'][0]['_id'],"filelink"=>$s3file,"filetype"=>system("file -i -b ".$_FILES['file']['tmp_name']),"filesize"=>$size,"uploadtime"=>$datetime,"ownerid"=>$_SESSION["user_information"][0]['_id']);
$collection->insert($mediaupload);
$media = $collection->findOne($mediaupload);
However what I am noticing is it echos the result out to the PHP page - which is not what I need it to do. i know it is the system function because when I remove that function it does not echo the uploaded file type to the php code.
I am wondering therefor how can i still run that system file -i -b function and get it to include into the MongoDB database but not echo the result into the public php page return.
Try something like (for multi-line output)
exec("file -i -b ".$_FILES['file']['tmp_name'], $output);
array("filetype"=>$output);
It may look a little unorthodox, but exec uses its second input parameter as a way to pass the output information back to you - the output of file will be stored as an array into $output.
From the docs:
If the output argument is present, then the specified array will be
filled with every line of output from the command. Trailing
whitespace, such as \n, is not included in this array. Note that if
the array already contains some elements, exec() will append to the
end of the array. If you do not want the function to append elements,
call unset() on the array before passing it to exec().
If you simply want the first line from the output, use the simpler version:
array("filetype"=>exec("file -i -b ".$_FILES['file']['tmp_name']));
I have switched the system to exec() and that seems to of fixed my issue

How to pass arguments in php exec()?

I am trying to run a .exe application with input file and argument.
With cmd I can successfully start the executable like this...
C:\Program Files\MyApp.exe "path\to\input file" argument
However, nothing happens when I simply copy paste the string above into the exec() function like this..
exec("C:\Program Files\MyApp.exe "path\to\input file" argument")
Do I need to escape parts of the string? How should I proceed?
Just pass the arguments like a normal calling from shell
ex:
exec("C:\Program Files\MyApp.exe \"path to\input file\" argument")
I had to use this format
php -q "./yii.php" migrate/up --interactive=0 --migrationPath=#vendor/pheme/yii2-settings/migrations

Using PHP to execute cmd commands

How do I properly execute commands in the command line using php? For example I'm using the command below in the command line to convert a docx file into a pdf file:
pdfcreator.exe /PF"D:\Documents\sample.docx
Now using PHP code I want to be able to execute the same command but nothing seems to be happening:
<?php
shell_exec('pdfcreator.exe /PF"D:\Documents\sample.docx"');
?>
Is this possible in PHP?If yes, how do I do it?
system("c:\\path\\to\\pdfcreator.exe /PF\"D:\\Documents\\sample.docx"");
try this.
Don't forget to escape your command with escapeshellcmd(). This will prevent you from having to use ugly backslashes and escape characters.
There are also other alternatives which may work:
`command` // back ticks drop you out of PHP mode into shell
exec('command', $output); // exec will allow you to capture the return of a command as reference
shell_exec('command'); // will return the output to a variable
system(); //as seen above.
Also, make sure your .exe is included within your $PATH variable. If not, include the full path for the command.

want to run c program from php using exec() function

i'm trying to run one c executable file using php exec().
When c contains a simple program like print hello. I'm using
exec('./print.out')
It's working fine. But when I need to pass a argument to my c program I'm using
exec('./arugment.out -n 1234')
It is not working. Can any body tell me how to pass arugment using exec to c program.
From taking a look at the php documentation, it appears that exec treats arguments a bit oddly. You could try doing
exec("./argument.out '-n 1234'")
to prevent it from mangling them (it normally separates them all on space, which might be what's messing it up).

Categories