1

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?

4 Answers 4

1

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. */
Sign up to request clarification or add additional context in comments.

1 Comment

thanks mate for the code and info on stackoverflow.com/questions/1566808/…
1
$files = array("header.php", "footer.php", "sidebar.php");
foreach($files as $required){
  if (!file_exists($required)) {
    die('Please upload files properly');
  }
}

Comments

1

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.

Comments

1

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.

9 Comments

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.)
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 is right. As initially written, the first iteration of the loop would've overwritten the array, breaking it. I fixed it.
@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; }.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.