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

Hello, my code is working but it is not opening the txt file to view the informa

ID: 3816182 • Letter: H

Question

Hello, my code is working but it is not opening the txt file to view the information. I am trying to get the code to open a .txt file to view the content while displaying "File Exist". Can someone help me find the missing piece?

<?php

// create an array to set page-level variables
$pae = array();

$page['title'] = 'Product Catalog';

/* once the file is imported, the variables set above will become available to it */

// include the page header

include('header.php');

?>

<html>

<head>

</head>

<body>

<?php

// if form has not yet been submitted

// display input box

if (!isset($_POST['file'])) {

?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    Enter file path <input type="text" name="file">
    </form>
   

<?php

}

// else process form input

else {

    // check if file exists

    // display appropriate message

    if (file_exists($_POST['file'])) {

        echo 'File exists!';

        }

    else {

        echo 'File does not exist!';

    }

}

?>

</body>

</html>

<?php

// include the page footer

include('footer.php');

?>

Explanation / Answer

<?php
// create an array to set page-level variables
$pae = array();
$page['title'] = 'Product Catalog';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('header.php');
?>
<html>
<head>
</head>
<body>
<?php
// if form has not yet been submitted
// display input box
if (!isset($_POST['file'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter file path <input type="text" name="file">
</form>


<?php
}
// else process form input
else {
// check if file exists
// display appropriate message
if (file_exists($_POST['file'])) {
echo 'File exists!';
       $myfile = fopen($_POST['file'], "r") or die("Unable to open file!");
       echo fread($myfile,filesize($_POST['file']));
       fclose($myfile);

}
else {
echo 'File does not exist!';
}
}
?>
</body>
</html>
<?php
// include the page footer
include('footer.php');
?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote