Path variables in PHP

Many of you develop php applications for multiple server environment. Webserver environment may be different, have different directory structure, so you have to use paths very carefully. For example in file upload script, you manually write absolute path of image directory, the application may not work on other server. To avoid this you should use inbuilt php path variables. I am posting summary of all php related path variables.

Operation: Extract file name from path
Function: string basename ( string $path  [, string $suffix  ] )
$path = /opt/lampp/htdocs/demo/test.php;
echo basename($path);
Output: test.php

Operation: Get web server root path
Output: /opt/lampp/htdocs

Operation: Get current script full path (including document root path)
Output: /opt/lampp/htdocs/test.php

Operation: Get current script name (relative to root)
Output: test.php

Operation: Get current script name (relative to root)
Output: test.php

Operation: Convert relative path to absolute path
Function: string realpath ( string $path  )
echo realpath(‘../images’);
Output: /opt/lampp/htdocs/images

Operation: Get all file details
Function: mixed pathinfo ( string $path  [, int $options= PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME  ] )
$info = pathinfo($_SERVER[‘PHP_SELF’]);
[dirname] => /abhi
[basename] => EmptyPHP.php
[extension] => php
[filename] => EmptyPHP

Quick php fix to CSS and Javascript browser cache

I am going to post very handy tip for javascript and css files caching. Browsers automatically cache the css and JS files and when you make change to CSS and JS some times change does not appears.

To prevent this use following

echo '<link rel="stylesheet" type="text/css" href="style.css?' . filemtime('style.css') . '" />';

And For JS

echo '<script type="application/javascript"="main.js?' . filemtime('
main.js') . '" />';

The PHP function filemtime returns last modified date of file, so every time you make change in css / JS files the filemtime will return different value and CSS / JS Url will change and browser will refresh CSS / JS file.


Large File Upload

Recently I faced a big problem with large file uploads. One of my client has a Centos5  dedicated server. I created a script for Video upload and conversion. I also added a progress bar to show file upload progress. This progress bar has limit of 100MB because of flash limitation. Everything was working fine for files less than 50MB, but as size grows the upload hangs on 20 – 30%.. I did every thing in php.ini and apache.conf but nothing helped. I wrote to server admin at least 50 times and finally they giveup! Now I has no solution.

I start searching on net and found uberUploader. This is the most comprehensive uploader available on net. You don’t even need to set php.ini settings, because the uploading part handled by Perl. This resolved my problem. Now I can upload 1GB file This has a bunch of uploading configuration option. This also has nice progress bar.

Finally end of 1 month long problem.. Thanx uberUploader!!

flv, ffmpeg and PHP: File conversion

Hi friends,

2 days ago I got a project in which I have to upload avi, mpeg or 3gp movie clip and convert this clip to flv as well generate thumbnail for this video. I know that this can be done through ffmpeg. But I didn,t know how haha..

Well I searched enough and start making environment for development. I did following mistakes.. I am listing those  mistakes. I am 100% sure it will save your day.

I was using Windows environment: FFmpeg is primarily made for Linux environment not for windows. Some experts developed windows version of ffmpeg.  Some functions are unavailable in windows version. SO I suggest you to use Linux environment.

I thought that only ffmpeg can do everything: FFmpeg is a command line tool to do video related task (only video) such as clipping, video conversion, frame extraction etc. But for the sound, watermarking you have to use other packages too. I will tell about these details later.

php-ffmpeg and ffmpgeg: I was very much confuse between php-ffmpeg and ffmpeg. I after 30 minutes of googling I came to know that ffmpeg-php is extension for php which provide handy functions to convert videos and if you use ffmpeg then you have to use exec() command to execute ffmpeg commands.

Step by step procedure to convert video:

1. Setup Linux system.. forget that windows can do the job!

2. follow instructions and install all required modules. here is comprehensive step by step procedure available . if you face any problem then you can ask me.

Remember: Installation order  LAME->FFmpeg->Ruby->FLVTool2

3. Now you have successfully setup environment. Download any example from net and run.

The  best thing I got is videotoolkit you can download it from Here . It saved my day. you can also try it. It’s really worth to try this!

Hope this article will helpful. If you still have any problem then ask.. Happy coding….

SoftException in Application.cpp:303:

Recently I came across following when deploying webapplication on Linux serevr…
SoftException in Application.cpp:303: UID of script “/home/xyz/public_html/en/pupgrade.php” is smaller than min_uid
The problem was file owner and group as root (I was uploading file through root account)
Solution: change file owner and group to webhoting user..  generally it is your user name..

The linux command for this task is

$ chown -R username:usergroup directory