How do I stream a file from my server?

Asked

Viewed 72 times

0

People would like to know how I can make a file type shareable, for example: I have a file called bots.txt on my server, I wish my clients could edit it through the web getting saved on my server. Follow an example from Onedrive: Filing cabinet. Can be solved in any language.

My need is to do this without needing Onedrive where you can add preferred names without deleting existing ones (no problem if you can still delete).

  • Do you already have something ready? Some code?

2 answers

1

With php you can use fopen

$arquivo = "bots.txt";
fopen($arquivo, "r+"); // abre o arquivo para leitura e escrita
$linhas = "";
while(!feof($arquivo)){
    $linhas .= fgets($arquivo, 1024)."\n"; // adiciona linhas
}

With this you can implement an editor to delete or insert lines:

<textarea><?php echo $linhas; ?></textarea>

To write you use the fwrite

fopen($arquivo, "r+"); // abre o arquivo para leitura e escrita
fwrite($arquivo, $_POST['textarea']); // altera o arquivo com o valor

In String Mode you have the following options:

'r' Opens read-only; puts the file pointer at the beginning of the archive.

'r+' Opens for reading and writing; puts the file pointer on beginning of archive.

'w' Open only for writing; place the file pointer at the beginning of the file and reduces the file length to zero. If the file does not exist, tries to create it.

'w+' Opens for reading and writing; puts the file pointer on file start and reduce the file length to zero. If the file does not exist, tries to create it.

'to' Opens only for writing; puts the file pointer at the end file. If the file does not exist, try to create it.

'to+' Opens for reading and writing; puts the file pointer on end of file. If the file does not exist, try to create it.

'x' Create and open the file for writing only; put the pointer on file start. If the file already exists, the call to fopen() will fail, returning FALSE and generating an E_WARNING level error. If the file does not exist, tries to create it. This is equivalent to specify the flags O_EXCL|O_CREAT for the open system call(2).

'x+' Creates and opens the file for reading and writing; places the pointer at the beginning of the file. If the file already exists, the call to fopen() will fail, returning FALSE and generating an E_WARNING level error. If the file does not exist, tries to create it. This is equivalent to specify the flags O_EXCL|O_CREAT for the open system call(2).

1


Simple:

 /project
      txt_edit.php
      text.txt

Code-txt_edit.php:

if(isset($_POST['text'])){
   $re = fopen("text.txt" , 'w');
   $write = fwrite($re ,$_POST['text']);
   if($write){
      echo "<script> alert('Conteúdo alterado') </script>";
   }else if($write == false ){
      echo "<script> alert('O correu um erro')</script>";
  }
}
$fp = fopen("text.txt" , "r");
echo '<form action="#" method="post">';
echo '<textarea name="text" rows="20" cols="50">';
while(!feof($fp)){
    $buffer = fgets($fp , 4096);  
    echo $buffer;    
}
echo '</textarea><br>';
echo '<input type="submit" value="Salvar">';
echo '</form>';

To add only one line without seeing the content:

if(isset($_POST['text'])){
    $fp = fopen("text.txt" , "r");    
    $lines = '';
        while(!feof($fp)){
           $lines .= fgets($fp , 4096) ;               
        }       
    fclose($fp);
    $re = fopen("text.txt" , 'w');  
    $lines .= $_POST['text'];       
    $write = fwrite($re , $lines . PHP_EOL);  
    if($write){
        echo "<script> alert('Conteúdo alterado') </script>";
  }else if($write == false ){
      echo "<script> alert('O correu um erro')</script>";
  }
}

echo '<form action="#" method="post">';
echo '<input type="text" name="text" rows="20" cols="50">';
echo '<input type="submit" value="Salvar">';
echo '</form>';
  • And how do I add one name below the other? Without seeing what’s already saved in the file? Type can only add, cannot view or edit existing names?

  • 1

    I edited the answer check if it solves the problem ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.