How to stream a file from my server?

0

People would like to know how I can make a shareable file type, for example: I have a file called bots.txt on my server, I would like my clients to edit it via the web getting saved on my server. The following is an example of Onedrive: File . It can be solved in any language.

  

My need is to do this without needing the Onedrive in which you can add preference names without deleting existing ones (no problem if you can still delete them).

    
asked by anonymous 16.04.2018 / 20:05

2 answers

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>';
    
16.04.2018 / 21:18
1

With php you can use the 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>

For writing you use 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 only for reading; puts the file pointer at the beginning   of the file.

     

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

     

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

     

'w +' Opens for reading and writing; places the file pointer on the   beginning of the file and reduces the file length to zero. If the   file does not exist, try to create it.

     

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

     

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

     

'x' Creates and opens the file for writing only; puts the pointer on 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, try to create it. This is equivalent to specifying   the O_EXCL | O_CREAT flags for the open (2) system call.

     

'x +' Creates and opens the file for reading and writing; put 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, try to create it. This is equivalent to specifying   the O_EXCL | O_CREAT flags for the open (2) system call.

    
16.04.2018 / 21:14