Explain how to store the uploaded file to the final location.
Correct Answer
A HTML form should be build before uploading the file The following HTML code is used for selecting the file that is to be uploaded The type attribute of input must be ?file? At the time of executing uploaderphp, the uploaded file will be stored in a temporary storage are on the webserver An associative array $_FILES['uploadedfile']['name'] is used for uploading The ?name? is the original file that is to be uploaded Another associative array $_FILES['uploadedfile']['tmp_name'] is used for placing the uploaded file in a temporary location on the server and the file should be empty and should exist with the tmp_name The following code snippet is used for uploading the file $target_path = "uploads/"; // the target location of the file $target_path = $target_path basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file " basename( $_FILES['uploadedfile']['name']) " has been uploaded"; } else { echo ?There was an error while uploading the file?; } The function move_uploaded_file()is used to place the source file into the destination folder, which resides on the server If the uploading is successful, the message ?The file filename has been uploaded Otherwise the error message ?There was an error while uploading the file? would be displayed
Technology problems
Search Results
1. Explain how to send large amounts of emails with php.
Correct Answer: The mail() function of PHP is quite robust for sending bulk emails A SMTP server can also be directly used from the script PHPmailer class can be used for sending emails
Correct Answer: To access the global data which is originating externally, the super globals are used The superglobals are available as arrays These superglobals represents the data from URLs, HTML forms, cookies, sessions and also the web server For this purpose, $HTTP_GET_VARS, $HTTP_POST_VARS are used The super globals are pretty good enough to be used in functions The following are the list of super globals $_GET, $_POST, $_COOKIE;$_REQUEST, $_SERVER, $_SESSION, $_ENV, $_FILE Another PHP Superglobal, called $GLOBALS; is available for persisting every variable with global scope
Correct Answer: Permissions in PHP are very similar to UNIX Each file has three types of permissions ? Read, write and execute Permissions can be changed using the change mode or CHMOD command CHMOD is followed by 3 digit number specifying the permission type CHMOD 777 is for Read, Write and execute Unnecessary permissions can be stripped off using UNMASK command Unmask (777);
4. What is the difference between $argv and $argc? Give example?
Correct Answer: To pass the information into the script from outside, help can be taken from the PHP CLI (Command line interface) method Suppose addition of two numbers has to be passed to PHP then it can be passed like this on the command line: php addphp 2 3 Here the script name is addphp, and 2 and 3 are the numbers that has to be added by the script These numbers are available inside the script in an array called $argv This array contains all the information on the command line; the statement is stored as follows: $argv[0]=addphp $argv[1]=2 $argv[2]=3 So, $argv always contains at least one element ? the script name Then, in your script, you can use the following statements: $sum = $argv[1] + $argv[2]; echo $sum; $argc is a variable that stores the numbers of elements in $argv $argc is equal to at least 1, which is saved for the name of the script Example is $argc=3 using the above statements
Correct Answer: $_SERVER and $_ENV arrays contain different information The information depends on the server and operating system being used Most of the information can be seen of an array for a particular server and operating system The syntax is as follows: foreach($_SERVER as $key =>$value) { echo ?Key=$key, Value=$value\n?; }
Correct Answer: Generating random passwords in PHP can be done in multiple ways depending on how much security the application demands:- Md5 function can be passed one parameter as uniqid() function which in turn generates a random number Passing the parameter as TRUE in uniqid() adds additional uniqueness
7. What type of inheritance that PHP supports? Provide an example.
Correct Answer: PHP supports single level inheritance Inheriting a class would mean creating a new class with all functionality of the existing class in addition to some more The created class is called as a subclass of the parent class Parent is a keyword which we use to access members of a parent class Inheritance is usually defined by using the keyword ?Extend? $this is used a reference to the calling object Inheritance avoids redundancy of code Example: Class employee extends manager : Here Employee is a subclass of manager Echo $this->name can be used to echo variable name of the parent class
8. Explain when to use GET or POST. Provide example for both the ways.
Correct Answer: Both GET and POST are used to collect data from a form However, when security is desired $_POST should be used When the $_ POST variable is used, all variables used are NOT displayed in the URL Also, they have no restrictions to the length of the variables GET should be used when the interaction with the form is more like a question For eg firing a query etc POST should be used more often when the interaction is more like an order or the interaction changes the state of the resource POST example: On clicking submit the URL resembles to: https://wwwmysamplesitecom/samplephp The samplephp file can be used for catching the form data using $_POST Hello You are years old! GET EXAMPLE On clicking submit the URL resembles to : https://wwwmysamplesitecom/samplephp?name=jim&age=37 The samplephp file can be used for catching the form data using $_GET Hello You are years old!
Correct Answer: The function setcookie() is used to define a cookie that is to be sent along with HTTP headers The cookie must be sent prior to any output from the script as is the protocol restriction After setting the cookies, they can be used when the next page is loaded by using $_COOKIE or $HTTP_COOKIE_VARS arrays
Correct Answer: A PHP Session persist the user information to be used later For example, user name, password, shopping item details The session is temporary and will be removed soon after the user has left the web site The session can be persisted for long term usage on databases like MySQL Each session is identified by a unique Id number for every visitor The first step to use PHP session is to start the session The starting session must precede the operations like HTML or the other The statement session_start() starts the PHP session and registers the user?s information on the server