How to force download file from server-PHP

Welcome back to shortlearner.com, today in this post we will see how to force fully download a file from the server with the help of php.
In out previous post we learn how to import csv file into mysql database using php.

download file using php

so first of we need to understand overall scenario before downloading file from the server.
so i was created a image folder in a root directory of my project. there is a image file inside img folder with the name of test.jpg.

now we are writing a code for downloading this test image from the server without showing our img path to our end user because if we show our img folder in url than user can view all our images or files from the directory.

In the below code i am crating a index.php file which having a download button. so whenever user click on that button our download script will run and file will downloaded.

index.php

after click on the submit button we use the below code to download file from remote server to our system.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

Convert Html to PDF using JavaScript

Welcome back to shortleatner.com, today in this post we will see how to convert html to pdf with the help of JavaScript. in our last post we will see how to find first and last digit of a number without using any type of loop.

html to pdf

so most of the time we want to download a PDF file of our html view. like one of our reader is working on a POS system and he wants to print the html format bill to a PDF format.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

in the below code we add the bootstrap and jQuery maxcdn file at head section of our html page.
if you are looking for amazing html and bootstrap snippets than click on the below button for getting free amazing snippets.


Demo

find first and last digit of a number without using loop

Welcome back to shortlearner.com, in this post we will see how to write a program to find first and last digit of a number without using any type of loop.

so in the below program we are declaring a php variable $n which will takes the number.
here we are defining 2 php functions which will returns the first and last digit of our $n number.

find first and last digit of a number

the first function is firstNumber which will accept $n as a parameter than we use log10() function.

now we will take an short overview of log10() function. basically log10() is a mathematical function which is defined in math.h header file and it returns log base 10 value of the passed paramater.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

than we use pow() function. so basically pow() is a predefined function which will return the first number to the power of second number. in other words we can say that it compute power of a certain number. this function takes 2 parameter which are the base and exponent and returns the desired answer.

after using pow() function we will get first digit of our $n number. now we will declare our second function lastNumer which will help us to find out the last digit of $n number.

If these article helps you please share it with your developers buddy.
and if you are a web designer and looking for amazing snippets thank click on the below button.

Amazing Snippets

Keep Learning, Keep Coding

How to edit/update a txt file with php

Welcome back to shortlearner.com, in this post we will see how to update a txt file with the help of PHP.
So in this post we learn how to fetch data from the file and show in the text area and when user changes in it and hit on the update button file will update.

update txt file php


there is a myfile.txt file in my directory and in the below code i am fetching data from myfile.txt with the help of PHP and showing the data into text area.

Method 1:

in this method we fetch data from PHP file with the help of loop.

Method 2: file_get_contents

In this method we used PHP predefined function file_get_contents.
this function is use for get the contents of a file and store the content into a variable.
if we need to store content into a file than we also use file_put_contents which is works just opposite to file_get_contents funcion.

after showing the file data into text area we will update the data into file. so when user click on the update button the files will get open again and with the help of write method we writes the data into file and close the file.
here is the complete code.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

If these article helps you please share it with your developers buddy.
and if you are a web designer and looking for amazing snippets thank click on the below button.

Amazing Snippets

Keep Learning, Keep Coding

how to store data in cache file in php

welcome back to shortlearner.com, in this post we will see how to store data in cache file with the help of PHP.
before start this tutorial we should take an overview about cache.
so the very first question in our mind is what is cache , why we store our data in cache file, and what are the benefits to store data in this file.

data caching in php

so basically data caching refers to storing the result of an operation into a file so that future request return faster or we can say that caching is a perfect way to showing repeated data with minimum execution time.

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

in the below code we are creating a cache file with the help of php script and store data into that file and match the execution time of both actions.

so i have a table in MySQL database with the name of cities and this table have thousands of record in it. so whenever i use select query to fetch all records from the database it takes some time to execution which is more than 5 seconds. i want to reduce this execution time from 5 seconds to 0.005 seconds.

here i am using microtime() which is store the time before the script is start for execution. than we declare a variable of $cache_file and check that the file is exist or not.

so in the very first time this is not exist so we fetch all records from database and store in our $cache_file by using predefined file functions.
now when we perform the same task again it will fetch records from our $cache_file file which will exist this time.

and after the end we store execution time again in our $end variable and than check the difference between start and end time of execution so we got the actual execution time of this script.