Jump to content

[SOLVED] Can't upload or create a file in the root directory (C:\Inetpub\wwwroot)


eelmitchell

Recommended Posts

I am trying to upload a file and move_uploaded_file() was failing.  It didn't seem to be able to write the file into the web root or a subfolder.  My .html form to browse for the file is as follows:

 

<html> <body>
<form action="testupload.php" method="post" enctype="multipart/form-data" >
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <label for="userfile">Upload File:</label>
    <input type="file" name="userfile" id="userfile" />
    <input type="submit" value="Send File" />
</form>
</body> </html>

 

In my receiving file (testupload.php) I tried various combinations of forward slashes, back slashes and even $_SERVER['DOCUMENT_ROOT'].  My test file to handle the file move from the temp file is as follows (the $newFile override was just to see if I could write anywhere):

 

<html> <body>
    <h1>Uploading file...</h1> <?php
    if($_FILES['userfile']['type'] != 'text/plain') {
        echo("Problem: File is not plain text");
        exit;
    }
    if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
        // move the file to a subdirectory of this script
        $newFile = $_SERVER['DOCUMENT_ROOT']."/uploads/foo.txt";
        $newFile = 'foo.txt';
        echo("newFile: $newFile<br />");
        echo("Type: ".$_FILES['userfile']['type']."<br />");
        echo("File: ".$_FILES['userfile']['tmp_name']."<br />");
        echo("Error: ".$_FILES['userfile']['error']."<br />");
        echo("DocRoot: ".$_SERVER['DOCUMENT_ROOT']."<br />");
        $tmp_name = $_FILES['userfile']['tmp_name'];
        echo("tmp_name: ".$_FILES['userfile']['tmp_name']."<br />");
        if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $newFile)) {
            echo("Problem: Could not move file to $newFile<br />");
            echo("Error Code: ".$_FILES['userfile']['error']."<br />");
            exit;
        }
    }
?>
</body> </html>

 

The output when I ran this was the following:

 

newFile: foo.txt

Type: text/plain

File: C:\WINDOWS\Temp\phpC96.tmp

Error: 0

DocRoot: c:\inetpub\wwwroot

tmp_name: C:\WINDOWS\Temp\phpC96.tmp

 

Warning: move_uploaded_file(foo.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\testupload.php on line 27

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpC96.tmp' to 'foo.txt' in C:\Inetpub\wwwroot\testupload.php on line 27

Problem: Could not move file to foo.txt

Error Code: 0

 

Since it seemed that the problem could be a permission problem, I made sure that I can write on the C:\Inetpub\wwwroot\ by changing the permission for my account to "all" and testing this by doing "touch" from a DOS window.

 

Then I added the "touch" to the .php code and found from the .php program I can't create the same file.  I added the following:

 

        // try and create a file in the root directory
        if(!touch($newFile)) {
            echo("Can't touch $newFile file");
            exit;
        }
        if(!unlink($newFile)) {
            echo("Can't unlink $newFile file");
            exit;
        }

 

This produced the following:

 

newFile: foo.txt

 

Warning: touch() [function.touch]: Unable to create file foo.txt because Permission denied in C:\Inetpub\wwwroot\testupload.php on line 13

Can't touch foo.txt file

 

So my question is, what user permissions am I running under when the testupload.php program runs?  What should I do in order to create the new file from the upload to the website?

Link to comment
Share on other sites

I use Windows Explorer to view C:\Inetpub\wwwroot.  Under the Security tab, I see a list of user names like Administrators, CREATOR OWNER, DELL340 Admins, Authors, Browsers, Edward, Everyone, SYSTEM, Users, VS Developers, but no IUSR_xxxx.  So I go to Computer Management and select Users.  Under this I find IUSR_DEL340 for Internet Guest Account which seems hopeful.

 

Back to wwwroot Properties, I add IUSR_DELL340 and on "Check Names" this is changed into DELL340\IUSR_DELL340.  I allow Read&Execute, List, Read, Write permissions.

 

Back to my original problem, and it's gone away.

 

Thanks for the feedback.  Problem solved.  I really appreciate the advice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.