Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:11:37+00:00 2026-06-05T09:11:37+00:00

Is there any way in PHP to open a file based on a variable

  • 0

Is there any way in PHP to open a file based on a variable for the filename? Basically what I want to is this:

$file = file('data.txt');
$needle = array("one", "two", "three");
$haystack = array("one", "three");

foreach($needle as $value){
  $pos = strpos($haystack, $value);

if($pos !== false){
  $filename = "$value.txt";
  file_put_contents($filename, $file);
}

The values of $needle are the names of .txt files. It works up until file_put_contents. $filename is not valid and I have searched all over for a solution to this and tried everything I can think of. I want to load the array value, say “one” with the .txt extension as a filename, depending on if the value was found in the haystack. Is there any way to do this without doing an if statement for each filename? I would rather handle it with a loop if possible.

Edited to swap the parameters.

Edit, new code:

$data = file_get_contents('data.txt');
$needle = array("one", "two", "three");
$haystack = array("one", "three");
$files = array_intersect($needle, $haystack);
foreach ($files as $value) {
  $newfilename = "$value.txt";
  var_dump($newfilename);
  file_put_contents($newfilename, $data);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:11:40+00:00Added an answer on June 5, 2026 at 9:11 am

    You mixed up the parameters of file_put_contents():

    int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

    So you need to swap them:

    file_put_contents($filename, $file);
    

    Second thing is, that you’re doing a strpos() on an array, but that function is (as its name says) for strings – what you want is in_array():

    foreach ($needle as $value) {
        if (in_array($value, $haystack) {
            $filename = "$value.txt";
            file_put_contents($filename, $file);
        }
    } 
    

    You can even enhance this a bit more by using array_intersect() – that gives you an array of all the value from $needle that are also in $haystack. I think that’s what you were asking for to avoid the if statement:

    $files = array_intersect($needle, $haystack);
    foreach ($files as $value) {
        $filename = "$value.txt";
        file_put_contents($filename, $file);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to include php file using require and calling a php
Is there any way to remove the contents of an file in php, do
Is there any way to call a php class (eg. $var = new className)
Is there any way to upload an entire folder using PHP?
Is there any way to differentiate IE7 versus IE6 using PHP's get_browser() function?
Is there any way that I could enjoy a decodeValue() function in PHP, too?
Is there any way to get a certain thumbnail generated by PHP from a
If yes is there any way to access a var defined in another PHP
Im new to netbeans, before this i use Nusphere Php Is there a way
Is there anyway for this PHP script to know what are the scripts or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.