Saving file into new directory using fwrite
<p>I have a simple PHP script that writes a file into directory where located, but need to have it written into a directory called "temp".</p>

<p>There are many answers here on the subject, but can't seem to find what I need. Have reviewedhttp://us2.php.net/manual/en/function.fwrite.php with no luck.</p>

<p>Here is the basic PHP without the form part:</p>

<pre><code>&lt;?php

function saveFile($filename,$filecontent){
    if (strlen($filename)&gt;0){
        $file = @fopen($filename,"w");
        if ($file != false){
            fwrite($file,$filecontent);
            fclose($file);
            return 1;
        }
        return -2;
    }
    return -1;
}

?&gt;
</code></pre>

<p>This appears below the /form tag:</p>

<pre><code>&lt;?php    

    if (isset($_POST['submitBtn'])){
        $filename    = (isset($_POST['filename']))    ? $_POST['filename']    : '' ;
        $filecontent = (isset($_POST['filecontent'])) ? $_POST['filecontent'] : '' ;

?&gt;
</code></pre>

<p>and then:</p>

<pre><code>&lt;?php

        if (saveFile($filename,$filecontent) == 1){
            echo "&lt;tr&gt;&lt;td&gt;&lt;br/&gt;File was saved!&lt;br/&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;";
        } else if (saveFile($filename,$filecontent) == -2){
            echo "&lt;tr&gt;&lt;td&gt;&lt;br/&gt;An error occured during saving file!&lt;br/&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;";
        } else if (saveFile($filename,$filecontent) == -1){
            echo "&lt;tr&gt;&lt;td&gt;&lt;br/&gt;Wrong file name!&lt;br/&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;";
        }

?&gt;
</code></pre>

<p>Thanks for input.</p>