Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, April 11, 2014

CentOS: Compiling and Installing PHP Imagick extension

Requirements

This extension requires ImageMagick version 6.2.4+ and PHP 5.1.3+. Package information is here, Imagick package on PECL

The normal procedure goes like this

    wget http://pear.php.net/go-pear.phar
    php go-pear.phar
    pecl install imagick

While it throws the following error,

    No releases available for package "pecl.php.net/imagick"
    install failed

So I decided to compile directly from source (in order to generate imagick.so)

    yum install php-pear php-devel
    yum install ImageMagick ImageMagick-devel
    yum install pcre-devel
    wget http://pecl.php.net/get/imagick-3.2.0RC1.tgz
    tar -xvf imagick-3.2.0RC1.tgz
    cd imagick-3.2.0RC1/
    phpize
    ./configure
    make
    make test
    make install

Remember to turn on the extension in the php configuration file.

    vim /etc/php.ini
        extension = imagick.so

References

Monday, November 18, 2013

Troubleshooting: Does PHP have write permission to a folder?

This article only talks about PHP installed alongside Apache under Linux (see here). Since PHP is run through libapache2-mod-php5. it inherits the permissions from Apache. So let's take a look about Apache. By default, it uses www-data user, to check this,

ps -u www-data

If it is so, you might get something similar:

PID TTY          TIME CMD
 1276 ?        00:00:00 php5-fpm
 1277 ?        00:00:00 php5-fpm
 1278 ?        00:00:00 php5-fpm
 1279 ?        00:00:00 php5-fpm
14934 ?        00:00:00 apache2
14935 ?        00:00:00 apache2
14936 ?        00:00:00 apache2
...

Now check user info and its group of www-data

cat /etc/passwd | grep www-data

The following shows up on my machine which says it doesn't belong to the root group or my administrator group.

www-data:x:33:33:www-data:/var/www:/bin/sh

We list the file permissions of the folder img/ to see apache's right:

ls -l | grep img

All right! Since Apache is in other group here, it doesn't have 'w' permission.

drwxrwxr-x 2 admin admin 4096 Nov 12 19:26 img

Then let's give him the permission. Notice that it is dangerous to do this, since in this way every user on the computer will have rights on this folder. Normally, we let the owner of the folder be Apache, and others can only read.

chmod o+w img
References

Thursday, January 24, 2013

PHP: Public, Protected and Private

These three keywords define the visibility of variables.

  • public: visible for both inside and outside
  • protected: visible only to its own methods
  • private: similar to protected, intended to be accessed only by the class who defines it

An Instance

error_reporting(E_ALL);
class foo{
   public    $a=1;
   protected $b=2;
   private   $c=3;

   function stampa()
   {
      echo $this->b;
      echo $this->c;
   }
}
class bar extends foo{
   function __construct(){
      $this->b = 4;
      $this->c = 5;
   } 
}
$A = new foo;
echo $A->a;
$A->stampa();
$B = new bar;
$B->stampa();

Here is the output,

12343

Notes

  • The version of tested php: 5.3.6-13
  • The instance shows the re-assigning of the private member doesn't make any difference. The inheriting class can not modify private members.
  • The private member is still visible (printed) to the inheriting class.

Wednesday, January 23, 2013

Ubuntu: LAMP Memoir

Installing

sudo apt-get install apache2
sudo apt-get install php5 libapache2-mod-php5
sudo apt-get install mysql-server
sudo apt-get install php5-mysql
sudo apt-get install libapache2-mod-auth-mysql

Restarting Service

sudo service apache2 restart
sudo service mysql restart

Interacting with MySQL

mysql -p 
mysql -p db_name < import.sql

Installing phpMyAdmin

sudo apt-get install phpmyadmin

Let Apache know about phpMyAdmin:

sudo gedit /etc/apache2/apache2.conf
Include /etc/phpmyadmin/apache.conf

Use php Mail with PEAR

If you see Failed opening required 'Mail.php', this means pear/Mail is not installed.

wget http://pear.php.net/go-pear.phar
sudo php go-pear.phar
sudo pear install Mail
sudo pear install Net_SMTP

Configure that base path be /usr/share/pear so that you don't need to change the include path. On wrong installation, uninstall it this way

sudo pear uninstall pear

Troubleshooting

After any change, restart service to see effects.

  • Program halts when accessing database: the module php5-mysql may be not installed
  • mysql_connect gives an empty output, this may be caused by a not guaranteed permission. Try use root or update the user with proper privilege.

Wednesday, September 19, 2012

php: Rendering text files in UTF-8

Pure .txt (even .utf8) files are rendered not as its natural encoding under Chrome (Version 21.0.1180.89 at present time). Then how to tell the browser to display it in the preferred encoding? We wrap it up via php and tell browser it requires utf-8.
  header('Content-Type: text/plain; charset=utf-8');
  include($txtname);