Tag

PHP

Browsing

Today, We are going to learn PHP remove non-empty directory. PHP provides rmdir function using that we can easily remove any directory. But When you are going to remove a non-empty directory using rmdir it’s fire exception. You can’t able to remove non-empty directory message something like The directory is not empty.

PHP remove non-empty directory

So Let’s see, how we can remove a non-empty directory. I am going to share function with you using that function you can able to remove a non-empty directory.

First of all, Place this function with your code. Now, you just need to pass the directory path as a parameter to deleteDirectory function like.

finally, Now you can able to remove a non-empty directory. You can get more detail about this function from here.

If you face any issue after using this function write the comment below.

Today, We are going to learn PHP Converting Timestamp into a time ago. PHP provides date functions for print the date. PHP date function returns the date something like.

print date(“Y-m-d H:s:i”);

output : 2019-02-26 6:39:45

PHP date function returns date like the above format. Also, You can customize the output of date function using date options. But you want to display the time like a time ago then you can achieve that PHP code that we are going to learn today.

  • a second ago
  • a minute ago
  • a days ago
  • a month ago
  • a year ago

PHP Converting Timestamp into a time ago

We can easily get the date like above. I have created a function for convert date to time ago string. Using that function you can easily convert date and time.

Use the above function into your code and then pass the date and time argument on the function then check the output. let’s see the example of how to use converElapsedTime function.

echo converElapsedTime(‘2017-06-23 19:23:15’);

output : a year ago

So, It’s too easy to convert a date and time to time ago string. If face any issue after using this function please write the comment below.

Today, We are going to learn PHP remove HTML tags from a string. We can remove HTML tags from the string using two ways. First, PHP provides the strip_tags function for removing HTML tags and preg_replace function for removing HTML tags from a string, Today, We learn both functions with the example.

  • Remove HTML tags using strip_tags function
  • Remove HTML tags using preg_replace function

PHP remove HTML tags from a string

We are going to learn the first example using strip_tags function.

Remove HTML tags using strip_tags function

We are using the strip_tags function for removing the HTML tags from the string. So, it’s very easy to remove HTML tags from a string. But the “strip_tags” add space after removing the tags from the string like.

The output of the above statement is “Hello world!” instead of “Helloworld!”.

So, let’s see the second example for removing HTML tags from a string and get the output like “Helloworld!” instead of “Hello world!”.

Remove HTML tags using preg_replace function

The output of the above function is “Helloworld!” instead of “Hello world!”.

We check the both function with the example Now, choose the which function is helpful for you and use it for removing HTML tags from a string.

If you face any issue after using above code please write the comment below.

Today, We are going to learn programmatically PHP create ZIP file. PHP provides ZipArchive class using that we can easily create the zip file. But Today, I am going to share function with you using that function you can easily create the ZIP file by passing the directory name or file name.

Suddenly, My client tells me to move his project from one server to another server and his use the shared hosting. So, I have only FTP detail at that time I created “createZip” function for creating the zip file of files or directories. I am going to share that function with you So, you can easily create the zip file without hard work using PHP. You just need to write single line code for PHP creating ZIP file.

PHP create ZIP file

Just past the “createZip” function anywhere on your project then you just need to call that function for creating the zip file like

If you need to create the zip file of the current directory then you need to pass dot except for the directory path like

Or else you should pass the full directory path like

After, Creating the ZIP file if you want to move it another server without download it on your local system then You can follow my tutorial “Download Archive file from URL

If you face any issue after using this code please write the comment below.

Today, We are going to learn how to Download zip file from URL using PHP. PHP download archive file using single line of code. We can easily download the any file using PHP single line of code. We use file_put_contents for download file.

PHP Download Archive file

You can download any types of file using this function like JPG, JPEG, EXE, PNG, MP4, MP3 etc…

This trick is very useful when you want to move project to new server. We don’t need to download project to local system and then upload it to new server. Just create zip file of the project and create PHP script to new server then directly download project file on new server using PHP script.

If you face any problem after using this code please write a comment.

Today, We are going to learn How to create Category tree from Array using PHP. Many times we need to create the parent and child category tree. Let’s make it easy with a single function. Yes, You can create the parent and child tree from Array using a single PHP function. Let’s follow the function and pass Array for creating parent and child category tree.

Category tree from Array

Finally, Our function is ready Now, you just need to pass the category array-like.

Now, let’s pass this Array to buildTree then check the output.

We got output something like below.

So, Our category tree is ready with parent and child hierarchy.

If you face any issue after using this code please write a comment.

Nowadays, PHP is a highly used language for Web development. So, Today we are going to learn file upload functionality. PHP file upload functionality is very easy and almost every application has file upload functionality. So, We must have knowledge about How to upload file on PHP. So, let see how to upload a file using PHP programming. You must have some basic knowledge of HTML for creating form.

PHP File upload

First of all, We are going to create the HTML form. So, let’s create the fileupload.html HTML file. Then write the following code.

We are ready with the HTML form. Now, let’s create the PHP code for handling the uploaded file and store it into the directory. First of all, create the fileupload.php PHP file. Then write the following PHP code.

Finally, We are ready with the PHP file upload code. Before, running PHP file upload example please create the uploads directory at the root directory of your project. Then after selecting a file and upload it and check the file is stored on uploads directory.

If you face any issue after following the code please write the comment below. We are always ready for help.

In the last example, We learn about single line comment. In this example, We are going to learn PHP multi-line comments. In multi-line comment PHP allow us to write multiple lines as a comment. Every developer must write the comment. So, another developer easily understands the code.

PHP multi-line comments

First of all, We are going to create a new PHP file for multi-line comment example. So, Let’s create a new file at “C:\xampp\htdocs\first\thecodignstuff.php” then write the below code.

Finally, We are ready with a multi-line comment example. So, Let’s run the file and check the output like

Hello World!20

The comments are not visible on output it’s only usable for a developer. If you face any issue on this example write the comment below.

PHP stands for Personal Home Page. PHP also was known as Hypertext processor language. Today, We are going to learn PHP Single Line comment example. Every language provides comment system. A developer must have habits to write down comment before writing the code. So, when another developer starts working or trying to read code his easily understand.

PHP Single Line comment

Every developer must have habits to write the comment. Let’s see, How we can write the single line comment on PHP. So, First of all, We are going to create a PHP file at “C:\xampp\htdocs\first\comment.php” then write the code like

When we run our example our output something looks like this.

Hello World!20

The Comments are not visible to the user. it’s only for developer use. If you face any issue on this example please write the comment below.

PHP stands for Personal Home Page. PHP also was known as Hypertext processor language. HTML stands for Hypertext Markup Language. Today, We are going to learn how we can embedding PHP in HTML. Actually, We are going to embedding HTML in PHP file. HTML is client side language and PHP is server-side language. PHP manage backend login and passing data to HTML. HTML handle that data and display to a user. Today, We are going to embedding “Hello World!” example with HTML.

Embedding PHP in HTML

We are going to embedding HTML in PHP. So, First of all, We should create a PHP file for our example. Let’s create the file at “C:\xampp\htdocs\first\thecodingstuff.php” then write the code like.

So, it’s too easy We can easily embedding HTML in PHP file like above example. Now, let’s run our example by following “http://localhost/first/thecodingstuff.php” URL. If you face any issue in this example write the comment below.