Search code examples
phpfopenfwrite

PHP fopen("filename", w) creates files with ~1


I am trying to write a file in PHP. So far it works "kind of".

I have an array of names in the format {Rob, Kevin, Michael}. I use the line of code

foreach($Team as $user)
 {
print_r($user);
    //create a file for each user
    $file = fopen("./employee_lists/".$user, 'w');
    //I have also tried: fopen("employee_lists/$user", 'w');
    // ... ... ...
    //write some data to each file.
 }

This works as expected: The print_r shows "Rob Kevin Michael", however, the filenames are saved are as follows: ROB~1, KEVIN~1, MICHAE~1

When I'm going on to use these files later in my code, and I want to relate the usernames of "Rob" to ROB~1, I'll have to take some extra step to do this. I feel like I'm using fopen incorrectly, but it does exactly what I want minus this little naming scheme issue.


Solution

  • It seems like your $user variable contains an invalid character for file system paths (my best guess would be a new line).

    Try:

    $file = fopen("./employee_lists/".trim($user), 'w');