How to get the maximum upload file size possible in PHP
Lets say you build websites for different servers the chance that the maximum upload size differs on these servers might be big. So, you want to automatically handle this.
In the following example we simply get the sizes who have influence on the maximum upload possible. Note that you can set the upload_max_filesize to a really big value but you still need memory to handle the file in most cases!
With the following example you can get the lowest possible value and use that in your script to handle as upload maximum.
$max_upload = (int) ini_get('upload_max_filesize'); $max_post = (int) ini_get('post_max_size'); $memory_limit = (int) ini_get('memory_limit'); $upload_mb = min($max_upload, $max_post, $memory_limit); echo $upload_mb;
This example gets the value in MegaBytes.
E_STRICT crashes php if date.timezone is not set in php.ini
In case you use the following in your php.ini:
error_reporting = E_ALL | E_STRICT
You might notice that if you do a phpinfo() that php crashes the apache child.
Reason is a bug in php that causes an infinite loop.
You can solve this by setting "date.timezone =" to your timezone.
You can also set this in your script with ini_set() but I would go for the php.ini since thats a more permanent solution.
For a list of timezones check:
http://www.php.net/datetime
How to enable error reporting in PHP
When you are developing something new in PHP you might want to enable error reporting so you can see what is going wrong and that you are working by the strict guidelines of PHP.
With this you can enable displaying errors in your browser and you can also save it to an error log if you want.
Note for PHP5.3 and above, E_ALL includes E_STRICT so you can leave out the "| E_STRICT" part.
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('error_reporting', E_ALL | E_STRICT); # Optional ini_set('error_log', '/data/www/marius/php_error.log');
You can also use these in your php.ini if you want.
How to fix inconsistent line ending (EOL) style with find and Perl
If you work with subversion you might get this error when you got files that have been edited on different operating systems like Windows, Linux, FreeBSD or Mac OS X.
Well, the fix is quite simple. You simply replace the wrong line endings with right ones depending of which you want. In my situation I want unix style line endings.
Replace in PHP/JavaScript files:
find ./ -name '*.php' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ; find ./ -name '*.php' -type f -exec perl -i -wpe 's/r/n/g' '{}' ; find ./ -name '*.js' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ; find ./ -name '*.js' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;
In case you want to replace them in multiple file types you can adjust the command. In this example we want to replace in the following file types:
- asp
- cfm
- css
- html
- js
- php
- pl
- txt
Use the following commands:
find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/rn/n/g' '{}' ; find ./ -name '*.asp' -or -name '*.cfm' -or -name '*.css' -or -name '*.html' -or -name '*.js' -or -name '*.php' -or -name '*.pl' -or -name '*.txt' -type f -exec perl -i -wpe 's/r/n/g' '{}' ;
Keep in mind, you may use these commands on your own risk. I'm not responsible if you lose your work
Now you should be able to commit your files again
Shellproxy, an easy tool to run shell commands from PHP or other languages
This is a simple script that runs a shell command from PHP with the function system().
It sometimes happens that a command doesn't close the stdin or stdout, resulting the system() function to halt and wait for it to finish...
This causes PHP to reach the 30 second time limit and kill the script.
This script runs it in the background and you could make your commands send their output to a log file and still having full control.
Read the code below how to use it.
You can compile it by placing the code into shellproxy.c and then compile it with the following command:
gcc -o shellproxy shellproxy.c
I hope this is something useful for you as it was for me.
#include <stdio .h> #include <stdlib .h> #include <string .h> int main(int argc, char *argv[]) { char command[1024]; int t; if (argc == 1) { printf("nShell Proxyn"); printf("By Marius van Witzenburg <http ://kitara.nl>nn"); printf("You will mostly use this program from PHP or any othern"); printf("webscripting language to background run programsnn"); printf("Usage:n"); printf("- CLI:n"); printf("shellproxy "<cmd> <arg>"n"); printf("shellproxy "<cmd> <arg> > redirect.txt"n"); printf("- PHP:n"); printf("system('shellproxy "<cmd> <arg> &"');n"); printf("system('shellproxy "<cmd> <arg> > redirect.txt"');nn"); } command[0] = 0; for (t = 1; t < argc; t++) { strcat(command, argv[t]); strcat(command, " "); } fclose(stdin); fclose(stdout); fclose(stderr); system(command); }
It would be nice if you keep my name mentioned into this code if you use it.
Ps. Donations are welcome...




