I use function system() to run system process.
$buff = system('python excel.py ' . $handle->file_dst_pathname, $retval);
It displays messages in line without separations.
In Python I use this line to print data:
print "# %d - article \"%s\" was inserted!" % (i, article)
Precede the output of system(...); with echo "<pre>"; and follow with echo "</pre>";
Or shorter:
$buff = "<pre>" . system('python excel.py ' . $handle->file_dst_pathname, $retval) . "</pre>";
You could also change all the "\n" chars of your output into "<br/>".
Related
I am working on a PHP code as shown below in which conversion of mp4 into mp3 is happening at Line B.
I have added if block after system command to print Conversion Completed on the webpage once the conversion is complete but it doesn't seem to work.
Php code:
if (isset($_POST['id']))
{
for($i=0; $i <count($mp4_files); $i++) {
if($i == $_POST['id']) {
$f = $mp4_files[$i];
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
print_r($f); // Line A
system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result); // Line B
if($result)
{
echo "Conversion Completed";
}
}
}
}
}
Problem Statement:
I am wondering what changes I should make in the PHP code above so that once the conversion is complete; on the webpage, it should print Conversion Completed.
You can use shell_exec to get a return value and then put that in an if statement like this
$output = shell_exec('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3');
if ($output) {
echo "Conversion Completed!";
// or redirect here
}
Also, make sure to sanitize your inputs as they are exposed to a CLI interface.
I'm trying to run a python script from a PHP file. for that I'm using system() function of PHP.
system('ls -l', $retval) is working as expected but system('python CMI.py', $retval) is not giving the desired results. The python script CMI.py is running perfectly fine when run from terminal.
I will give the code snippets and outputs so that there will be a clear picture of permissions.
<?php
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls -l', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('python CMI.py', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
This is the output in the browser
I've implemented a file optimization script which takes about 30 seconds or more. There's a loop in which I added echos to track what's being processed.
However, most of the time, no output is being sent, until the end of the process.
How can I control this in order to send the echos in an iteration just as they're being finished?
EDIT:
This is the implied code, with the output buffer functions:
set_time_limit(60);
ini_set("memory_limit", "256M");
$documents = $this->documents_model->get($date1, $date2);
ob_start();
echo '-- Start ' . "<br>\n";
ob_end_flush();
flush();
foreach ($documents as $document) {
ob_start();
echo '-- Processing document ' . $document->id . "<br>\n";
$file = $document->get_file_name();
if (! $file || ! file_exists(DOCUMENT_ROOT . 'documents/' . $file)) {
echo '---- Document ' . $document->id . " has no PDF file yet or it was deleted<br>\n";
$path = $this->documents_model->generatePDF($document);
echo '------ file generated: ' . $path;
}
ob_end_flush();
flush();
}
echo '-- End ' . "<br>\n";
The reason why you will get the output after the processing finishes, is php's output buffer and eventually your webserver's buffer.
You will have to flush these buffers with the invokation of ob_flush() and flush() after every few echo statements.
For more technical information see PHP buffer ob_flush() vs. flush()
I'm trying this
<?php
/* read the PHP source code */
$source_code = file_get_contents("hello.php");
$source_code = preg_replace('#^<\?php\s+#', '', $source_code);
$source_code = preg_replace('#\s+\?>\s*$#', '', $source_code);
/* create the encrypted version */
$redistributable_key = blenc_encrypt($source_code, "encrypt.php", "my_fixed_password");
$key_file = __DIR__ ."\keys";
file_put_contents($key_file, $redistributable_key . "\n", FILE_APPEND);
include 'encrypt.php';
echo $hello;
?>
hello.php
<?php
$hello = "Ciao";
I got this error
PHP Fatal error: blenc_compile: Validation of script
'encrypt.php' failed, cannot execute.
Please note that:
The key file is created, I'm already using the '\n' fix
I replaced <?php and ?> because another Stack Overflow question told me that it's a problem
<?php
$file_name = basename($file);
$unencrypted_key = = md5(time());
$source_code = file_get_contents($file);
//This covers old-asp tags, php short-tags, php echo tags, and normal php tags.
$contents = preg_replace(array('/^<(\?|\%)\=?(php)?/', '/(\%|\?)>$/'), array('',''), $source_code);
$html .= "<br> BLENC blowfish unencrypted key: $unencrypted_key" . PHP_EOL;
$html .= "<br> BLENC file to encode: " . $file_name . PHP_EOL;
//file_put_contents('blencode-log', "---\nFILE: $file_name\nSIZE: ".strlen($contents)."\nMD5: ".md5($contents)."\n", FILE_APPEND);
$redistributable_key = blenc_encrypt($contents, TARGET_DIR . '/blenc/' . $file_name, $unencrypted_key);
$html .= "<br> BLENC size of content: " . strlen($contents) . PHP_EOL;
/**
* Server key
* key_file.blenc
*/
file_put_contents(TARGET_DIR . '/blenc/' . 'key_file.blenc', $redistributable_key . PHP_EOL);
$html .= "<br> BLENC redistributable key file key_file.blenc updated." . PHP_EOL;
exec("cat key_file.blenc >> /usr/local/etc/blenckeys");
?>
https://github.com/codex-corp/ncryptd/blob/master/app/controllers/MagicalController.php#L479
you should put the key to
blenckeys
file on your server
Note: Sometimes you need to reload the apache, if you have "Validation of script" issues
How to use BLENC in PHP?
When I use file_get_contents and pass it as a parameter to another function, without assigning it to a variable, does that memory get released before the script execution finishes?
For Example:
preg_match($pattern, file_get_contents('http://domain.tld/path/to/file.ext'), $matches);
Will the memory used by file_get_contents be released before the script finishes?
The temporary string created to hold the file contents will be destroyed. Without delving into the sources to confirm, here's a couple of ways you can test that a temporary value created as a function parameter gets destroyed:
Method 1: a class which reports its destruction
This demonstrates lifetime by using a class which reports on its own demise:
class lifetime
{
public function __construct()
{
echo "construct\n";
}
public function __destruct()
{
echo "destruct\n";
}
}
function getTestObject()
{
return new lifetime();
}
function foo($obj)
{
echo "inside foo\n";
}
echo "Calling foo\n";
foo(getTestObject());
echo "foo complete\n";
This outputs
Calling foo
construct
inside foo
destruct
foo complete
Which indicates that the implied temporary variable is destroyed right after the foo function call.
Method 2: measure memory usage
Here's another method which offers further confirmation using memory_get_usage to measure how much we've consumed.
function foo($str)
{
$length=strlen($str);
echo "in foo: data is $length, memory usage=".memory_get_usage()."\n";
}
echo "start: ".memory_get_usage()."\n";
foo(file_get_contents('/tmp/three_megabyte_file'));
echo "end: ".memory_get_usage()."\n";
This outputs
start: 50672
in foo: data is 2999384, memory usage=3050884
end: 51544
In your example the memory will be released when $matches goes out of scope.
If you weren't storing the result of the match the memory would be released immediately
In following code memory usage = 6493720
start: 1050504
end: 6492344
echo "start: ".memory_get_usage()."\n";
$data = file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
but memory usage in following code = 1049680
start = 1050504
end = 1050976
echo "start: ".memory_get_usage()."\n";
file_get_contents("/six_megabyte_file");
echo "end: ".memory_get_usage()."\n";
Note: in first code file stores in a variable.
If you think this will help in avoiding insufficient memory errors you are wrong. Your code (bytes_format):
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
preg_match('~~', file_get_contents($url), $matches);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB:
Before: 215.41 KB
After: 218.41 KB
Peak: 10.5 MB
and this code:
<?php
$url = 'http://speedtest.netcologne.de/test_10mb.bin';
echo 'Before: ' . bytes_format(memory_get_usage()) . PHP_EOL;
$contents = file_get_contents($url);
preg_match('~~', $contents, $matches);
unset($contents);
echo 'After: ' . bytes_format(memory_get_usage()) . PHP_EOL;
echo 'Peak: ' . bytes_format(memory_get_peak_usage(true)) . PHP_EOL;
?>
uses 10.5 MB as well:
Before: 215.13 KB
After: 217.64 KB
Peak: 10.5 MB
If you like to guard your script you need to use the $length parameter or read the file in chunks.