Adding an extention .txt to a saved file from checkbox selections


  • In menu we have a menu.txt file with times to select.

    <?php
    	echo '<form method="POST" action="process.php">';
    		$file = fopen("menu.txt","r");
    	echo '<table border="1">';
    		while(! feof($file))
    	{
    		$data= fgets($file);
    		if(trim($data) != '') {
    	echo '<tr>
    		<td><input type="checkbox" name="check[]" value="'.$data.'"></td>
    		<td>'.$data.'</td></tr>';
    	}
    		}
    		fclose($file);
    	echo '<input type="submit" value="send">';
    	echo '</form>';
    ?>
    

    In the process below we have a way to save those selections $myFile=fopen($name,"a"); but when including .txt in save $myfile=fopen($name . ".txt","a"); and saving as txt file the data does not save in the text file. The selections only save when we try not to add an extension in the code.

    <?php
    if(isset($_POST['check']) && ! empty($_POST['check']))  
    {
        $check=$_POST['check'];
        $numberOfCheck=count($check);
    		echo ("You have checked $numberOfCheck checkboxes : ");
    		//echo ("You have ordered $numberOfCheck itmes on the breakfast menu : ");
    			for ($i=0; $i<$numberOfCheck; $i++)  
    		{
    			echo '<br>';
    			echo ($check[$i]. " ");
    		$VarCheck=$check[$i];
            $name=time();
            **$myFile=fopen($name,"a");**
    		//$myfile=fopen($name.'.txt','a'); 		
    		//$myfile=fopen($name . ".txt","a");
    		$txt=$VarCheck;
    			fwrite($myFile,$txt);
    			fclose($myFile);    
    		}
    	}
    	else
    	{
    		echo "Zero check selected";
    	}
    ?>
    


  • Strange. It worked for me.

    $name = time();
    $txtName = $name . ".txt";
    $myFile=fopen($txtName,"a");
    $txt = "Text added";
    fwrite($myFile,$txt);
    fclose($myFile);
    

    Make sure you have the right permissions set on the directory. You should have write permissions enabled. I have drwxr-xr-x and it worked for me.

    That is the only thing I can think of. I know you said it creates it without the extension so I am not 100% sure.

    So it doesn't create any file as soon as you add the extension part?



  • That's so weird it works with this change..

    $name = time();
    $txtName = $name . ".txt";
    $myFile=fopen($txtName,"a");
    $txt = $VarCheck;
    fwrite($myFile,$txt);
    fclose($myFile);