My php script is supposed to simply take a filename and within a specific directory outputs the filename’s contents. The issue is that it does that BUT it apparently once again calls the case resulting in throwing errors, I have tried to replace switch with if/else but still it recalls itself(The code related to case:”1″). Here are the error:
Warning: fopen(C:/xampp/htdocs/phptut/practice/) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\phptut\practice\fileuploadread.php on line 8
Warning: fread() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\phptut\practice\fileuploadread.php on line 9
Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\phptut\practice\fileuploadread.php on line 10
And This is the code:
switch($_GET['id']){
case"1":
{ //here $file gets the pathtothefile + filename
$file="C:/xampp/htdocs/phptut/practice/" . $_POST["element"];
$fh=fopen($file,"r");
$contents=fread($fh,filesize($file));
fclose($fh);
//It does output the $contents at this point
echo $contents;
}
break;
default:{
echo"
<html>
<head>
<style type='text/css'>
fieldset{
background-color:lightblue;
border-color:blue;
width:50%;
margin-left:auto;
margin-right:auto;
margin-top:200px;
}
legend{
color:blue;
}
</style>
</head>
<body>
<fieldset>
<legend> Upload Your CV </legend>
<form method='POST' name='form' action='fileuploadread.php?id=1'>
<input type='input' name='element' size='20' /><br/>
<input type='submit' value='submit...'/>
</form>
</fieldset>
</body>
</html>
";
} }
?>
I’m not sure what real problem is here, but you should check file pointer is good before using it in fread() and fclose() calls
If there is a chance the file does not exist, then check first and deal with it.