Posts Tagged ‘php’

Execute a console command in PHP revised

Friday, September 3rd, 2010

While it seems like there are multitude of different methods to execute commands in PHP, so far the code I will go over below is my preferred method. Using the passthru() function works nicely and includes the final return code of whatever was being executed, which is generally useful to me in error checking. In processing, I also include ob_start(), ob_get_contents(), and ob_end_clean() so that I can capture the console programs output and return it the user eventually.

//execute a console command and return both the commands
//success value and any output to the console it made
function executeConsoleCommand($commandToExecute, &$commandConsoleOutputToAppend)
{
	$returnValue = 0;

	$commandConsoleOutputToAppend .= $commandToExecute . "\r\n";

	//catch console output
	ob_start();

	//execute the console program
	passthru($commandToExecute, $returnValue);

	//save console output to a string variable
	//(this should APPEND to the variable)
	$commandConsoleOutputToAppend .= ob_get_contents() . "\r\n";
	ob_end_clean();

	//add this data to the log file
	updateLogFile($commandConsoleOutputToAppend, "a");

	return $returnValue;
}

Take note that in my function, I append all of the console output to a parameter variable using pass by reference. The console program’s return code is passed back through the standard return variable of the function.

.NET Path.Combine() in PHP

Wednesday, September 1st, 2010

I’m the type of person who prefers to do things in a way that I perceive as the proper or correct way. While this tends to make things take a bit longer to accomplish, I feel the result ends up being better overall. One such thing I noticed is that file and path handling in PHP was missing a function I used quite a bit when writing C# code. In C# there is a Path.Combine() function that appends two file paths together to form one complete path without having to care what the beginning character and ending character of the two paths are. I wrote up a little function in PHP to mimic what happens.

//take a path and/or a filename and combine them into one file path
function filePathCombine($path1, $path2)
{
  $completedPath = '';

  if(substr($path1, strlen($path1) - 2, strlen($path1) - 1) !== DIRECTORY_SEPARATOR)
  {
		$completedPath = $path1 . DIRECTORY_SEPARATOR;
  }
  else
  {
		$completedPath = $path1;
  }

  if(substr($path2, 0, 1) !== DIRECTORY_SEPARATOR)
  {
		$completedPath .= $path2;
  }
  else
  {
		$completedPath .= substr($path2, 1, strlen($path2) - 1);
  }

  return $completedPath;
}

Looking through the code you will notice DIRECTORY_SEPARATOR, which is a global PHP constant. While it isn’t needed considering using / instead of \ would work on Windows and Linux, I thought it best to try to stay with my idea on how to do things the proper way.

Clean URLs in PHP Yii Framework

Wednesday, November 18th, 2009

Here is a quick tip to save some trouble. The goal here is to get clean urls like http://localhost/site/test/5 or http://localhost/site/contact without having a query string variable and also not having the filename index.php show up.

My configuration:
EasyPHP (apache/php/mysql combo for windows)
Yii PHP framework

In the Yii main.php configuration file you need:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
),

Inside the ‘components’ array area.

In the primary directory where the index.php file is you need a .htaccess files with this in it:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

If you have problems, check your httpd.conf file for these items:

<directory c:\easyphp\www>

Inside that directory section, make sure the rules don’t disallow the removal of index.php or the other rewrite rules.

Make sure this line is un-commented:

LoadModule rewrite_module modules/mod_rewrite.so

I think that’s about it…


TheWayOfCoding.com

 
Scott J. Waldron Photography
Stock Photo Website
Tech Learning Site
Follow me on Twitter

Popular Article Tags

Archives

Pages