How do code record information from multiple files?

Asked

Viewed 36 times

0

I have a watermark code on every image that was uploaded. At the end of this code, I would like every time a person uploads the image, to create a record in a notepad. I arrived at this code but I’m having a hard time getting it to keep track of multiple uploads.

$arquivo = "rastro.txt"; 
$data = date("d/m/Y H:i:s");   
$ip = $_SERVER['REMOTE_ADDR'];    
$browser = $_SERVER['HTTP_USER_AGENT']; 
$fp = fopen($arquivo, "w+");   
fwrite($fp,"Nome: $new_name | Data: $data | IP: $ip | Navegador: $browser");   
fclose($fp);

How do I generate a loop in code ?

  • The loop would be after opening the file fopen, the $new_name you pick from where?

  • From the code that creates the watermarks. It’s just this part where I was in doubt, but $new_name is working normal

  • Yes I have no doubt about it, but when you get the $new_name it is likely that you take from an array not?

  • In this particular case, no :)

  • Well, I believe that in order to help you I need more information, because your code is correct... more precise information of your scope, of the variables that you have there.

  • Well, I’m still learning and so don’t notice the kkk mangrove, but follow the whole code here: http://pastebin.com/5H6MTabs

  • I posted an answer, test it and see if it works.

Show 2 more comments

1 answer

1


The problem is how you open the file. With the w+ you open the file for read and write more puts the write pointer always at the beginning of the file. This causes you to always overwrite the file data, giving the impression that you only recorded once.

Consider using a or a+ because it opens the file and puts the recording pointer at the end of the file.

$arquivo = "rastro.txt"; 
$data = date("d/m/Y H:i:s");   
$ip = $_SERVER['REMOTE_ADDR'];    
$browser = $_SERVER['HTTP_USER_AGENT']; 
$fp = fopen($arquivo, "a+");   
fwrite($fp,"Nome: $new_name | Data: $data | IP: $ip | Navegador: $browser \n\r");   
fclose($fp);
  • It worked perfectly, thank you very much Jhonatan

  • I also added on fwrite \n\r that serves to go to the next line, otherwise would record everything together... kkkk

Browser other questions tagged

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