# \[PHP\] File Location Problem

**URL:** <https://forum.kirupa.com/t/php-file-location-problem/225485>\
**Category:** programming\
**Created:** [May 12, 2007, 2:12pm UTC](https://forum.kirupa.com/t/php-file-location-problem/225485 "2007-05-12T14:12:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seb\_Hughes](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Seb\_Hughes](https://forum.kirupa.com/u/Seb_Hughes)\
**Post date:** [May 12, 2007, 2:12pm UTC](https://forum.kirupa.com/t/php-file-location-problem/225485/1 "2007-05-12T14:12:01Z")

</div>

I’m currently writing a script, So i can upload a picture and it will add some info to a .htacces file and also it will create a .txt file. The thing is I’m runing the script from /digg/stats and the .htaccess is in /digg/ and so will the picture, but I can’t seem to get the .htaccess and picture to be written/put from digg/stats to just /digg. Below is my script:

```php
<?php
//Getting & Creating/ Text file
$file = preg_replace ('/[^a-zA-Z0-9\_]/', '', $_POST['textfile']); 
$file = str_replace(' ', '_', $file);
$image_name = $file;
$file = $file.".txt";

if (file_exists($file)) {
    die ("The Link/Image Name You Have Entered Already Exists");
}

if($_FILES['imagefile']['type'] == "image/jpeg") {
	$type = ".jpg";
} else if ($_FILES['imagefile']['type'] == "image/gif") {
	$type = ".gif";
} else if ($_FILES['imagefile']['type'] == "image/png") {
	$type = ".png";
} else {
	die("Invalid Image Type");
}

$f = fopen ($file, 'w'); 
fputs ($f, ""); 
fclose ($f); 

//Uploading File

$uploadpath = 'digg/';

$new_name = $uploadpath.$image_name.$type;
copy ($_FILES['imagefile']['tmp_name'], $new_name)  
    or die ("Could not copy");
	
//Write to .htaccess
$htaccess = '.htaccess';
$slash = "\\";
$content = "
RewriteRule ^$image_name$ image_tracker.php?image=$image_name$slash$type
RewriteRule ^$image_name/$ image_tracker.php?image=$image_name$type
RewriteRule ^$image_name$slash$type$ image_tracker.php?image=$image_name$slash$type";

	//Checks / creates file
   if (!$handle = fopen($htaccess, 'a+')) {
         echo "Cannot open file ($htaccess)";
         exit;
   }

	//Write To File
   if (fwrite($handle, $content) === FALSE) {
       echo "Cannot write to file ($htaccess)";
       exit;
   }
  
  fclose($handle);

if (!is_writable('.htaccess')) {
		die("The file $htaccess is not writable");
}

```
