If you see a WordPress site, you will notice that at the bottom of the blog listing page, there will be links to next and previous pages.
They are useful to move the user to next or previous page.
If user is at the last page, then the next button will not be displayed.
Similarly, if user is at the first page, then the previous button will be hidden.
We are achieve this in PHP and MySQL. Following is the code to do that:
<?php
// Create a new PDO connection to the MySQL database
$pdo = new PDO("mysql:host=localhost; dbname=test", "root", "root", [
PDO::ATTR_PERSISTENT => true // Enables persistent connection for better performance
]);
// Define the number of records to show per page
$per_page = 2;
// Get the current page number from the query string, default to 1 if not set
$page = $_GET["page"] ?? 1;
// Calculate the offset for the SQL query
$skip = ($page - 1) * $per_page;
// Fetch the users for the current page, ordered by ID in descending order
$sql = "SELECT * FROM users ORDER BY id DESC LIMIT $skip, $per_page";
$stmt = $pdo->prepare($sql); // Prepare the SQL statement
$stmt->execute([]); // Execute the query
$users = $stmt->fetchAll(PDO::FETCH_OBJ); // Fetch all results as objects
// Query to get the total number of users in the database
$sql = "SELECT COUNT(*) AS total_count FROM users";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_OBJ);
$total_count = $row->total_count ?? 0; // Get the total count or default to 0
// Calculate the total number of pages required
$total_pages = ceil($total_count / $per_page);
// Determine if there are more pages
$has_more_pages = ($page * $per_page) < $total_count;
// Initialize previous and next page variables
$previous_page = $next_page = null;
// Determine the previous page number
if ($page > 1)
{
$previous_page = $page - 1;
}
// Determine the next page number
if ($page < $total_pages)
{
$next_page = $page + 1;
}
?>
<!-- Loop through and display each user -->
<?php foreach ($users as $user): ?>
<p><?php echo $user->name; ?></p>
<?php endforeach; ?>
<!-- Pagination controls -->
<?php if ($next_page > 0): ?>
<a href="?page=<?php echo $next_page; ?>">Previous</a>
<?php endif; ?>
<?php if ($previous_page > 0): ?>
<a href="?page=<?php echo $previous_page; ?>">Next</a>
<?php endif; ?>
Comments has been added with each line for explanation.
Even if you are working in any other language like Node.js and MongoDB, you can still achieve this because now you have the algorithm to do that.
From there, you will get the idea how you can render next and previous links if you are working in Node.js and MongoDB as backend and React as frontend.
I was looking for a tool that allows me to write API documentation so I can provide it to the frontend developers and also for myself. I checked many but didn’t find any that fits to all of my requirements. So I decided to create one of my own. Creating my own tool gives me flexibility to modify it as much as I want.
I also wanted it to share it with other developers who might be having problem in finding the tool to write documentation for their API. So I uploaded it on Github and it is available for anyone for free.
How to write API documentation
A tool is created in PHP and MySQL that allows developers to write API documentation, and this tool is available for free. You can create multiple sections to group the APIs based on modules. For example, user authentication, user posts, comments, replies can be separate sections.
To write each API, you need to tell:
The section where it goes.
The name of the endpoint. It can be the URL of API.
The method, it can be either GET or POST. But since you will have the code, you can add more methods as per your needs.
Add a little description about the API, for example what this API does.
Headers:
You need to tell the key of header, whether it is required or optional. And a little description about the header, for example, it’s possible values.
Parameters:
Parameters are usually send in the URL. You can define them along with their key, and value and whether they are optional or not.
Arguments:
For defining the arguments, you need to specify it’s type too. Whether it can be an integer, a string, boolean value or a file object.
Example request body. You can write the CURL code inside it to give an example.
Status codes and their responses.
I wrote a complete documentation of a project using this tool. You can check that from here.
Suppose you have a database where data of all players of any sports team is stored. Every player has a name and a shirt number. We will create a program that will generate a new number for new player. We will make sure the new number is not already assigned to any player. We will keep generating new numbers until a unique number is found.
First, create a table “players” in your database by running the following SQL query.
CREATE TABLE IF NOT EXISTS players2 (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
shirt_number INTEGER(11) NOT NULL
);
Then, create a file named “assign-shirt-number-to-new-player.php” and write the following code in it:
// connect with database
$pdo = new PDO("mysql:host=localhost; dbname=test;", "root", "", [
PDO::ATTR_PERSISTENT => true
]);
This will make a persistent connection with the database. We are using PHP PDO because it has built-in support for SQL injection. After that, you need to fetch all the players already saved in the database.
// fetch all the players from the database
$statement = $pdo->prepare("SELECT * FROM players");
$statement->execute([]);
$rows = $statement->fetchAll(PDO::FETCH_OBJ);
Then we will set the range of numbers in which we want to generate a new number.
// set number of range for shirt numbers
$number_range = 10;
This will generate numbers between 0 and 10. After that, we will create a recursive function that will keep generating a new number unitl a unique shirt number is found.
// recursive function to return the new shirt number
function get_code()
{
// define global variables to access them inside function
global $rows, $number_range;
// if all the numbers are taken, then return -1
if (count($rows) >= $number_range)
return -1;
// generate a random number
$code = rand(0, $number_range);
// check if it already exists in the database
foreach ($rows as $row)
{
if ($row->shirt_number == $code) // if number already exists, then start recursion i.e. generate another number
return get_code();
}
// if number not already exists, then return it
return $code;
}
We need to call this function once to start checking for new unique shirt number.
// initial call to recursive function
$code = get_code();
There is a possibility that all the numbers from 0 to 10 are already taken, in this case, we will display an error message.
// if all numbers are taken, display an error
if ($code == -1)
{
echo "<p>All numbers are taken.</p>";
exit;
}
If everything worked fine, we will have a new unique shirt number in our $code variable. We can simply insert it in our database.
// name of new player
$name = "Adnan";
// if shirt number not taken, then insert in database
$statement = $pdo->prepare("INSERT INTO players (name, shirt_number) VALUES (:name, :code)");
$statement->execute([
":name" => $name,
":code" => $code
]);
// display the new shirt number
echo "<p>New code: " . $code . "</p>";
Complete code
<?php
// connect with database
$pdo = new PDO("mysql:host=localhost; dbname=test;", "root", "root", [
PDO::ATTR_PERSISTENT => true
]);
// fetch all the players from the database
$statement = $pdo->prepare("SELECT * FROM players");
$statement->execute([]);
$rows = $statement->fetchAll(PDO::FETCH_OBJ);
// set number of range for shirt numbers
$number_range = 10;
// recursive function to return the new shirt number
function get_code()
{
// define global variables to access them inside function
global $rows, $number_range;
// if all the numbers are taken, then return -1
if (count($rows) >= $number_range)
return -1;
// generate a random number
$code = rand(0, $number_range);
// check if it already exists in the database
foreach ($rows as $row)
{
if ($row->shirt_number == $code) // if number already exists, then start recursion i.e. generate another number
return get_code();
}
// if number not already exists, then return it
return $code;
}
// initial call to recursive function
$code = get_code();
// if all numbers are taken, display an error
if ($code == -1)
{
echo "<p>All numbers are taken.</p>";
exit;
}
// name of new player
$name = "Adnan";
// if shirt number not taken, then insert in database
$statement = $pdo->prepare("INSERT INTO players (name, shirt_number) VALUES (:name, :code)");
$statement->execute([
":name" => $name,
":code" => $code
]);
// display the new shirt number
echo "<p>New code: " . $code . "</p>";
In order to check if a URL is valid in PHP, we are going to use the cURL. You can get the complete reference on cURL from php.net. First, we are going to create a variable that will hold the value of URL that needs to be checked.
<?php
// URL to check
$url = "https://adnan-tech.com/";
Then, we are going to initialize the cURL.
// Initialize cURL
$curl = curl_init();
After that, we need to set the URL of cURL and make sure to return the response from server. For this, we will use cURL options.
// Set cURL options
curl_setopt_array($curl, [
// Set URL
CURLOPT_URL => $url,
// Make sure to return the response
CURLOPT_RETURNTRANSFER => 1
]);
Finally, we can execute this cURL request.
// Execute the CURL request
$response = curl_exec($curl);
Then, we will get the error from response (if there is any). If there is no error in response, it means that the URL is valid and the curl_error will return an empty string.
// Get error from CURL, it will be empty if there is no error.
$error = curl_error($curl);
We can put a condition on this variable to check if this variable has some value, it means there is an error in the request. In that case, we will display the error and stop the script. Otherwise, we will display a message that the URL is valid.
// If not empty, display the error and stop the script.
if ($error)
die($error);
// Else, the URL is valid
echo "Valid";
Here is the complete code.
<?php
// URL to check
$url = "https://adnan-tech.com/";
// Initialize CURL
$curl = curl_init();
// Set CURL options
curl_setopt_array($curl, [
// Set URL
CURLOPT_URL => $url,
// Make sure to return the response
CURLOPT_RETURNTRANSFER => 1
]);
// Execute the CURL request
$response = curl_exec($curl);
// Get error from CURL, it will be empty if there is no error.
$error = curl_error($curl);
// If not empty, display the error and stop the script.
if ($error)
die($error);
// Else, the URL is valid
echo "Valid";
That’s how you can check if a URL is valid or not in PHP.
Suppose you have custom post type in WordPress that you want to sort such that it displays the first created post on top. By default, WordPress sort the posts by ID in descending. That means that the latest uploaded post will be displayed on top, which is good in many cases. But there might be a scenario where you want the first post to be displayed first. In this post, we will teach you how to do it using our Social Network API custom post type.
First, you need to go to your WordPress admin panel and navigate to Appearance -> Theme File Editor. In that page, select your current theme and find it’s “index.php” file and open it. In this file, search for a code:
<?php if ( have_posts() ) : ?>
Inside this “if” condition, and before the while loop:
<?php while ( have_posts() ) : the_post(); ?>
Write the following code, make sure to replace “social-network-api” with your own custom post type where you want to apply this sort.
In this article, I will show you, how you can generate a random string in PHP. You can set the length of random string. Also, you can define all the possible characters you want in your random string.
In order to generate random string in PHP, we will first create a string of all the possible characters we want in our random string.
$str = "qwertyuiopasdfghjklzxcvbnm";
Then we are going to get the length of that string.
$str_length = strlen($str);
After that, we will create a variable that will hold the output value i.e. random string.
$output = "";
Loop through the number of characters you want in your random string.
Freelance Status is a tool for freelancers to update clients about their work progress. You can download it for free.
Technologies used
Laravel
I have used Laravel framework for building this project basic structure and APIs. It is also a very good framework with many built-in security features. It can also be deployed on shared hosting as well.
React JS
All the pages on client and admin side are rendered in React JS. This is not a Single Page Application (SPA) but the HTML is rendered by React JS.
Bootstrap
Bootstrap is used for building structure and layout of website. It is also very helpful for responsive design.
Features
In “Freelance Status”, there are 2 panels. One for freelancer and one for client. Freelancer portal is actually the admin panel because he can add his clients, projects and tasks. He can update the status of his tasks etc. But client can only see the progress of their tasks from user side.
Admin panel (freelancer)
You (as a freelancer) holds the admin panel. You can:
Add, edit or delete clients. While adding clients, you can enter their name, email, phone (if have) and password. You can let them know their password via message or inbox so they can access the client side.
You can add projects for each client. While adding projects, you can enter the name of the project. Then you can select the client of that project via dropdown. In dropdown, you will see a list of all clients added from previous step.
Similarly, you can add tasks of each project. For adding tasks, you have to provide many values.
Task title and its description.
The price of that task.
The status (todo, progress or done).
The payment status. It can be either (paid, not paid, not finalized). Payment “not finalized” means that the amount is not yet decided between client and freelancer. But you are saving the task so it can be discussed later.
Select the client.
Select the project this task belongs to.
Client side
Clients will first have to login using the email and password provided by freelancer.
After login, they will see all their projects on home page.
With each project, they will see a link “View tasks”. On clicking that, they will be redirected to a new page.
On this page, they can see a list of all tasks in that project. They can also see the price, status and payment status of each task.
With each task, a button saying “Detail” is displayed. When clicked, will display a modal containing the details of the task.
Installation
Following steps helps you install this project on your localhost. If you want to learn how to deploy this on your live server, check our this guide.
Step 1
First you need to create a database named “freelance_status”, or any other name of your choice, in your phpMyAdmin.
Step 2
Then you need to set your database credentials in “config/database.php” file. Here you can set your database name, username and its password.
Note: If you are working on live server, make sure you have given that user all permissions to your database.
Step 3
Open command prompt or terminal at the root of this project and run the following commands:
COMPOSER_MEMORY_LIMIT=-1 composer update
This will install all the required libraries for this project. Make sure you have composer installed in your system. COMPOSER_MEMORY_LIMIT=-1 will prevent you from timeout exception.
Note: For live server, you can talk with your hosting provider customer support to install composer.
Step 4
Set storage link in your public folder by running the following command:
php artisan storage:link
This will create a shortcut link of storage/app/public folder into your public folder. It allows you to access your files stored in storage from public directory.
Step 5
Next step is to generate an application key for your project.
php artisan key:generate
This will generate a random string of 32 characters. The key will automatically be saved in APP_KEY variable of your .env file.
Step 6
If you have set your database credentials, you can run the following command.
php artisan migrate
It will create all the tables required for your project.
Step 7
After running the migration, we need to run the seeder.
A super admin will be added in “users” table. You can set your own name, email and password here.
Step 8
Finally you can run your project by running the following command.
php artisan serve
If you run the URL “http://127.0.0.1:8000” in your browser, you will see your project. If anything goes wrong, feel free to contact me.
“Freelance Status” allows you to add clients, their projects and tasks from admin panel. And clients can see them trough a user side. Clients do not have to keep asking you about update on the project. And you can focus more on work than on updating the client about progress.
In this blog post, you will learn how you can capitalize a string in PHP. We will not use any external library for this purpose. This code can be used in any PHP framework (Laravel, Yii) because I have not used any dependency for it. We will also teach you, how you can update the array element inside foreach loop in PHP.
First, we will explode the string by space.
<?php
// input string
$str = "adnan afzal";
// split words by space
$parts = explode(" ", $str); // ["adnan", "afzal"]
explode(delimiter, string): This is a PHP built-in function used to split the string into an array using a delimiter. Here, we are splitting the string using space. So it will return an array of all words in a string.
Then we will loop through all words one-by-one:
// loop through each word
foreach ($parts as $key => $value)
{
// [section-1]
}
Inside the loop, we will update the word by uppercasing the first letter of each word. Write the following code in [section-1]:
// make first letter uppercase
// and update the word
$parts[$key] = ucfirst($value); // converts ["adnan"] to ["Adnan"]
ucfirst(string): This is another PHP built-in function and it is used to uppercase the first letter of the string. So “adnan” will became “Adnan”.
We are converting all the words first letter to uppercase and update the word itself. You can update the array value inside foreach loop using the $key variable.
After the loop, we will join all array parts by space.
// join all words by space
$str = implode(" ", $parts); // converts ["Adnan", "Afzal"] to "Adnan Afzal"
// output string
echo $str;
implode(glue, pieces): This function will join the array into a string using a glue. In this case, we are joining the array using space.
Complete code
Following is the complete code to capitalize the string in PHP:
<?php
// input string
$str = "adnan afzal";
// split words by space
$parts = explode(" ", $str); // ["adnan", "afzal"]
// loop through each word
foreach ($parts as $key => $value)
{
// make first letter uppercase
// and update the word
$parts[$key] = ucfirst($value);
}
// join all words by space
$str = implode(" ", $parts);
// output string
echo $str;
Bonus
There might be some cases where you have a value in database like “processing_order” and you want it to display like “Processing Order”. In this case, you just have to add 1 line before using explode() function.
JWT (Json Web Tokens) can be used for user authentication. They are token-based. First, you need to encode the token and send it to the client. Client will save it in his local storage or cookie. In order to decode the token, user must provide the token. Both encode and decode function on JWT wll be performed on server side in PHP.
Or you can download and include it manually in your PHP project.
Encode JWT in PHP
When admin is logged-in, we need to generate his authentication token. First, you need to include the JWT and Key class on top of your file.
require_once "vendor/autoload.php";
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
Following code will generate a JWT using ID.
$jwt_key = "your_secret_key"; // This should be consistent
$issued_at = time();
// $expiry = strtotime("+30 days");
// jwt valid for 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
$expiry = $issued_at + (60 * 60 * 24 * 30);
$payload = [
'iss' => 'https://your-website.com',
'aud' => 'https://your-website.com',
'iat' => $issued_at, // issued at
'nbf' => $issued_at, // not before
'exp' => $expiry, // expiry time
"id" => 1
];
$jwt = JWT::encode($payload, $jwt_key, 'HS256');
Here, we have set the expiry date of this token to 30 days.
iss: Issuer. It tells who issued this token. Usually it is a URL of the server from where the JWT is being generated.
aud: Audience. This tells who can use this token. By providing the URL of your website, you are telling that this token will be valid if it comes from your website only. You can also pass it as an array if you want to allow multiple audience for same token.
iat: It will be a timestamp in seconds since January 01, 1970 00:00:00 UTC.
nbf: The timestamp seconds after which the token can be used. Token provided before this time will not be accepted. We are setting it same as issued time, so you will be able to use it as soon as you generate it.
exp: This will be the timestamp in seconds when the token will be expired. It will also be timestamps seconds since January 01, 1970 00:00:00 UTC. We are setting it’s validity for 30 days.
id: Optional. This is the custom claim that we are attaching with JWT. So when we decode the token, we will get this ID. This ID can be used to check if the user still exists in the database.
Here we are using HS256 algorithm that is used for authentication in JWT. It is a combination of 2 cryptographic methods:
HMAC (Hash-based Message Authentication Code)
SHA-256 (Secure Hash Algorithm 256 bit)
HMAC
HMAC combines cryptographic hash function with a secret key, in this case it will be $jwt_key. It ensures that the token is not been changed and is sent from the owner of secret key (your server).
SHA-256
SHA-256 generates a 256-bit hash from any given value. It is a one-way encryption method. It means that once the hash is generated, it cannot be decrypted back to its original state. So you will not be able to see the original message once it is encrypted.
So in JWT::encode function call, we are sending our payload and secret key. We are also telling the php-jwt library to use the HS256 algorithm. It takes our payload and encrypt it with our secret key and return the hash.
You do not have to save this token in your database. Just return it as a response to AJAX request and user can save it in his local storage.
localStorage.setItem("accessToken", accessToken)
To save the token on user side, we are using localStorage web API. This will keep the access token in local storage until removed using Javascript localStorage.removeItem(“accessToken”) or if the browsing history is deleted. However, it can store only upto 10 MB. But that will be enough in this case. The token generated will be a string of just a few bytes.
It stores the data in key-value pair. In this case, our key is “accessToken” and our value is the accessToken received from server.
Note: The first parameter is a string and second is a variable.
Decode JWT in PHP
Now whenever admin sends an AJAX request, he needs to pass that access token in headers.
Here, we are first creating an AJAX (Asynchronous JavaScript and XML) object. Then we are opening the request with method POST and the server URL is “index.php”, you can write your own route. Third parameter is async, it’s default value is also true. It means that the request will be processed asyncronously. If you use false, it will not return any value until the response is received, thus blocking the UI which is not good for user experience.
After that, we are attaching Authorization header with the AJAX request. It is a good practice to send authorization tokens in header for security and it is also a standardized way of sending tokens. So if you are developing a website or a mobile application, every developer will know that the token needs to be sent in the header.
We are using Bearer authorization token because they hide the sensitive data that we are sending in payload. Thus, even if someone reads the headers, he won’t be able to read the ID of user. And we are fetching the token value from local storage we saved earlier.
Finally, we are sending the request. If you are to send some data with the request too, you can send it in FormData object. Check our tutorial on how to send AJAX request with FormData.
Then on server side, we need to get the access token from Authorization header and decode it to see if it is valid.
// index.php
// Get the JWT from the Authorization header
try
{
$headers = getallheaders(); // returns an array of all headers attached in this request
if (!isset($headers['Authorization']))
{
echo json_encode([
"status" => "error",
"message" => "'authorization' header not present."
]);
exit();
}
$auth_header = $headers['Authorization'];
list($jwt) = sscanf($auth_header, "Bearer %s");
$decoded = JWT::decode($jwt, new Key($jwt_key, 'HS256'));
$id = $decoded->id;
return $id;
}
catch (\Exception $exp)
{
echo json_encode([
"status" => "error",
"message" => $exp->getMessage()
]);
exit();
}
First, we are fetching all the headers from the request. Then we are checking if the Authorization header is present in the request. If not, then we are throwing an error. So if user fails to attach the Authorization header in the request, it will not process further.
sscanf: This function is used to read and parse the value from a string. While “sprintf” is used to only format the data, but this function reads the data and parses as well.
Our format “Bearer %s” tells the function to expecct Bearer at the start of the string. And %s tells it to put the remaining string in a variable.
list() is a built-in function in PHP. It is used to assign multiple values to multiple variables respectively. In this case, we are assigning value of “%s” to $jwt variable.
After that, we are decoding the token. Decode function in JWT PHP is a little different than the encode function. First parameter is the token, second is an instance of Key class from php-jwt library. It also accepts 2 parameters, first is the secret key used to generate the token and second is the algorithm used while generating the token. If there is any mismatch in token, secret key or algorithm, it will throw an exception and we will receive that in catch block.
It will return the payload that we are saving in $decoded variable. From this decoded variable, you can get the ID of the authenticated user.
That’s how you can encode and decode the JWT in PHP. If you face any problem in following this, kindly do let me know.
I created an online free FTP manager in PHP that allows developers to work on their projects from anywhere. I created this tool in PHP and MySQL using Laravel framework. The frontend is designed in Bootstrap and React JS.
Let’s discuss each feature and I will also show you how I built this.
What you will learn:
Connect with FTP.
List files from FTP directory.
Fetch file content.
Edit FTP file.
Create a new file.
Create a new folder.
Upload files.
Download files.
Rename file.
Delete file.
Delete folder.
We created a PHP class named “FTP” in a file called “FTP.php”. All our functions will go in that class.
1. Connect with FTP
The first step is to connect with FTP server. Following code will create a function that will connect with your FTP server (we are not calling it yet).
class FTP
{
private $conn_id = null;
private $server = "";
private $username = "";
private $password = "";
private function do_connect()
{
try
{
// Establishing connection
$this->conn_id = ftp_connect($this->server);
if (!$this->conn_id)
{
echo json_encode([
"status" => "error",
"message" => "Could not connect to " . $this->server
]);
exit();
}
// Login with username and password
$login_result = ftp_login($this->conn_id, $this->username, $this->password);
if (!$login_result)
{
echo json_encode([
"status" => "error",
"message" => "Wrong password for '" . $this->username . "'."
]);
exit();
}
// Enable passive mode for better compatibility
ftp_pasv($this->conn_id, true);
}
catch (\Exception $exp)
{
echo json_encode([
"status" => "error",
"message" => "Could not connect to " . $this->server
]);
exit();
}
}
}
Comments has been added with each line for explanation. Make sure to enter your correct FTP server address, FTP account’s username and its password. In the next step, we will call this function to connect with FTP server.
2. List files from FTP directory
The next step is to list all files from FTP directory. Create the following function in your FTP class.
public function fetch_files()
{
$this->do_connect();
$path = "/directory-name";
// Get file listing
$file_list = ftp_nlist($this->conn_id, $path);
if ($file_list === false)
{
echo json_encode([
"status" => "error",
"message" => "Error retrieving file list."
]);
exit();
}
$files = [];
foreach ($file_list as $file)
{
$obj = [
"file" => $file,
"size" => ftp_size($this->conn_id, $file)
];
if ($obj["file"] != "." && $obj["file"] != "..")
{
array_push($files, $obj);
}
}
// Close the connection
ftp_close($this->conn_id);
echo json_encode([
"status" => "success",
"message" => "Data has been fetched.",
"files" => $files
]);
exit();
}
This function will first connect with the FTP server.
Then it will fetch all the files from $path variable.
If the directory does not exists or if it does not have read permission, then it will return an error.
All files from that directory will be put in an array along with the size of the file in bytes.
If it is a directory, then it’s size will be -1.
Finally, it returns the array with the response.
3. Fetch file content
Reading the content of FTP file is necessary because in order to edit the file, we need to first view the file. Following function will fetch the content of file and return it in response.
public function fetch_content()
{
$this->do_connect();
$file = "/directory-name/file-name.php";
// Local path for downloading the file
$local_file = tempnam(sys_get_temp_dir(), 'ftp_download');
// Download the file from FTP server to local file
if (!ftp_get($this->conn_id, $local_file, $file, FTP_BINARY))
{
echo json_encode([
"status" => "error",
"message" => "Failed to read '" . $file . "'"
]);
exit();
}
// Read contents of the local file
$file_content = file_get_contents($local_file);
// Display or process the file content as needed
if ($file_content === false)
{
echo json_encode([
"status" => "error",
"message" => "Failed to read file content."
]);
exit();
}
// Clean up: Delete the temporary local file
unlink($local_file);
echo json_encode([
"status" => "success",
"message" => "Data has been fetched.",
"content" => $file_content
]);
exit();
}
It will first download the file in a temporary directory to your server.
If the file does not exists, then it will return an error.
It will return an error if it fails to read the FTP file.
Usually it is because if the $path variable is a directory or an image or document file.
After the file’s content is fetched, we will remove the temporary file from our server.
4. Edit FTP file
After the file’s content is successfully fetched, the next step is to update the file. You are working in FTP, editing the code is what you will be doing most of the time.
Updating the content of file requires 2 steps:
Saving the file in temporary folder.
Uploading the temporary file on FTP server.
public function update_content()
{
$this->do_connect();
$file = "/directory-name/file-name.php";
$content = "console.log(\"Hello world\")";
// Local path for downloading the file
$local_file = tempnam(sys_get_temp_dir(), 'ftp_download');
// Write the new content to a local temporary file
file_put_contents($local_file, $content);
if (!ftp_put($this->conn_id, $remote_file, $local_file, FTP_ASCII))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem updating '" . $remote_file . "'"
]);
exit();
}
unlink($local_file);
// Close the FTP connection
ftp_close($this->conn_id);
echo json_encode([
"status" => "success",
"message" => "File has been updated."
]);
exit();
}
5. Create a new file
Creating a new file in FTP in PHP is as simple as updating the file. We just need to set the content of file as empty string. So we will be uploading an empty file to FTP server.
public function create_file()
{
$this->do_connect();
$path = "/directory-name";
$name = "file-name.php";
// Local file content to be uploaded
$file_content = "";
// Local path for downloading the file
$local_file = tempnam(sys_get_temp_dir(), 'ftp_download');
// Write the content to a temporary local file
file_put_contents($local_file, $file_content);
// Remote file path where the file will be created
$remote_file = $path . "/" . $name; // Replace with the desired remote file path
// Upload the local file to the FTP server
if (!ftp_put($this->conn_id, $remote_file, $local_file, FTP_ASCII))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem while creating '" . $name . "'"
]);
exit();
}
unlink($local_file);
// Close the FTP connection
ftp_close($this->conn_id);
echo json_encode([
"status" => "success",
"message" => "Successfully created '" . $name . "'"
]);
exit();
}
Create a temporary empty file on local directory.
Upload it to FTP.
Remove the file from local directory.
6. Create a new folder
Creating a folder is must easier than creating a file on FTP server.
public function create_folder()
{
$this->do_connect();
$path = "/directory-name";
$name = "folder-name";
$folder = $path . "/" . $name;
if (!ftp_mkdir($this->conn_id, $folder))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem while creating '" . $name . "'"
]);
exit();
}
echo json_encode([
"status" => "success",
"message" => "Successfully created '" . $name . "'"
]);
exit();
}
Note: It will return an error if the directory already exists.
7. Upload files
To upload the file from local computer to FTP server, you need to first save it in your local server. Then upload the file to FTP server. After it is successfully uploaded, you can remove the file from local server.
public function upload()
{
$this->do_connect();
$path = "/directory-name";
$file_name = basename($_FILES["file"]["name"]);
$target_file = "uploads/" . $file_name;
if (!move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
echo json_encode([
"status" => "error",
"message" => "The file " . htmlspecialchars(basename( $_FILES["file"]["name"])) . " has been uploaded."
]);
exit();
}
if (!ftp_put($this->conn_id, $file_name, $target_file, FTP_ASCII))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem updating '" . $file_name . "'"
]);
exit();
}
// Close the FTP connection
ftp_close($this->conn_id);
echo json_encode([
"status" => "success",
"message" => "File(s) has been uploaded."
]);
exit();
}
If you are working in Laravel, your upload function should be like this:
public function upload()
{
$this->do_connect();
$path = "/directory-name";
$files = request()->file("files");
foreach ($files as $file)
{
$remote_file = $file->getClientOriginalName();
$file_path = "ftp/" . $file->getClientOriginalName();
$file->storeAs("/private", $file_path);
if (!ftp_put($this->conn_id, $remote_file, storage_path("app/private/" . $file_path), FTP_ASCII))
{
return response()->json([
"status" => "error",
"message" => "There was a problem updating '" . $remote_file . "'"
]);
}
Storage::delete("private/" . $file_path);
}
// Close the FTP connection
ftp_close($this->conn_id);
return response()->json([
"status" => "success",
"message" => "File(s) has been uploaded."
]);
}
8. Download files
Downloading the file to local computer requires 2 steps.
First is to download the file on local server from FTP server.
Then download it to local computer from local server.
Download file from FTP to local server
Following code will be used to download the file on local server.
public function download()
{
$this->do_connect();
$path = "/directory-name";
$file = "file-name.php";
// Local path for downloading the file
$local_file = tempnam(sys_get_temp_dir(), 'ftp_download');
$remote_file = $path . "/" . $file;
// Download the file from FTP server to local file
if (!ftp_get($this->conn_id, $local_file, $remote_file, FTP_BINARY))
{
echo json_encode([
"status" => "error",
"message" => "Failed to download '" . $remote_file . "'"
]);
exit();
}
// Create a unique download URL for the file
$download_url = url('download.php?file=' . urlencode($local_file) . '&name=' . urlencode($file));
// Close the FTP connection
ftp_close($this->conn_id);
echo json_encode([
"status" => "success",
"message" => "File has been downloaded.",
"download_url" => $download_url
]);
exit();
}
It will also return the “download_url” variable. That will be link to file from where you can download the file.
Download from server to local computer
Create a new file named “download.php” in your server and write the following code in it:
if (isset($_GET['file']) && isset($_GET['name']))
{
$temp_file = $_GET['file'];
$file_name = $_GET['name'];
if (file_exists($temp_file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($temp_file));
// Read the file and output it to the browser
readfile($temp_file);
// Delete the temporary file
unlink($temp_file);
exit();
}
echo "File does not exist.";
exit();
}
echo "Invalid request.";
exit();
9. Rename file
Renaming the file on FTP using PHP is very easy. You just have to provide 3 things:
Folder name
Old file name
New file name
public function rename()
{
$this->do_connect();
$path = "/directory-name";
$name = "new-file-name.php";
$file = "old-file-name.php";
// File to be renamed on the FTP server
$remote_file = $path . "/" . $file;
$remote_new_file = $path . "/" . $name;
// Try to rename the file or folder
if (!ftp_rename($this->conn_id, $remote_file, $remote_new_file))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem renaming " . $file . " to " . $name
]);
exit();
}
echo json_encode([
"status" => "success",
"message" => "Successfully renamed " . $file . " to " . $name
]);
exit();
}
10. Delete file
Deleting a file from FTP in PHP is very simply. You just need to enter the full path of the file and call the PHP built-in ftp_delete function. Pass the FTP connection ID and file path as arguments.
public function delete_file()
{
$this->do_connect();
$path = "/directory-name/file-name.php";
// Delete the file on the FTP server
if (!ftp_delete($this->conn_id, $path))
{
echo json_encode([
"status" => "error",
"message" => "There was a problem while deleting " . $path
]);
exit();
}
echo json_encode([
"status" => "success",
"message" => "Successfully deleted " . $path
]);
exit();
}
11. Delete folder
Deleting a folder is NOT as simple as deleting a single file. There are 2 steps involved in deleting a folder from FTP in PHP.
Recursively delete all files and folders inside that folder.
Delete the folder itself.
So first we will create a function that will initialize the call to recursive function.
public function delete_folder()
{
// Set maximum execution time to unlimited (0 means no limit)
set_time_limit(0);
$path = "/directory-path/folder-name";
$this->do_connect();
$this->ftp_delete_dir($path);
echo json_encode([
"status" => "success",
"message" => "Folder has been deleted."
]);
exit();
}
Our recursive function ftp_delete_dir will look like this:
// Function to recursively delete a directory
private function ftp_delete_dir($path)
{
// Get the list of files in the directory
$files = ftp_nlist($this->conn_id, $path);
// Loop through each file
foreach ($files as $file)
{
$file = basename($file);
if ($file == '.' || $file == '..')
{
continue;
}
$file_path = "$path/$file";
// If it's a directory, delete recursively
if (@ftp_chdir($this->conn_id, $file_path))
{
ftp_chdir($this->conn_id, '..');
ftpDelete($this->conn_id, $file_path);
}
else
{
// If it's a file, delete it
ftp_delete($this->conn_id, $file_path);
}
}
// Finally, remove the directory itself
ftp_rmdir($this->conn_id, $path);
}
It does the following things:
Loops through all the files inside that folder.
Skip the “.” and “..” folders (they are just pointing towards current and parent directory respectively).
If the file is a directory, start the recursion.
Else delete the file.
Finally at line #33, we are deleting the current directory itself.
So these are all the features we have in FTP manager I created in PHP and MySQL using Laravel framework. If you have any quetions regarding FTP or if you are having any problem, feel free to contact me.