background preloader

Tutorials and Tips

Facebook Twitter

PHP: Why you should use dirname(__FILE__).‘/include.php’ instead of just ‘include.php’ InShareinShare When you need to include or require a php file that is in the same directory as the currently running one, most people come up with this simple line in the current script: include('include.php'); While this approach doesn’t present obvious breaches, it is slightly inefficient than the following way: include(dirname(__FILE__).'/include.php'); You will type a little more but the extra code frees PHP from iterating through the include_path in the attempt to locate ‘include.php’ because dirname(__FILE__) has explicitly returned the absolute path to the file.

The constant __FILE__ in PHP always returns the absolute path to the script file that’s currently active – the PHP file in which the code line is being run right now. A better approach would be: include('. Which explicitly commands PHP to look for the file ‘include.php’ in the current directory, yet comes without the overhead of the function dirname(). Define('APP_DIR', '/home/appuser/appdomain.com/app'); include(APP_DIR.'

Php. How To: Create PDF With Php. In this tutorial, we will explore the possibilities of generating a PDF file - on-the-fly - with PHP. The samples that are presented can be run on astahost.com. Why would we want to generate a PDF on-the-fly ? Well, we might want to include in the PDF some data that must be entered by our surfer, by means of a html form. Or we might want to include in the PDF some data that comes from a database that is updated by another process. Or some other reason. You invent one. All reasons are legit! (1) The first thing to do, when we want to generate a PDF file with PHP, is to check that the PHP libraries, that support the creation of PDF files, are present and available to PHP. <? The phpinfo() function generates a html page with all the information about the PHP installation, including the information about the Apache web server, the version and settings of the PHP compiler, and the supplemental PHP libraries that are installed and activated.

We have to check the pdf libraries. . (2) A simple test. <? Send Emails Using Php (SMTP Direct) - PHP Tutorials. Distributing PHP processing with Gearman - PHP Classes blog. Contents- Introduction to Gearman- Gearman architecture- Using Gearman with its PHP extension- Starting a Gearman job server with a persistent job queue- Distributing PHP applications by setting up PHP worker and client processes- Application ideas- Other ideas and comments - Introduction to Gearman Gearman is a generic framework to distribute processing jobs to separate processes in the same machine or other machines in a cluster. It allows your application to perform tasks in parallel, balance the processing load, and even invoke code written in other languages. The "Gearman" word is an anagram of "manager". Its purpose is solely to dispatch jobs that need to be executed, but Gearman just by itself does not do anything useful. - Gearman architecture A Gearman based architecture has three types of elements: * Job servers Job servers are instances of the manager program.

. * Clients The clients talk to a job server in order to request the execution of job functions. . <? If (! Dates in PHP and MySQL. I see a lot of people on forums and on my training courses asking about the best way (or any way) to manage dates stored in a MySQL database and used in PHP. Three options follow, but first the problem. PHP uses unix timestamps for all its date functionality. It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format. A timestamp is simply an integer. Specifically, it’s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time). MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP. So the problem is how to work with these two very different date formats – the PHP timestamp integer and the MySQL DATETIME string.

One common solution is to store the dates in DATETIME fields and use PHPs date() and strtotime() functions to convert between PHP timestamps and MySQL DATETIMEs. So finally we come to the choice of which to use. PHP Remove Non ASCII Characters from a String. Get rid of extra spaces using PHP's preg_replace | Open Data. Array set to empty. PHP File Upload Configuration. Though PHP presents a very versatile and user friendly interface for handling file uploads, the default installation is not geared for working with files in excess of 2 Mega Bytes. This article will help you configure your PHP engine for handling such large file transfers. The php.ini File All the configuration settings for your installation are contained in the php.ini file. Sometimes these setting might be overridden by directives in apache .htaccess files or even with in the scripts themselves. However you cannot over ride some of the settings that effect file uploads with .htaccess directives in this way.

You can call the phpinfo() function to find the location of your php.ini file, it will also tell you the current values for the following settings that we need to modify file_uploads upload_max_filesize max_input_time memory_limit max_execution_time post_max_size The first one is fairly obvious if you set this off, uploading is disabled for your installation. Memory_limit Other Options. PHP: Connection handling - Manual. Creating excel files through PHP. No need to use complicated or extensive libraries (such as PEAR) to create Excel pages. Just take advantage of the "smart" part of Excel; its ability to parse an HTML table to a nice Excel page. Just create a regular .PHP file, where you output your data in a nice little html-table. Then place the following snippet in the top of that file (no output can happen before these lines, as they change your headers -- so place these all the way at the top): header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=excel.xls"); And it's just that easy.

If that's not enough, you can look at more extensive libraries such as PHPExcel. [PHP] Secure Downloads. PHP How to do a POST request. This example shows how to do a simple POST request to another webserver by using a socket connection. Easy way to create XLS file from PHP. PHP Tutorial. PHP and long running processes. It seems this question keeps coming up on the PHP newsgroups and, now that I've plugged into Stack Overflow - I keep seeing it their too: How I do I start a PHP program which takes a long time to complete and how do I track its progress? While these tend to attract lots of replies, they are usually wrong. The first thing to consider is that you need to seperate the the thing which takes a long time from its initiation, the ongoing monitoring and whatever final reporting is required. Since we're talking about PHP its fair to assume that in most cases, the initiation will be a PHP script running in a webserver.

However this is not a good place to keep a long-running program. 1) webservers are all about turning around requests quickly - indeed most have failsafe mechanisms to prevent one request hanging about too long. 2) the webserver ties the request to both the execution of the script and to the client socket connection. You need to create a new process certainly. A brave attempt. Writing a Template System in PHP. (Page 1 of 9 ) Learn how to use templates in your web applications and implement them with object oriented code. Templating is an easy way to create large sites without much hassle in maintaining a consistent look and feel across them. A standalone template file contains the HTML framework of the page and placeholders. A script can take the template file and replace the placeholders with the appropriate content to produce the final page. While I was working on a recent project I came across the need for a basic templating script.

The project needed something small and simple but also extensible. More Display Tutorials ArticlesMore By bluephoenix.