-1

i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:

Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3

this is my code:

$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);

both the php file and input.txt are placed in www folder of wamp.

2
  • try to use full path of your file. if it still cannot be opened, it should be the permission problem. Commented May 6, 2016 at 6:38
  • Also, if the .txt file is in the same directory of your PHP file, remove the / or just do parent directories. I've had similar problems and it was cause it didn't know what the / was. Hope this helps. Commented May 6, 2016 at 6:40

9 Answers 9

1

Hope this will help you:

$File = "log_post.txt"; 
$fh = fopen ($File, 't') or die("can't open file");  
fclose($fh);
Sign up to request clarification or add additional context in comments.

Comments

0

Add full path to file. On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.

$location = fopen("C:\\folder\\input.txt", "r");

1 Comment

There is a troubleshooting checklist for this error : stackoverflow.com/questions/36577020/…
0
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);

Use this code and keep the input.txt file in the same directory where this code is written.

1 Comment

There is a troubleshooting checklist for this error : stackoverflow.com/questions/36577020/…
-1

First check if file exist or not?

 $filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    chmod($filename, 0777);
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

1 Comment

There is a troubleshooting checklist for this error : stackoverflow.com/questions/36577020/…
-1
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;

or

$location = file_get_contents('input.txt');
echo $location;

hope it will help

1 Comment

There is a troubleshooting checklist for this error : stackoverflow.com/questions/36577020/…
-1

Remove '/'(Slash)

$location = fopen("input.txt", "r") or die("Unable to open file!");

1 Comment

There is a troubleshooting checklist for this error : stackoverflow.com/questions/36577020/…
-1
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);

You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.

Comments

-1

first.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="prviProvera.php" method="POST">
        Satnica <input type="number" name="satnica" id="satnica" placeholder="Unesite satnicu" step="100"><br><br>
        Datum početka <input type="date" name="datumPocetka"><br><br>
        Datum kraja <input type="date" name="datumKraja"><br><br>
        <button type="submit" name="dugme">Izračunaj zaradu</button><br>
    </form>

    <?php
        if(isset($_POST["dugme"])){
            if(isset($_POST["satnica"]) && isset($_POST["datumPocetka"]) && isset($_POST["datumKraja"])){
                $satnica=$_POST["satnica"];
                $pocetak=$_POST["datumPocetka"];
                $kraj=$_POST["datumKraja"];
                if(date_create($pocetak)<=date_create($kraj)){
                    setcookie("satnica",$satnica,time()+60,"/");
                    setcookie("pocetak",$pocetak,time()+60,"/");
                    setcookie("kraj",$kraj,time()+60,"/");
                    echo "Cookies su postavljeni.";
                }
                else{
                    echo "Datum pocetka mora biti pre datuma kraja";
                }
                
                
            }
            else{
                echo "Niste popunili sva polja";
            }
        }
    ?>

</body>
</html>

firstcheck.php

   <?php
        if(isset($_COOKIE["satnica"]) && isset($_COOKIE["satnica"]) && isset($_COOKIE["kraj"])){
            $satnica=$_COOKIE["satnica"];
            $pocetak=$_COOKIE["pocetak"];
            $kraj=$_COOKIE["kraj"]; 
            $pocetakDatum=date_create($pocetak);
            $krajDatum=date_create($kraj);

            $brojRadnihDana=0;
            while($pocetakDatum<=$krajDatum){
                $danUNedelji=$pocetakDatum->format("N");
                if($danUNedelji<=5){
                    $brojRadnihDana++;
                }
                $pocetakDatum->modify("+1 day");
            }
            $zarada=$brojRadnihDana*$satnica*8;
            echo "Zarada za ".$brojRadnihDana." radnih dana je ".$zarada." dinara.";
        }
        else{
            echo "Kolacici ne postoje, idite na stranicu <a href='prvi.php'>Racunanje zarade</a>";
        }
        
        
    ?>

s2nd.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Prijava na konkurs za master studije</h1>
    <form action="drugi.php" method="POST">
        Ime: <input type="text" name="ime"><br><br>
        Prezime: <input type="text" name="prezime"><br><br>
        Smer: <select name="smer" id="">
            <option value="RT">RT</option>
            <option value="NRT">NRT</option>
            <option value="IS">IS</option>
            <option value="ELITE">ELITE</option>
        </select><br><br>
        Prosek: <input type="text" name="prosek"><br><br>
        <button type="submit" name="dugme">Pošalji</button><br>
    </form>

<?php
    if(isset($_POST["dugme"])){
        if(!empty($_POST["ime"]) && !empty($_POST["prezime"]) && isset($_POST["smer"]) && !empty($_POST["prosek"])){
            $ime=$_POST["ime"];
            $prezime=$_POST["prezime"];
            $smer=$_POST["smer"];
            $prosek=$_POST["prosek"];
            
            $putanja="datoteke/prijava_master.txt";
            $fajl=fopen($putanja,"a");
            $stringZaUpis=$ime."#".$prezime."#".$smer."#".$prosek."\n";
            echo $stringZaUpis;
            fwrite($fajl,$stringZaUpis);
            fclose($fajl);
            header("Location: drugi2.php");
            exit;
            
        }
        else{
            echo "Popunite sva polja!";
        }
    }
?>

</body>
</html>

2nispos.php

<body>
    <?php
        if(file_exists("datoteke/prijava_master.txt")){
            $fajl=fopen("datoteke/prijava_master.txt","r");
            
            while(($red=fgets($fajl))!=NULL){
                $tmpNiz=explode("#",$red);
                echo "<div style='background-color:red; margin:5px; padding:10px; font-size:1.4rem;'>Ime: ".$tmpNiz[0].", Prezime: ".$tmpNiz[1].", Smer: ".$tmpNiz[2].", Prosek: ".$tmpNiz[3]."</div>";
            }
            fclose($fajl);
            echo "Vrati se na stranicu za popunjavanje <a href='drugi.php'>forme</a>";
        }
        else{
            echo "fajl ne postoji, idite na stranicu <a href='drugi.php'>Prijava studenata</a>";
        }
       
    ?>
</body>

3rd.php

<body>
    <h1>Stranica za prijavu</h1>
    <form action="treci.php" method="POST">
        Ime: <input type="text" name="ime"><br><br>
        Prezime: <input type="text" name="prezime"><br><br>
        Lozinka: <input type="password" name="lozinka"><br><br>
        <button type="submit" name="dugme">Prijavi se</button>
    </form>

    <?php
        
        if(isset($_POST["dugme"])){
            if(!empty($_POST["ime"]) && !empty($_POST["prezime"]) && !empty($_POST["lozinka"])){
                session_start();
                $_SESSION["ime"]=$_POST["ime"];
                $_SESSION["prezime"]=$_POST["prezime"];
                $_SESSION["lozinka"]=$_POST["lozinka"];
                $_SESSION["brPoseta"]=0;
                header("Location: profil.php");
            }
            else{
                echo "Morate popuniti sva polja";
            }
        }
    ?>

</body>

functionfor3rd.php

<?php
function ispis(){
    $ime=$_SESSION["ime"];
    $_SESSION["brPoseta"]++;
    
    echo "Dobrodošao/la, ".$ime."<br><br>";
    echo "Broj poseta: ".$_SESSION["brPoseta"];
}
?>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

//make sure that your text in the folder is name input only, not input.txt

$location = fopen("input.txt", "r") or die("Unable to open file!"); echo $location; fclose($location)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.