Monday, 30 November 2015

42_introduction_of_XML_in_PHP

Introduction of XML


XML: - Extensible markup language. It creates custom tag that allows us to specific data and pick up data using different tag.





Example Site like facebook, youtube etc… Write the code in home.php page as specify below. One thing remember is that in PHP we have new function named simplexml_load_file() to load our .xml file into .php page





XML IS CASE SENSITIVE


1) using the variable $author_xml call the tag which you want and run the page.






XML IS CASE SENSITIVE


1) using the variable $author_xml call the tag which you want and run the page.






41_remember_user_ip_address

UNIQUE HIT COUNTER


The concept of unique hit counter is to remember/recording the user IP address.


1) Create homeunique.php, find_ip_address.php, remember_user_ip.txt and initialize some of the ip address.











2) Open find_ip_address.php and write the code as specify below.


3) Remember one thing in mind is if you read the data line by line basis the system will automatically place ONE BLANK SPACE.




4) Open homeunique.php files and write the code as specify below and run the page






















5)Here what we want is if found is false, means ip address not found then we want to increment our user number hit as well as append the current ip address in to remember_user_ip.txt file. So again we need to make some of the changes in our find_ip_address.php file.






6) For Checking purposes remove all the data from remember_user_ip.txt file and put zero in find_visit.txt file. Now run home_unique.php file nothing is going to display but if you check your both the text file then counter is incremented and user ip address is appended.



40_find_number_of_visitor_php

FIND VISIT IN OUR SITE.


1) Create two PHP page home.php, calculate_visit.php and one find_visit.txt file. Put initial value 0 [zero] in find_visit.txt file.


2) Open calculate_visit.php file and right the code as specify below.





3) Here in the first line we specify our text file very we have initialize value zero. In next line we are going to open the file in read mode. Then after we want to read all the data/size from text file so we use filesize() function of PHP.


4) Read the current value and closes the file. Now after reading the value we want to increment the current value with 1. So again open the file using fopen() function with mode “w”. Using fwrite() function write the incremented variable value and close it again.


5) Open home.php file, include calculate_visit.php file and call the function find_visiter().


6) Run the page home.php as many times as you want and check find_visit.txt file.



Sunday, 29 November 2015

39_only_images_specific_size_in_php

Checking file size


Checking file size in PHP, we can use $_FILE[“filename”][“size”]. When we use $_FILE[“filename”][“size”] in our program it will return the SIZE IN BYTES, and if we want the file 2MB size only.


2 MB =2097152 BYTES OR ANYTHING WHICH YOU WANT TO MAKE IT.


Here in the program we have done very simple if condition, where we are checking if $file_size<=$max_image_size.


< ? php
//Prepared By :- Harshida D Parmar
$uploded_file_name = $_FILES["filename"]["name"];
$file_extenstion=strtolower(substr($uploded_file_name,strpos($uploded_file_name,",")+1));
$file_type=$_FILES["filename"]["type"];
$file_size=$_FILES["filename"]["size"];
$max_image_size=200000;//2000 byte
$tmp_file_name=$_FILES["filename"]["tmp_name"];
if(isset($uploded_file_name)){
if(!empty($uploded_file_name)&&$file_size<=$max_image_size){
$file_location="../7_upload_file/upload_your_file/";
move_uploaded_file($tmp_file_name,$file_location.$uploded_file_name);
}else{
echo "CHECK IMAGE SIZE PLEASE";
}
}
?
>


UPLOAD ONLY IMAGE








Upload file
 


ONLY JPEG FILE
< ? php
//Prepared By :- Harshida D Parmar
$uploded_file_name = $_FILES["filename"]["name"];
$file_extenstion=strtolower(substr($uploded_file_name,strpos($uploded_file_name,".")+1));
$file_type=$_FILES["filename"]["type"];
$tmp_file_name=$_FILES["filename"]["tmp_name"];
if(isset($uploded_file_name) && !empty($uploded_file_name)){
if(($file_extenstion=="jpg" || $file_extenstion =="jpeg")&& $file_type=="image/jpeg"){
$file_location="../7_upload_file/upload_your_file/";
move_uploaded_file($tmp_file_name,$file_location.$uploded_file_name);
}else{
echo "CHECK IMAGE TYPE PLEASE";
}
}
? >

38_upolad_image_in_php

PHP: - UPLOAD FILES


To perform upload file operation we need to create one folder where we want to place our uploaded file and second one PHP page.




Here in above program we can see that we are able to display the file name, size, type and ONE OF THE IMPORTANT Part is $_FILE[“filename”][“tmp_name”] . The $_FILE[“filename”][“tmp_name”] is something different from the others, when we do process of file upload each file is sent to temporary location on your server and ready to do the process and store it some related folder.


Till now you can upload any file but if we forced user to upload only images file then we need to modify/add some PHP code in our page . Before do any operation let’s check it out our folder ”upload_your_file”.




NOW after uploading image to specific location check it out your folder.

Code with IMAGE ONLY

37_DELETE_AND_RENAMING_FILES_PHP

In this post we going to learn how to delete/rename the files in PHP. Create two text file 1) your_delete_file.txt and 2) your_rename_file.txt in your notepad. If user had uploaded one file and later if user wants to delete that file then in PHP we have function unlink (), using this function we can easily able to delete it.



Mostly this example is very useful for images, where user wants to upload new picture at that time delete the previous image as well. So let do this operation, in unlink function 1st argument is the filename which we want to delete.



Run your home.php two times, first time when you run the page then it will display the IF BLOCK statement, And second time if you run the page then ELSE block statement will be executed.



Output: - When you run the home.php page at first time




Output: - Run home.php page at second time you will get above warning message.


But if we don’t want the WARNING MESSAGE IF WE RUN THE PAGE AT SECOND TIME THEN just add “@” at the beginning of unlink function.



Run the page home.php again and yep warning message gone…


So here we have successfully delete the file from the folder now let’s understand how to rename the file using rename() function in PHP. As in the beginning we have created one text file your_rename_file.txt and we want to rename this file.


rename() function have two argument in the 1st argument is file which we want to rename and 2nd argument is new name with FILE EXTENSION.



Run your page name rename_file.php


Here you can see that we have our file name your_rename_file.txt but after running the rename_file.php our file name will be renamed.




Check it out your folder now…




Friday, 27 November 2015

36_Checking_File_exists_in_PHP

Checking File is exists in PHP


In PHP the image uploads / store operation if we want to check that specific files are exists or not, at that time we are going to perform the operation as specify below.



This will generate the random file name, while in the above code md5() function create 32 character hash from “image1.jpeg”.


The file name which is generated randomly will store on the server. But there is one problem during the live server, imagine that other user has upload the same image1.jpeg file and also the random number “194” and last random number “178” is same for both the user. So at that time we need to use file_exists () function of PHP.



So here in the above example is file does exists then do nothing else create file.


Very useful for storing the images in to the folder with random number. Now run the page file_exists.php page. It shows me nothing/ blank page. Go back from browser and check harshida.txt file is generated or not.


35_LIST_FILES_IN_PHP

LIST FILES IN PHP


Listing a file is very useful while doing the operation related to images, OR let’s says you want to find a image from a specific folder. So we are going to use two functions one is open directory and second is read directory.



So above program say that we are interested to find the folder/directory “file”.



Check it out in above output my “file” directory contains four text file, and I want to display each text file name using the loop.



However you can also see “.” And “..” in our output. This is just shows me no more directory structure.
“.” :- Means Current Directory
“..”:- Back a Directory
But during the output we don’t want Single and double “.”. So we need to add some of the condition in our code.




34_implode()_function_in_PHP

implode() function in PHP


Here we are going to learn another PHP function named implode (). Imagine that we have one array and we want to add comma at the end of each value. To do this operation implode() function is used in PHP.



User is allowed to add not only “,” but also “- , /, ” etc..



33_php_explode_function

PHP explode() function


filesize() contain the argument as a file name. This will give us the exact size of our file. Using fread() function we are going to read the data.



PHP contain one function named explode(), which contain argument
1st:- “,” , ”. ” , “/” etc … context which you want to remove from the word.
2nd:- file name from which file you want to remove the 1st argument value


check it out the code below.


explode() function return the data in the form of ARRAY. So either use foreach () loop to display data one by one or use print_r() function() of PHP.



Above example is display data using print_r() function of PHP.



you can see in above example display data using foreach loop. You are allowed to use $name_return_array[0] as well to display data based on key value.