Solved

php - check if file exists

Posted on 2013-05-30
11
399 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

12 Experts available now in Live!

Get 1:1 Help Now