eelmitchell Posted April 6, 2009 Share Posted April 6, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152785-solved-cant-upload-or-create-a-file-in-the-root-directory-cinetpubwwwroot/ Share on other sites More sharing options...
eelmitchell Posted April 7, 2009 Author Share Posted April 7, 2009 I moved the files to my Unix web server and there was no problem with the upload. I could touch a file in the root directory. Seems to be something special about IIS on my Windows machine. Quote Link to comment https://forums.phpfreaks.com/topic/152785-solved-cant-upload-or-create-a-file-in-the-root-directory-cinetpubwwwroot/#findComment-803516 Share on other sites More sharing options...
mr_chuya Posted April 15, 2009 Share Posted April 15, 2009 You should change security option for upload folder (use windows explorer to change these) for user IUSR_xxxx. Make this user can write/modify for that folder. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/152785-solved-cant-upload-or-create-a-file-in-the-root-directory-cinetpubwwwroot/#findComment-810412 Share on other sites More sharing options...
eelmitchell Posted April 15, 2009 Author Share Posted April 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152785-solved-cant-upload-or-create-a-file-in-the-root-directory-cinetpubwwwroot/#findComment-810734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.