Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Modify add_quote.php from Chapter 11 so that it confirms that the quot

ID: 3848401 • Letter: Q

Question

Question:

Modify add_quote.php from Chapter 11 so that it confirms that the quotes.txt file exists prior to checking if it is writeable. Make the text area sticky.

Code:

<!doctype html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>Add A Quotation</title>
</head>
<body>
<?php // Script 11.1 - add_quote.php
/* This script displays and handles an HTML form. This script takes text input and stores it in a text file. */

// Identify the file to use:
$file = '../quotes.txt';

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

   if ( !empty($_POST['quote']) && ($_POST['quote'] != 'Enter your quotation here.') ) { // Need some thing to write.
  
       if (is_writable($file)) { // Confirm that the file is writable.
          
           file_put_contents($file, $_POST['quote'] . PHP_EOL, FILE_APPEND); // Write the data.

           // Print a message:
           print '<p>Your quotation has been stored.</p>';
      
       } else { // Could not open the file.
           print '<p>Your quotation could not be stored due to a system error.</p>';
       }
      
   } else { // Failed to enter a quotation.
       print '<p>Please enter a quotation!</p>';
   }
  
} // End of submitted IF.

// Leave PHP and display the form:
?>

<form action="add_quote.php" method="post">
   <textarea name="quote" rows="5" cols="30">Enter your quotation here.</textarea><br>
   <input type="submit" name="submit" value="Add This Quote!">
</form>

</body>
</html>

Explanation / Answer

Answer:

To check wheather file exists or not use this function

file_exists(path) where is the path of the file name. It returns 1 if file exists otherwise 0.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Add A Quotation</title>
</head>
<body>
<?php // Script 11.1 - add_quote.php
/* This script displays and handles an HTML form. This script takes text input and stores it in a text file. */

// Identify the file to use:
$file = '../quotes.txt';

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

    if ( !empty($_POST['quote']) && ($_POST['quote'] != 'Enter your quotation here.') ) { // Need some thing to write.
  
        if(file_exists("quotes.txt")){
       /*Give your full path name*/
       if (is_writable($file)) { // Confirm that the file is writable.
          
            file_put_contents($file, $_POST['quote'] . PHP_EOL, FILE_APPEND); // Write the data.

            // Print a message:
            print '<p>Your quotation has been stored.</p>';
      
        } else { // Could not open the file.
            print '<p>Your quotation could not be stored due to a system error.</p>';
        }}
       else echo "file is not exist";
      
    } else { // Failed to enter a quotation.
        print '<p>Please enter a quotation!</p>';
    }
  
} // End of submitted IF.

// Leave PHP and display the form:
?>

<form action="add_quote.php" method="post">
    <textarea name="quote" rows="5" cols="30">Enter your quotation here.</textarea><br>
    <input type="submit" name="submit" value="Add This Quote!">
</form>

</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote