1

Ok, so I have a folder called pictures that has around 100 images of sunset. I use glob to create an array of all of those images. Then, I write those to a text file for later use, but my code isn't working and I don't know why. When I run this code on a webpage, nothing happens, even the "echo $image" doesn't work and my text file doesn't get updated either. Does anyone know what I'm doing wrong? Also, I started php last week so I'm very new to this.

<?php
  $directory = "C:\Users\Owner\Desktop\SunsetDirectory\pictures";
  $images = glob($directory . "/*.jpg");

  $myfile = fopen("C:\Users\Owner\Documents\SunsetWebsite\sunsettext.txt", "w") or die("Unable to open file!");

 foreach($images as $image)
 {
   echo $image;
   fwrite($myfile, $image);
 }
  fclose($myfile);


 ?>
Bobby.lock
  • 59
  • 1
  • 7
  • file_put_contents / file_get_contents. fread / fwrite. copy. – Honk der Hase Oct 19 '20 at 19:43
  • are you saying I should be using all of those functions? – Bobby.lock Oct 19 '20 at 19:48
  • No. You read about all of them in the documentation first (php.net) and then decide, which one suits you the best – Honk der Hase Oct 19 '20 at 19:52
  • Is the PHP code being executed at all? If you view the page source, do you see the code there? – Don't Panic Oct 19 '20 at 19:54
  • Yeah i do see it, when I write a echo

    test

    , it prints it out, I don't know why nothing is happening because i do believe my code should work unless I'm missing something.
    – Bobby.lock Oct 19 '20 at 19:55
  • Did you see this part in the fopen documentation? "On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes." Also, the http process may not have access to your user directory. – Don't Panic Oct 19 '20 at 19:59
  • You can `var_dump($myfile)` to see if the fopen worked. And be sure to enable error reporting on your dev server. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Don't Panic Oct 19 '20 at 20:02
  • Ok thank you guys for the help, I had a problem with windows but when i changed to linux, my code seemed to work with apache2, so I guess I'm going to stick with linux now haha! – Bobby.lock Oct 19 '20 at 20:28
  • I just have one question, it seems like each image file name get appended to one another, is there a way i can write each file name to its own separate line? @Don'tPanic – Bobby.lock Oct 19 '20 at 20:30
  • You can just append a newline when you write, like `$image.PHP_EOL` – Don't Panic Oct 19 '20 at 20:34

0 Answers0