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');
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.