tags:

views:

84

answers:

4

Hi

I want to make a validation to my files. Example my files are header.php, footer.php, sidebar.php, info.php etc. So if some of these files missed error will shown.

$required = 'header.php';
if (!file_exists($required)) {
  die('Please upload files properly');
}

How to validate the all of files in simple code?

+1  A: 
$files = array("header.php", "footer.php", "sidebar.php");
foreach($files as $required){
  if (!file_exists($required)) {
    die('Please upload files properly');
  }
}
Marius
+2  A: 

Using the code you already have, you could use PHP's (excellent) foreach statement:

$required = array("header.php", "footer.php", "sidebar.php", "info.php");
foreach($required as $req) {
    if(file_exists($req)) continue;
    die("Please upload files properly.");
}

That being said, this sounds like a case for the require and require_once keywords:

require "header.php";        /* Will puke if header.php is missing. */
Jed Smith
thanks mate for the code and info on http://stackoverflow.com/questions/1566808/php-how-to-make-an-array-to-this-code/1566839#1566839
bob
+1  A: 

Just use a foreach to iterate the array:

$required = array('header.php', 'footer.php', 'sidebar.php', 'info.php');
foreach ($required as $required) {
    if (!file_exists($required)) {
        die('Please upload files properly');
    }
}


Edit    It’s not a mistake that I used the same variable for the array items as for the array itself. foreach uses an internal copy of the array and not the array itself:

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.

Although the original array in $required overwritten with the first iteration, the internal one stays as initially passed to foreach. This might not be best practice since it’s a little confusing when not known. But it’s absolutely valid.

Gumbo
You want to use something other than $required as the destination variable; you'll nuke the array after the first run. (Won't you? God, I'd hope so.)
Jed Smith
@Jed Smith: Who cares? I don’t think he wants to reuse it after the loop.
Gumbo
I suspect that you will nuke it after the first iteration, and when it goes to get the next one it will either (a) bomb as what was an array is now a string, (b) dutifully expect that the next element is not there as a string is a "one-item array", or (c) proceed as if nothing had happened, sidestepping the source code by copying the first $required into some hidden variable that the foreach uses. I really, really hope for A or B, but you might be right that C happened.
Jed Smith
And, having just tested it, that indeed works. So it's making an iterator we can't see and binding all of the source array's elements. Cool. Good 'ole PHP.
Jed Smith
@Jed Smith is right. As initially written, the first iteration of the loop would've overwritten the array, breaking it. I fixed it.
ceejayoz
@Jed Smith: Answer D: `foreach` uses an internal copy of the array. Changing the original array doesn’t affect the internal one. You can even extend the array on each iteration like `foreach ($array as $item) { $array[] = $item; }`.
Gumbo
Yeah, I just tested it. Interesting sidestep. Learn something new every day.
Jed Smith
@ceejayoz: You don’t have to correct me. I intended to use the same variable for the array items as for the original array itself. That might be a little confusing. But it isn’t wrong.
Gumbo
If it's confusing and the confusion can be easily avoided, then it's wrong.
GZipp
+1  A: 

Does validation of your own application really makes sense? Anyway, if you really want to do this:

  1. Construct an array of all files.
  2. Use foreach to iterate over all the file in the array.
  3. Run the check you have for each of the files from the loop.
Lukáš Lalinský