Get the path of the current script

echo __FILE__;

Get the directory of the current script

echo realpath(dirname(__FILE__));

Fix those pesky PDO SQLSTATE[HY000] [2002] No such file or directory errors

  • Try using 127.0.0.1 instead of localhost for DB host
  • Add a utf-8 charset

Do a 301 permanent redirect

header("Location: https://www.example.com", true, 301);

Increasing the WordPress maximum upload file size

Make sure these values are properly set in your PHP configuration file:

post_max_size
upload_max_filesize

And reload the PHP service you’re using. E.g. for fpm you need to do something like:

sudo systemctl reload php8.0-fpm

Note that you also might need to change your webserver settings, e.g. for Nginx you need a line like:

client_max_body_size 32M;

In your site configuration or nginx.conf.

Make PHP give trackebacks on every error, including warnings

Add this to the top of your main PHP file:

set_error_handler(function($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
});