Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. FILE UPLOAD To upload files from your browser to your hosting, using PHP,the first thing we need to do is create an HTML form that allows people to choose the file they want to upload . < form enctype=&quot;multipart/form-data&quot; action=&quot;upload.php&quot; method=&quot;POST&quot;> Please choose a file: <input name=&quot;uploaded&quot; type=&quot;file&quot; /><br / > <input type=&quot;submit&quot; value=&quot;Upload&quot; /> </form>
  2. 2. Continue... This form sends data to the file &quot;upload.php&quot;, which is what we will be creating next to actually upload the file. The actual file upload is very simple: <?php $target = &quot;upload/&quot;; $target = $target . basename( $_FILES['uploaded'] ['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded'] ['tmp_name'], $target)) { echo &quot;The file &quot;.
  3. 3. Continue... basename( $_FILES['uploadedfile'] ['name']). &quot; has been uploaded&quot;; } else { echo &quot;Sorry, there was a problem uploading your file.&quot;; }?> This very small piece of code will upload files sent to it by your HTML form. 1.The first line $target = &quot;upload/&quot;; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file
  4. 4. Continue... 2.We are not using $ok=1; at the moment but we will later in the tutorial. 3.We then move the uploaded file to where it belongs using move_uploaded_file () . This places it in the directory we specified at the beginning of our script. If this fails the is given an error message, otherwise they are told that the file has been uploaded.
  5. 5. SESSION A PHP session variable is used to store information about,change settings for a user session. Session variables holinformation about one single user, and are available to allpages in one application. Starting a PHP Session: Before you can store user information in your PHP session, you must first start up the session. <?php session_start(); ?> <html> <body> </body> </html>
  6. 6. Continue... The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session. Storing a Session Variable: The correct way to store and retrieve session variables is to use the PHP $_SESSION variable: <?php session_start(); // store session data $_SESSION['views']=1; ?> <html>
  7. 7. Continue <body> <?php //retrieve session data echo &quot;Pageviews=&quot;. $_SESSION['views']; ?> </body> </html> Output: Pageviews=1 Destroying a Session: If you wish to delete some session data, you can use the unset() or the session_destroy() function. ?>
  8. 8. Continue... The unset() function is used to free the specified session va riable: <?php unset($_SESSION['views']); ?> You can also completely destroy the session by calling the session_destroy() function: <?php session_destroy(); ?>
  9. 9. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. How to Create a Cookie? The setcookie() function is used to set a cookie. setcookie(name, value, expire, path, domain);
  10. 10. Continue... How to Retrieve a Cookie Value? The PHP $_COOKIE variable is used to retrieve a cookie value. Example <?php // Print a cookie echo $_COOKIE[&quot;user&quot;]; // A way to view all cookies print_r($_COOKIE); ?>
  11. 11. Continue.... How to Delete a Cookie? When deleting a cookie you should assure that the expiratio date is in the past . Example <?php // set the expiration date to one hour ago setcookie(&quot;user&quot;, &quot;&quot;, time()-3600); ?>
  12. 12. TYPES OF ERRORS 1.Notices: These are trivial, non-critical errors that PHP encounters while executing a script -for example, accessing a variable that has not yet been defined by default, such errors are not displayed to the user at all -although you can change this default behavior.
  13. 13. Continue... 2.Warnings: These are more serious errors – for example attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination. 3.Fatal errors: These are critical errors – for example, instantiating an object of a non- existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take plac
  14. 14. PHP error_log() Function The error_log() function sends an error to the server error log, a file or a remote destination. This funtion returns TRUE on success, or FALSE on failure. Syntax: error_log(error,type,destination,headers) PHP parse_ini_file() Function The parse_ini_file() function parses a configuration (ini) file and returns the settings in it in an array. Syntax: parse_ini_file(file,process_sections)