Linux Commands Examples

A great documentation place for Linux commands

php

PHP Command Line Interface ’CLI’

Synopsis

php [options] [ -f ] file [[--] args...]

php [options] -r code [[--] args...]

php [options] [-B code] -R code [-E code] [[--] args...]

php [options] [-B code] -F file [-E code] [[--] args...]

php [options] -- [ args...]

php [options] -a


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

0

php -r ’echo "Hello World\n";’

This command simply writes the text "Hello World" to standard out.

php -r ’print_r(gd_info());’

This shows the configuration of your gd extension. You can use this to easily check which image formats you can use. If you have any dynamic modules you may want to use the same ini file that php uses when executed from your webserver. There are more extensions which have such a function. For dba use:
php -r ’print_r(dba_handlers(1));’

php -R ’echo strip_tags($argn)."\n";’

This PHP command strips off the HTML tags line by line and outputs the result. To see how it works you can first look at the following PHP command ´php -d html_errors=1 -i´ which uses PHP to output HTML formatted configuration information. If you then combine those two ´php ...|php ...´ you’ll see what happens.

php -E ’echo "Lines: $argi\n";’

Using this PHP command you can count the lines being input.

php -R ’@$l+=count(file($argn));’ -E ’echo "Lines:$l\n";’

In this example PHP expects each input line being a file. It counts all lines of the files specified by each input line and shows the summarized result. You may combine this with tools like find and change the php scriptlet.

php -R ’echo "$argn\n"; fgets(STDIN);’

Since you have access to STDIN from within -B -R -F and -E you can skip certain input lines with your code. But note that in such cases $argi only counts the lines being processed by php itself. Having read this you will guess what the above program does: skipping every second input line.


0
source

PHP Fatal error: Call to undefined method PEAR::raiseErro() in /usr/share/php/PEAR/REST.php on line 165

I had this on Ubuntu 10.04 installing the Mongo PHP-extension. Did some research and tried this:

# pear search http
The value of config option cache_dir (/tmp/pear/cache) is not a directory and attempts to 
create the directory failed.

When I created the directories by hand:

mkdir -p /tmp/pear/cache
the error "went away" and I was able to install: "pecl install mongo".
0
source

How to add PHP in netbeans on Ubuntu?

The first result when googling "php netbeans ubuntu", this should solve your problems.

0
source

Making Apache use compiled PHP instead of bundled package on Debian

Debian can actually help you here--apt-get has a mode for downloading the source and build-dependencies for a package, which you can then tweak and build yourself. In theory, the setup should be identical to what Debian's repository contains, so it ought to integrate well with your version of Apache.

I originally found this is PHP Magazine where they used it to customize the version of GD they were compiling with PHP. But you could use it for changing other build flags just as easily.

http://web.archive.org/web/20101229025544/http://www.phpmag.ru/2009/09/12/ubuntu-9-04-php-5-gd-2/

Since the original site isn't available any more (link above is through the Way Back Machine), I'm reproducing the instructions here:

# Install build tools, debian helpers and fakeroot
apt-get install build-essential debhelper fakeroot
# Get PHP source (it should go into /usr/src)
cd /usr/src
apt-get source php5
# Install all packages required to build PHP5
apt-get build-dep php5

#Now what we need is to update compile options,
# so we need to edit debian/rules file:
cd php5-5.2.6.dfsg.1
vim debian/rules
# locate the line having "--with-gd=shared,/usr --enable-gd-native-ttf \"
# replace with "--with-gd=shared --enable-gd-native-ttf \"
# that's remove reference to /usr so that bundled library is used

# compile (drink some coffee, walk you dog, see the latest House episode)
dpkg-buildpackage -rfakeroot

# install the new php5-gd package
cd ..
dpkg -i php5-gd_5.2.6.dfsg.1-3ubuntu4.2_i386.deb

# finally restart apache
/etc/init.d/apache2 restart

Obviously, change the version number to match the version you're actually compiling, and replace the flags with the ones you actually want.

0
source

Linux server broken packages

Ok here's what I ended up doing:

First of all, I would recommend using aptitude instead of apt-get because it makes better choices by default. So, I went ahead and accepted aptitude's first solution, which was to leave everything the same version except for php5-curl which was to be installed. Trust me, you don't want to update the php5-common or libapache2-mod-php5 packages on a mission-critical server because they have a million dependencies. Then the server couldn't download the package because the repositories are outdated on this server (it's 10.04 remember) and it couldn't find the .deb file. So, I copied the name php5-curl-5.2.10.dfsg.1-2ubuntu6_amd_64.deb and Google'd it and found site hosting the .deb file. I then did the following commands to install this package without causing dependency hell:

$ wget http://www.thesitewhereyoufounditongoogle.com/php5-curl-5.2.10.dfsg.1-2ubuntu6_amd_64.deb

$ sudo dpkg -i php5-curl-5.2.10.dfsg.1-2ubuntu6_amd_64.deb

and then of course restart Apache

$ sudo /etc/init.d/apache2 restart

And voila! cURL was installed.

0
source

modify 128 bytes

Simply open the file in "append" or similar mode, seek to position 0, then write your data.


"Linux native commands": the syscalls (or their libc wrappers) are as close as you can get.

#include <fcntl.h>
#include <unistd.h>

void main() {
    char buf[128] = "this and that";
    int fd = open("file", O_WRONLY);
    lseek(fd, 0, SEEK_SET);
    write(fd, &buf, sizeof(buf));
    close(fd);
}

PHP:

<?php
$buf = "this and that";
$fh = fopen("file", "a" "r+");
fseek($fh, 0);
fwrite($fh, $buf);
fclose($fh);
0
source

Server uptime checker?

If all you want to do is find out if its up, write a script to ping the server every 10 min or so. If you want something that will actually check functionality (like delivering a web page) write a javascript snippet that will refresh your page every n minutes.

0
source

What are the benefits of using Linux OS to develop a PHP based website?

I've been using Windows, Mac and Ubuntu for PHP development, and if your environment is set correctly, there's absolutely no difference imho.

Some tools, like Git, are less available for Windows, so you may need to look for special deployables or use cygwin, but generally speaking, no difference.

0
source

How to convert .PDF and .doc/.docx files to .HTML files?

You should give unoconv a spin. It should be able to convert anything that Open Office can read to anything it can write.

This works on doc/docx and a whole lot of other files. It does not seem to work on PDF's so I guess you're stuck with using 2 separate programs for the job.

0
source

Execute PHP files in mounted directory from VirtualBox host system is not working

Check the options on the mount. Is "noexec" enabled?

0
source

Installed mysql from source on linux and then how to get it work with php?

Finally I recompiled my php source with appending --with-mysql=/opt/mysql, --with-mysqli=/opt/mysql/bin/mysql_config and --with-pdo-mysql three configure options to let my php support the modules of mysql. Steps taken as follows:
1> stop apache and mysql services
2> backup php.ini
3> remove php
4> reconfigure php source
./configure --prefix=/opt/php --with-apxs2=/opt/apache/bin/apxs --with-mysql=/opt/mysql --with-mysqli=/opt/mysql/bin/mysql_config --with-pdo-mysql --...and other options
5> make and then make install
6> copy php.ini back and uncomment the corresponding directs on mysql modules

0
source

How can two identically named files exist in the same place on linux

It can happen when one of the file name has special character which is not visible.

Either its not visible in the browser or your file browser is just stripping this character. Many software does it to beautify the output.

0
source

php startup error Invalid library (maybe not a PHP library) 'pcntl.so'

You should have a file called pcntl.so in the modules subdirectory of the directory where you built php. Copy that to /usr/lib/php5/20090626/ :

sudo cp PHP_BUILD_DIR/modules/pcntl.so /usr/lib/php5/20090626/

Obviously, change PHP_BUILD_DIR to the directory where you compiled php.

0
source

phpmyadmin symlinks error after ubuntu upgrade

For me the following solution solved the problem:

In my website conf file, I have the following section:

    <IfModule mod_php5.c>
            AddType application/x-httpd-php .php

            php_flag magic_quotes_gpc Off
            php_flag track_vars On
            php_flag register_globals Off
            php_admin_flag allow_url_fopen Off
            php_value include_path .
            php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
            php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/share/php/php-gettext/
    </IfModule>

The only change I've made since Ubuntu 13.04 install is include /usr/share/php/php-gettext/ in the open_basedir, and that did the trick.

0
source

How to find out from Apache what pages cause long loading times?

Run them with Apache JMeter.

Apache JMeter may be used to test performance both on static and dynamic resources (files, Servlets, Perl scripts, Java Objects, Data Bases and Queries, FTP Servers and more). It can be used to simulate a heavy load on a server, network or object to test its strength or to analyze overall performance under different load types. You can use it to make a graphical analysis of performance or to test your server/script/object behavior under heavy concurrent load.

description

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. This is the command line interface that enables you to do the following:

You can parse and execute files by using parameter -f followed by the name of the file to be executed.

Using parameter -r you can directly execute PHP code simply as you would do inside a .php file when using the eval() function.

It is also possible to process the standard input line by line using either the parameter -R or -F. In this mode each separate input line causes the code specified by -R or the file specified by -F to be executed. You can access the input line by $argn. While processing the input lines $argi contains the number of the actual line being processed. Further more the parameters -B and -E can be used to execute code (see -r) before and after all input lines have been processed respectively. Notice that the input is read from STDIN and therefore reading from STDIN explicitly changes the next input line or skips input lines.

If none of -r -f -B -R -F or -E is present but a single parameter is given then this parameter is taken as the filename to parse and execute (same as with -f). If no parameter is present then the standard input is read and executed.

options

--interactive

-a

Run PHP interactively. This lets you enter snippets of PHP code that directly get executed. When readline support is enabled you can edit the lines and also have history support.

--bindpath address:port|port
-b
address:port|port

Bind Path for external FASTCGI Server mode (CGI only).

--no-chdir

-C

Do not chdir to the script’s directory (CGI only).

--no-header

-q

Quiet-mode. Suppress HTTP header output (CGI only).

--timing count

-T count

Measure execution time of script repeated count times (CGI only).

--php-ini path|file

-c path|file

Look for php.ini file in the directory path or use the specified file

--no-php-ini

-n

No php.ini file will be used

--define foo[=bar]

-d foo[=bar]

Define INI entry foo with value bar

-e

Generate extended information for debugger/profiler

--file file

-f file

Parse and execute file

--global name

-g name

Make variable name global in script.

--help

-h

This help

--hide-args

-H

Hide script name (file) and parameters (args...) from external tools. For example you may want to use this when a php script is started as a daemon and the command line contains sensitive data such as passwords.

--info

-i

PHP information and configuration

--syntax-check

-l

Syntax check only (lint)

--modules

-m

Show compiled in modules

--run code

-r code

Run PHP code without using script tags ’<?..?>’

--process-begin code

-B code

Run PHP code before processing input lines

--process-code code

-R code

Run PHP code for every input line

--process-file file

-F file

Parse and execute file for every input line

--process-end code

-E code

Run PHP code after processing all input lines

--syntax-highlight

-s

Output HTML syntax highlighted source

--version

-v

Version number

--stripped

-w

Output source with stripped comments and whitespace

--zend-extension file

-z file

Load Zend extension file

args...

Arguments passed to script. Use ’--’ args when first argument starts with ’-’ or script is read from stdin

--rfunction

name

--rf

name Shows information about function name

--rclass

name

--rc

name Shows information about class name

--rextension

name

--re

name Shows information about extension name

--rzendextension

name

--rz

name Shows information about Zend extension name

--rextinfo

name

--ri

name Shows configuration for extension name

--ini

Show configuration file names

copyright

Copyright © 1997-2010 The PHP Group

This source file is subject to version 3.01 of the PHP license, that is bundled with this package in the file LICENSE, and is available through the world-wide-web at the following url:
http://www.php.net/license/3_01.txt

If you did not receive a copy of the PHP license and are unable to obtain it through the world-wide-web, please send a note to license[:at:]php[:dot:]net so we can mail you a copy immediately.

files

/etc/php5/cli/php.ini

The configuration file for the CLI version of PHP.

/etc/php5/cgi/php.ini

The configuration file for the CGI version of PHP.

/etc/php5/apache2/php.ini

The configuration file for the version of PHP that apache2 uses.

tips

You can use a shebang line to automatically invoke php from scripts. Only the CLI version of PHP will ignore such a first line as shown below:

#!/bin/php
<?php
// your script
?>

version information

This manpage describes php, version 5.4.9-4ubuntu2.2.


bugs

You can view the list of known bugs or report any new bug you found at:
http://bugs.php.net


see also

For a more or less complete description of PHP look here:
http://www.php.net/manual/


authors

The PHP Group: Thies C. Arntzen, Stig Bakken, Andi Gutmans, Rasmus Lerdorf, Sam Ruby, Sascha Schumann, Zeev Suraski, Jim Winstead, Andrei Zmievski.

Additional work for the CLI sapi was done by Edin Kadribasic, Marcus Boerger and Johannes Schlueter.

A List of active developers can be found here:
http://www.php.net/credits.php

And last but not least PHP was developed with the help of a huge amount of contributors all around the world.

How can this site be more helpful to YOU ?


give  feedback