Sunday, February 7, 2016

File Upload in PHP


Hello, How are you today?

Today we are going to upload a file using PHP and save it in the server.

When we upload a file it will be uploaded as a temporary file to the server. If we do not save this uploaded file in another directory in the server it will get deleted after the session is over.

First of all let's create the HTML form that helps us to upload the file.

<form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="uploadedFile" />
         <input type="submit"/>
      </form>

When creating the HTML form you have to keep two things in your mind.

  • Type of the input field should be file.
  • Form must have an enctype attribute set as enctype="multipart/form-data".
Now we have created the HTML form. Let's see how upload the file and save it using PHP.

PHP has a global variable called $_FILE to access the information of the uploaded file. This global variable has a two dimensional array which stores all the information about the uploaded file.
$_FILE has following variables:-
  • $_FILE['uploadedFile'][temp_name''] - This gives the name of the temporary file which has been created in the server.
  • $_FILE['uploadedFile']['error'] - This returns the error codes which occurred and associated with the file.
  • $_FILE['uploadedFile']['name'] - This returns the actual name of the uploaded file.
  • $_FILE['uploadedFile']['size'] - This returns the size of the uploaded file.
  • $_FILE['uploadedFile']['type'] - This returns the MIME type of the uploaded file.
Now let's upload and save the file in the server.


if(isset($_FILES['uploadedFile'])){
    
    $file_name = $_FILES['uploadedFile']['name'];
    $file_size = $_FILES['uploadedFile']['size'];
    $file_tmp = $_FILES['uploadedFile']['tmp_name'];
    $file_type = $_FILES['uploadedFile']['type'];
    
//getting the actual file extention
    $file_ext=strtolower(end(explode('.',$_FILES['uploadedFile']['name'])));
    
//checking whether the file is an excel file
    if(!($file_ext=="xlsx" || $file_ext=="xls")){
   
        $newName = "CTNS.".$file_ext;

//saving the file in new location called upload
         move_uploaded_file($file_tmp,'upload/'.$newName);
     }
}
 

Well now we have uploaded the file and saved in a new location in the server. Hope you can understand!

See you from another tutorial.

Comment box is open for Questions, Suggestions, Complaints and Comments.

Past Topics :-

Monday, January 4, 2016

Python Basics 02 - Data Structures


How are you today?

Today we are going to see how to define and initialize python variables and other data structures like arrays, etc.

Before we talk about variables, I want to tell you some additional but vary basic things in python.

print("This is the First Line",end=' ')
print("This is the Second Line")

In the above code end='' will tell the compiler to print the line break.

\t is used to print the tab space and \n is used to print the line break.

To generate random numbers in Python you have to import an external library.

import random
random.random() - This function will return a random  number between 0 and 1
random.randint(1,30) -  This function will return a random values between two specified integer values

Ok now, Let's start with Python Variables.
It is not very hard to create a variable in Python. You just have to give the name of the variable and assign the value to the variable as you do in other programming languages. Python compiler will identify the variable type using the value which is stored.

str = "This is a String Variable"
number = 23 //This is a Integer Variable
floating = 34.78233 // This is a floating point number

Well the variable initialization and deceleration in Python is simple as that.

But you have to keep in mind that if you save an number as a String variable type you will not be able to do any arithmetic calculations without converting that variable value to Integer type.

num1 = "23" //This variable holds 23 as a String Type values
num2 = num1*2 //If you try to do arithmetic operation like this you will get an error
If you want to do this calculation you have to convert the string value to Integer value first as follows.
num2 = int(num1)*2  //This will print the correct value

Now let's look in to LIST data structure in Python.

List is similar to array in PHP or Java but can grow in the size.

[] - This will create an empty list
numbers = [1,2,3,4,5,6] - This is an Integer List

Python allows nested lists too.
nestedList = [1,2,3,4,["One",''Two","Three''],5,6]

len(nestedList) - To get the length of the List
nestedList[4] - Returns the value of the 4th index of the List
nestedList[1:4] - Returns the values between 1 and 4 indexes of the List
nestedList[:4] - Returns the values of the indexes which are less than 4
nestedList[4:] - Returns the values of the indexes which are greater than 4

Now let's look in to DICTIONARY data structure in Python.

Dictionary data structure in Python allows you to save a values with a reference key just as a phone book saves the numbers with the name which refers to the number.

empty = {}  // This is an empty dictionary
phoneList = {"Dulanjana":"12345","Bhagya":"67890","Fernando":"23986"}

phoneList.keys() - This will return all the keys in phoneList dictionary
phoneList.values() - This will return all the values in phoneList dictionary
phoneList["Dulanjana"] - This will return the value which refer by the given key // 12345

Well, Hope you got something from this.
See you from another tutorial.

Past Topics :-