Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

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

    PHP
    2
    3
    36
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • MPG Radio
      MPG Radio last edited by

      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";
      	}
      ?>
      
      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by

        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?

        Reply Quote 0
          1 Reply Last reply

        • MPG Radio
          MPG Radio last edited by MPG Radio

          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);
          
          Reply Quote 1
            1 Reply Last reply

          • First post
            Last post