Solved

php - check if file exists

Posted on 2013-05-30
11
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 110

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
 

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 110

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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

630 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