Friday, December 11, 2015

How to upload a file on server using PHP?

Receive the file on server using the function

$_FILES['fieldname']

If provides different sub fields to get more information about the file being uploaded

$_FILES['fieldname']['type']' -- returns file type
$_FILES['fieldname']['name'] -- returns the name of the file
$_FILES['fieldname']['temp_name'] -- returns temporary file name of current file
$_FILES['fieldname']['size']   -- returns size of the file in bytes
$_FILES['fieldname']['error']  -- returns error code


Check whether a file exists or not

file_exists('filename with path') -- returns true if file exists

Now upload the file in the folder where you want

move_uploaded_file('temporaryname','actual filename with path')
 -- to move the file into specified folder from temporary folder






Functions used to work with database operations


mysql_connect('servername','userid','password','databasename')
                         -- connect with server and return reference of it

mysqli_query($dbc,$sql)
                        -- used to execute some SQL statement

mysqli_close();
                        -- close the database connection

What are super global variables used to take data from user into PHP?

$_GET['fieldname']  
                 - It can read data send by a query string only

$_POST['fieldname']] 
                 - It can read data send by a form with method="post" only

$ _REQUEST['fieldname']
                 - It can read data using get and post both