# PHP: Creating file in different directory

**URL:** <https://forum.kirupa.com/t/php-creating-file-in-different-directory/251972>\
**Category:** programming\
**Created:** [February 14, 2008, 2:56pm UTC](https://forum.kirupa.com/t/php-creating-file-in-different-directory/251972 "2008-02-14T14:56:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tampa1adam](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@Tampa1adam](https://forum.kirupa.com/u/Tampa1adam)\
**Post date:** [February 14, 2008, 2:56pm UTC](https://forum.kirupa.com/t/php-creating-file-in-different-directory/251972/1 "2008-02-14T14:56:01Z")

</div>

Hello, I’m putting together an XML file using the following code, and it works 100%, but creates the file in the dir the script ran in.

```php

$myFile = urlencode(strtolower($location) . ".xml");
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "<states>
");
fwrite($fh, "<state name=\"$location\" colour=\"#8800ff\">
");
foreach ($repDelID as $val) {
$y += 1;
$strpos = strpos($repDelID[$y-1], ',');
$val = "<point lat='" . substr($repDelID[$y-1], 0, $strpos) . "' lng='" . trim(substr($repDelID[$y-1], $strpos+1)) . "' />";
fwrite($fh, " " . $val . "
");
}
fwrite($fh, "</state>
");
fwrite($fh, "</states>
");
fclose($fh);

```

I want it to create the file in another dir called ‘ **xmlfiles** ’. It’s in the same dir as the script, and is writeable. The following code does not work:

```php

$myFile = "\\xmlfiles\\" . urlencode(strtolower($location) . ".xml");
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "<states>
");
fwrite($fh, "<state name=\"$location\" colour=\"#8800ff\">
");
foreach ($repDelID as $val) {
$y += 1;
$strpos = strpos($repDelID[$y-1], ',');
$val = "<point lat='" . substr($repDelID[$y-1], 0, $strpos) . "' lng='" . trim(substr($repDelID[$y-1], $strpos+1)) . "' />";
fwrite($fh, " " . $val . "
");
}
fwrite($fh, "</state>
");
fwrite($fh, "</states>
");
fclose($fh);

```

Please help!
