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>
4
  • 1
    Images are cached by browser. Add a random string as parameter like <img src="images/b9.jpeg?r=<?= rand() ?>"> Commented Mar 1, 2021 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 Commented Mar 1, 2021 at 11:44
  • Sounds like maybe you need a proper database instead of a simplistic text file storage. Commented Mar 1, 2021 at 11:45
  • Take a look at this question on how to prepend text to a file. Commented Mar 1, 2021 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.