• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

php - check if file exists

Hello,
I have a file called (images.txt) with about 10,000 line by line of path to an image.
I would like to make something in php so that It can go through each image and check if it exists.. how can I make this?

$file = '/path/to/images.txt'; 

if (file_exists($image)) {
    echo ".";
} else {
    echo "$image missing<br>";
}

Open in new window

0
XK8ER
Asked:
XK8ER
  • 5
  • 4
  • 2
3 Solutions
 
Ray PaseurCommented:
It looks like you're already on the right track.  You can read the list of file names with PHP file().  Please post a sample of the images.txt file here.  You can cut it down to a few lines and use the "attach file" feature.  I'll show you how it's done.
0
 
zaillehCommented:
The below URL should help.
http://php.net/manual/en/function.file.php

For example:

$file = '/path/to/images.txt';
$lines = file($file);

foreach($lines as $imagePath)
    echo file_exists($imagePath);

Open in new window

0
 
XK8ERAuthor Commented:
Okay, I came up with this and its working.. is that okay?

	echo 'Starting<br>';
	$line_of_text='';
	$file = fopen("images.txt", "r");
	$i = 0;
	while (!feof($file)) {
	    $line_of_text .= fgets($file);
	}
	$photo = explode("\n", $line_of_text);
	fclose($file);
		
	foreach ($photo as $eachPicture) {
		$i++;
		if (file_exists($eachPicture)) {
		    echo ".";
		} else {
		    echo "$image missing<br>";
		}		
	}
	echo '<br>Total: '.$i.'<br>all done!';

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ray PaseurCommented:
It looks more or less OK (I cannot test it because I don't have your data - that's why I asked for some test data), but why write all that code when PHP file() will give you the array of file names?  And PHP count() will tell you how many file names are in the array produced by file().  All with built-in functions.  Easy!
0
 
Ray PaseurCommented:
Sidebar note to @zailleh: PHP file_exists() returns a boolean value, so echo file_exists($imagePath) does not produce any meaningful output.  Not everyone who posts solutions at EE tests the solutions before posting, but everyone should!
0
 
zaillehCommented:
@Ray_Paseur: my understanding as that echo true; will output "1" and echo false; will output "0". For an example/test-case, this should be enough to determine that it's doing what you want. Obviously you would change the script to have different output depending on your needs usually by use of an if statement.
0
 
Ray PaseurCommented:
Here's what I am getting at.  Consider this output:

111110110111

Now tell me the files that are missing ;-)
0
 
zaillehCommented:
Granted, that's true Ray, but I was assuming that XK8ER would be able to use the main construct of the file() function and foreach loop to achieve his goals as this was what his question was, I wasn't trying to build an entire solution for him as that's not what he needed.

We have different perspectives on helping, I suppose.
0
 
Ray PaseurCommented:
Yeah, the experience level and the ability to apply generalized solutions is kind of hard to discern over a text-based forum.  I try to err on the side of tested examples.  But it is always amazing to me how many questions contain code that does not work (as if we needed that) and a "help me fix" question.  Computer programs only do one thing -- they transform data from one format to another.  

How in the world can anyone expect help when they don't show the before-and-after data?  It's easy to transform the data.  But it's nearly impossible to guess what the data should be or should have been!  I guess that's the difference between professional programmers and the rest of us who don't know how to exploit the principles of test-driven development.

In any case this question is fairly well answered, so I'll sign off now.  Best to all, ~Ray
0
 
XK8ERAuthor Commented:
im getting this error.. what can I do?

Fatal error: Allowed memory size of 136314880 bytes exhausted (tried to allocate 32 bytes)
0
 
zaillehCommented:
Can you post the code and sample data you're using?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now