PHP Questions

Q:

How to open a file?

Answer

<?php
$file = fopen("file.txt","r");
?>

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2449
Q:

Explain how to create random passwords.

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.


<?php
   $passwd = md5(uniqid(rand(), true));
   Echo $passwd;
?>

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2440
Q:

How to set cookies?

Answer

setcookie('variable','value','time');
variable - name of the cookie variable
value     - value of the cookie variable
time       - expiry time

Example:
<?php
setcookie('Test',$i,time()+3600);
?>
Test - cookie variable name
$i - value of the variable 'Test'
time()+3600 - denotes that the cookie will expire after an one hour

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2374
Q:

What is the difference between using copy() and move() function in php file uploading?

Answer

Copy() makes a copy of the file. It returns TRUE on success. It can copy from any source to destination. Move simply Moves the file to destination if the file is valid. While move can move the uploaded file from temp server location to any destination on the server. If filename is a valid upload file, but cannot be moved for some reason, no action will occur.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2371
Q:

How do you call a constructor for a parent class?

Answer parent::constructor($value)
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2370
Q:

Explain how to store the uploaded file to the final location.

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”.


<form enctype="multipart/form-data" action="uploader.php" method="POST">


<input type="hidden" name="MAX_FILE_SIZE" value="100000" />


Choose a file to upload: <input name="uploadedfile" type="file" /><br />


<input type="submit" value="Upload File" />


</form>


At the time of executing uploader.php, 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.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2369
Q:

What’s the difference between include and require?

Answer It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2363
Q:

What is a Session?

Answer

A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests. Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2350