0

I write my website but i have a problem with automatization uploads and auto add in site. I use a php upload images and add html code in txt file and add file in site.

My php code:

for($i=0;$i<$countfiles;$i++){
$filename = $_FILES['file']['name'][$i];
 
// Upload file
if ($filename == ''){
    echo 'No file selected.';
}else{
    if (file_exists('images/'.$filename)) {
        echo "$filename is exist.". "<br>";
    } else 
    {
        move_uploaded_file($_FILES['file']['tmp_name'][$i],'images/'.$filename);
        $myfile = fopen("51.txt", "r+") or die("Unable to open file!");
        $txt = "\n".'<div class="b"> <img src="images/' . $filename . '" width="620"> <div class="b">' . "\n";
        fwrite($myfile, $txt);
        fclose($myfile);
        echo $filename .' Uploaded' . '<br>';
    }
}

I want to insert it at the beginning of the file but always rewrite the previously inserted line. and my txt file:

<div class="b"> <img src="images/1554df478sdf2sdf8.jpeg"> </div>
</div>
<div class="b"> <img src="images/b9.jpeg"> </div>
<div class="b"> <img src="images/b8.jpeg"> </div>
<div class="b"> <img src="images/b7.jpeg"> </div>
  • 1
    Images are cached by browser. Add a random string as parameter like `` – Markus Zeller Mar 01 '21 at 11:23
  • I want the code of the uploaded image in the first line in txt after uploading. and so would be the order – Bence Hencs Mar 01 '21 at 11:44
  • Sounds like maybe you need a proper database instead of a simplistic text file storage. – ADyson Mar 01 '21 at 11:45
  • Take a look at [this question](https://stackoverflow.com/questions/1760525/need-to-write-at-beginning-of-file-with-php) on how to prepend text to a file. – Michel Mar 01 '21 at 11:58

0 Answers0