Solved

php - check if file exists

Posted on 2013-05-30
11
393 Views
Last Modified: 2013-05-30
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
Comment
Question by:XK8ER
  • 5
  • 4
  • 2
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39209377
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
 

Accepted Solution

by:
zailleh earned 334 total points
ID: 39209394
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39209404
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39209412
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39209419
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:zailleh
ID: 39209434
@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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39209438
Here's what I am getting at.  Consider this output:

111110110111

Now tell me the files that are missing ;-)
0
 

Assisted Solution

by:zailleh
zailleh earned 334 total points
ID: 39209451
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39209628
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39209752
im getting this error.. what can I do?

Fatal error: Allowed memory size of 136314880 bytes exhausted (tried to allocate 32 bytes)
0
 

Expert Comment

by:zailleh
ID: 39209769
Can you post the code and sample data you're using?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now