Home » Programming » PHP

How to Write/Save to a File with PHP [Examples]

PHP Write/Save File

This tutorial will demonstrate how to write data to a file on the server in the PHP programming language. Code examples are provided to show you how it’s done. Writing data to a file server-side is a common task. Whether you’re writing log files so you can debug your app, or saving user submitted data, you will need to save a file at some point in your web development career. Writing data to a file in PHP is a three-step process. You must first open … Read more

Categories PHP

Home » Programming » PHP

Ultimate Guide to Uploading Multiple Files With PHP [Examples]

PHP Multiple File Uploads Ultimate Guide

This tutorial will explain how to upload one or multiple files from the web browser to the server using PHP. Code examples are included, including the code for limiting file size and type. Being able to have your users upload files to your app has become expected functionality in most web apps. Photos, filled out PDF forms, videos, recordings – any kind of file can be uploaded, and the process is relatively straight forward. PHP provides all of the required tools for file uploading and … Read more

Categories PHP

Home » Programming » PHP

How to Resize/Crop Images in PHP with the GD Library

PHP Resize/Crop Image

This tutorial will show you how to resize or crop images in the PHP programming language. Code examples are provided which can be copy and pasted into your project. GD Library vs ImageMagick Image resizing in PHP is performed by an optional image processing library. The two most popular image processing libraries for PHP are GD and ImageMagick. Both work well, but I’ll be sticking to the GD Library for this tutorial as it is the most popular and supported. Installing the GD PHP Extension The GD library … Read more

Categories PHP

Home » Programming » PHP

How to Rename Files & Directories in PHP [Examples]

PHP Rename File

This tutorial will show you to rename files and folders in PHP with the rename() function – including some code examples you can use in your own project. We’ve already covered uploading files and deleting files in PHP, read on to find out how to rename them. rename() PHP Function Syntax The syntax for the rename() function used to delete files in PHP is as follows: rename($FROM, $TO, $CONTEXT) Note that: $FROM is the path of the existing file you wish to rename $TO is the new path you wish to rename … Read more

Categories PHP

Home » Programming » PHP

Output or Display an Image in the Browser from PHP

PHP Output Image File

This article will show you how to output or display an image in the web browser from PHP, with a quick copy & paste code snippet. Images in PHP We’ve covered how to resize images in PHP using the GD library. Once an image is created, it can be saved or returned to the browser to be displayed without saving it. Outputting/Displaying an Image in PHP without Saving it The following PHP code snippet creates an image object and returns it without saving it: // The header() function is … Read more

Categories PHP

Home » Programming » PHP

How to Locate the PHP.ini Configuration File [Linux/Ubuntu]

Locate PHP ini configuration

Here’s a quick tip on how to locate the php.ini configuration file which is currently in use by PHP on your system or web host. Locate PHP.ini From the Command Line You can usually find out which php.ini file PHP is pulling its configuration from from the command line. The php -i command will print the information about your current PHP environment and grep will filter that output to only lines containing the string ‘Configuration File’, including it’s location: php -i | grep ‘Configuration File’ Finding the PHP.ini Configuration in Use for … Read more

Categories PHP

Home » Programming » PHP

PHP_SELF – What It Is, and Why It’s Too Dangerous to Use [WARNING]

PHP_SELF

This article outlines the PHP_SELF attribute of the $_SERVER system information variable and why you should never, ever use it. What is _$SERVER? Check out our full article on $_SERVER here – but in short, it’s a variable containing an array with information about your PHP environment – including server and request details that are quite sensitive and shouldn’t be publicly accessible. What is $_SERVER[‘PHP_SELF’] ? $_SERVER[‘PHP_SELF’] contains the full path to the PHP script being executed, including any query parameters. This allows the party making the request to include arbitrary data. Displaying data … Read more

Categories PHP

Home » Programming » PHP

What is the $_SERVER Superglobal Variable in PHP?

PHP _SERVER

This article will explain what the $_SERVER superglobal variable is in the PHP programming language. What is a ‘Superglobal’ Variable? A superglobal variable is a variable that is available to all scripts in all scopes in PHP. It is available from within any file, class, or function. What is the $_SERVER Superglobal Variable? The $_SERVER superglobal contains information about the server and execution environment PHP is running in/on. It contains information on the request made to the web server, file paths, and other information. It will provide little to no info … Read more

Categories PHP

Home » Programming » PHP

A Trick for Validating JSON in PHP

PHP Validate JSON

Here’s a quick trick for validating JSON in PHP – with a reusable function, you can copy and paste it into your own project. JSON has pretty much become the standard format for transferring data between applications on the internet. So if you’re building an API to be consumed by others or just building a backend for your app, you’ll be sending and receiving a lot of JSON data. But, you can never trust the request – the data coming into your application. Users enter bad data. Hackers deliberately enter bad … Read more

Categories PHP

Home » Programming » PHP

How to Use the PHP empty() Function

Check if PHP Variable Empty

This easy tutorial will show you how to use the PHP empty() function to check whether a variable can be considered empty and show some code examples. What Is Empty? A variable is considered empty in PHP if its value equates to FALSE or the variable does not exist. This means that the following are considered empty: An empty string (“”) The number zero (0) In any form (0.0000) Including strings containing only a 0 character (“0”) Boolean FALSE NULL An undeclared variable Empty arrays The empty() function is different from the isset() function. The latter checks only whether … Read more

Categories PHP