Posts

Showing posts with the label PHP

Increasing PHP file upload size

Image
Just a walk through of something that took me 3 minutes longer than it should have 🤔  Uploading a file via a PHP script was throwing an error. The culprit: upload_max_filesize was set to the default 2M in /etc/php.ini. (Tip: Use phpinfo() to determine the Loaded Configuration File, against the possibility there’s more than one on the system.) Once you tweak php.ini, restart the PHP FastCGI Process Manager, if that’s how PHP is running on the system (it is, at least on the Rocky Linux 9.7 box this service was running on): systemctl restart php-fpm.service And, Bob’s your uncle. (When I was a freshman, you just had to restart Apache ... 🤣)

Quick and dirty way to run PHP with a double-click

I’m doing more with Things integration and PHP is a quick-and-dirty, but way more powerful than BASH , way to do what I’m trying to do. But tweaking permissions, bang-bash lines, etc., is more than I wanted to deal with. So I’m wrapping some PHP code in a BASH script with a .command extension. This may not be the best way to do this, but, again, quick-and-dirty. #!/usr/bin/env bash clear PHP= `php --version` EXIT= $? if [[ $EXIT -ne 0 ]] then echo "PHP not installed, running brew install php..." brew install php fi UUID= "`uuidgen`" DIRECTORY= "`dirname " $0 "`" cat > /tmp/ $UUID .php << 'FNORD_EOF' <?php print "Hello, world. Running in directory: " . $argv[1] . "...\n"; print_r($argv); ?> FNORD_EOF php /tmp/ $UUID .php "$DIRECTORY" rm /tmp/ $UUID .php

Why I love/hate PHP

Image
I’ve been slinging PHP code since the late 90s (PHP3 era). My name’s in a half-dozen Wrox Press books as a technical reviewer, covering Linux, MySQL, PHP4. (I’m even on the cover of one, as an author.) As a quick, dirty way to get a website up and running, it’s ... well, quick and dirty. But it was kind of obviously hacked together, and there’s a lot of inconsistency. For example, in the strpos() call, the arguments are in the order haystack, needle, while in in_array() it’s needle, haystack. If you’re not using PhpStorm (and I’m usually not ‡ ), with API autocompletion, that may not be that big of a deal, but if you’re logged in remotely using vim or otherwise not wrapped in an IDE, the mental task switch of flipping over to the API reference to see which order a particular call requires is, for me, less than ideal.  But for all its foibles, it’s still, in my opinion, one of the best ways to rapidly get a web application up and running. Historically, it has been loosely ...

Making an executable PHP script

Image
Super basic, and I don’t know why I’ve never done this before; a PHP script can be run in a UNIX or Linux (etc) terminal session (etc) just like a BASH script or other standard UNIX shell script: % cat php_script #!/usr/bin/env php <?php print "Arguments:\n"; print_r($argv); % chmod 755 php_script % ./php_script fnord ranger Arguments: Array (     [0] => ./php_script     [1] => fnord     [2] => ranger )

Relative root path in PHP

PHP has Magic Constants , like __DIR__ and __FILE__. But (unfortunately?) those will point to the directory and the file where the constants are referenced; e.g., index.php will show /path/to/index.php, while a class contained in a file in a lib/ directory will have /path/to/lib/ClassFile.php. For webserver executed scripts, a good way of getting the "relative root" is via: $_SERVER [ "SCRIPT_FILENAME" ] Like so: $db_class_dir = dirname ( $_SERVER [ "SCRIPT_FILENAME" ] ) . DIRECTORY_SEPARATOR ;

10 Second Guide to PHPDoc

Image
% wget https://phpdoc.org/phpDocumentor.phar % sudo mv phpDocumentor.phar /usr/local/bin % sudo chmod +x /usr/local/bin/phpDocumentor.phar % sudo ln -s /usr/local/bin/phpDocumentor.phar /usr/local/bin/phpdoc % phpdoc -d . -t docs/api /** * Returns the value of the given Calendar field. * * @param int $field The Calendar field to return (e.g., Calendar::YEAR). * * @return mixed The value of the supplied Calendar field (e.g., 2022). */ public function get( int $field ) { ... }

Ten Second Guide to PHPUnit

Installing PHPUnit Downloading Directly $ wget https://phar.phpunit.de/phpunit-6.5.phar $ chmod +x phpunit-6.5.phar $ sudo mv phpunit-6.5.phar /usr/local/bin/ $ sudo ln -s /usr/local/bin/phpunit-6.5.phar /usr/local/bin/phpunit $ phpunit --version PHPUnit 6.5.14 by Sebastian Bergmann and contributors. Using Composer $ composer require --dev phpunit/phpunit ^6.5 $ vendor/phpunit/phpunit/phpunit --version Create Tests To test, in this example, the Calendar class, create a CalendarTest class with at least one function with a name starting with ‘ test ’: <?php require "Calendar.php"; use PHPUnit\Framework\TestCase; class CalendarTest extends TestCase {    // Tests must be written in a public functions with a name starting with test    public function testDayOfWeek() {                  $c = Calendar::parse("2019-10-01");         $c->setWeekdayInMonth( Calendar::THURSDAY, 3); ...