Help with a Warning Error, please....

Discussion in 'PHP' started by chrisj, Oct 3, 2007.

  1. #1
    I believe this is a permission issue, regarding an upload php script on a windows server.

    "Warning: move_uploaded_file(C:/Documents and Settings/input/TEST.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\http\members_manager_script\upload1.php on line 109"

    I asked my host for help and they replied:
    "In order to set up your permissions correctly I need to know from you the following details:
    1. The EXACT files/folders you need permissions altered (if you're unable to provide this, please consult a web developer and ask him to take a look over your code)
    2. The file permissions you need (read/write, etc)
    3. The user (or users) you want to grant the files permissions to"

    So should I grant permission to a user? An administrator? How, on a windows server, do you grant permission to web visitors who will be uploading files?
    And is it read and write permission?

    Thank you. I greatly appreciate any help.

    
    <?
    	/*
    	Sile
    	Modified March 4, 2007
    	uploader.php copyright 2005-2007 "Hy"
    	*/
    
    	// Begin options
    
    	$allow_file_deletion = true; // To allow visitors to delete files, leave this at true; otherwise, change it to false
    
    	$file_extensions = array(".doc", ".rtf", ".htm", ".html", ".pdf", ".txt"); // Add or delete the file extensions you want to allow
    
    	$file_extensions_list = "doc,  rtf,  htm,  html,  pdf,  txt"; // Type the same as above, without the quotes separating them
    
    	$max_length = 50; // The maximum character length for a file name
    
    	$maximum_file_size = "2048000"; // In bytes
    
    	$upload_log_file = "upload_log.txt"; // Change this to the log file you want to use
    
    	// End options
    
    	$folder_directory = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
    	$message = "";
    	$set_chmod = 0;
    	$site_uri = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
    	$upload_directory = "C:/Documents and Settings/input/";
    	$upload_uri = $folder_directory."C://Documents and Settings//input//";
    
    	if($allow_file_deletion == true) $status = "enabled";
    	else $status = "disabled";
    
    	if($_REQUEST["delete"] && $allow_file_deletion) {
    	$resource = fopen($upload_log_file,"a");
    	fwrite($resource,date("F d, Y / h:i:sa")." - ".$_REQUEST["delete"]." deleted by ".$_SERVER["REMOTE_ADDR"]."\n");
    	fclose($resource);
    
    	if(strpos($_REQUEST["delete"],"/.") > 0);
    	elseif(strpos($_REQUEST["delete"],$upload_directory) === false);
    	elseif(substr($_REQUEST["delete"],0,6) == $upload_directory) {
    	unlink($_REQUEST["delete"]);
    	$message = "File has been deleted.";
    	header("Location: $site_uri?message=$message");
    	}
    	}
    
    	elseif($_FILES["userfile"]) {
    	$resource = fopen($upload_log_file,"a");
    
    fwrite($resource,date("F d, Y / h:i:sa")." - ".$_FILES["userfile"] ["name"]." ".$_FILES["userfile"]["type"]." (".$_FILES["userfile"]["size"]." bytes) uploaded by ".$_SERVER["REMOTE_ADDR"]."\n");
    	fclose($resource);
    
    
    	$file_type = $_FILES["userfile"]["type"];
    	$file_name = $_FILES["userfile"]["name"];
    	$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
    	@chmod($upload_uri."".$file_name, 0755);
    	if($_FILES["userfile"]["size"] > $maximum_file_size) {
    	$message = "ERROR: File size cannot be over ".$maximum_file_size." bytes.";
    	}
    
    	elseif($file_name == "") $message = "ERROR: Please select a file to upload.";
    	elseif(strlen($file_name > $max_length)) $message = "ERROR: The maximum length for a file name is ".$max_length." characters.";
    	elseif(!preg_match("/^[A-Z0-9_.\- ]+$/i",$file_name)) $message = "ERROR: Your file name contains invalid characters.";
    	elseif(!in_array($file_ext, $file_extensions))
    	$message = "ERROR: <ins>$file_ext</ins> is not an allowed file extension.";
    	else $message = upload_file($upload_directory, $upload_uri);
    	header("Location: $site_uri?message=$message");
    	}
    
    	elseif(!$_FILES["userfile"]);
    	else $message = "ERROR: Invalid file specified.";
    
    	$open = opendir($upload_directory);
    	$uploaded_files = "";
    	while($file = readdir($open)) {
    	if(!is_dir($file) && !is_link($file)) {
    	$uploaded_files .= "		<tr>
    				<td style=\" font-size:12px;color:#900;text-align: left; width: 70%\"> <a href=\"$upload_directory$file\" title=\"$file (".filesize($upload_directory."".$file)." bytes)\">".$file."</a> (".filesize($upload_directory."".$file)." bytes)</td>";
    	if($allow_file_deletion)
    	$uploaded_files .= "
    				<td style=\"background: #fff; font-size:12px;color:#6B6B6B; text-align: right; width: 30%\"><a href=\"?delete=$upload_directory".urlencode($file)."\" title=\"Delete File\">Delete File</a></td>";
    	else
    	$uploaded_files .= "
    				<td style=\"background: #fff; font-size:12px;color:#6B6B6B; text-align: right; width: 30%\"><del>Delete File</del></td>";
    	$uploaded_files .= "
    			</tr>
    			<tr>
    				<td colspan=\"2\" style=\"background: #eee; font-size:12px;color:#6B6B6B; text-align: left; text-indent: 20px\">        Uploaded--"     .date("F d, Y ", filemtime($upload_directory.$file))."</td>";
    	$uploaded_files .="
    			</tr>
    	";
    	}
    	}
            function upload_file($upload_directory, $upload_uri) {global $account;
            $file_name = $_FILES["userfile"]["name"];
    	$file_name = str_replace(" ","_",$file_name);
    	$ext=pathinfo($file_name,PATHINFO_EXTENSION);
            $file_name = basename($file_name, '.' . $ext);
            $file_path = $upload_directory. $file_name.'~~'. $account->get_user_name().'.'.$ext;
    
    	$temporary = $_FILES["userfile"]["tmp_name"];
    
    	$result = move_uploaded_file($temporary, $file_path);
        if(!chmod($file_path,0777))
    	$message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
    	else $message = ($result)?"File has been uploaded." : "An error has occurred.";
    
    
    	return $message;
    	}
    ?>
    Code (markup):

     
    chrisj, Oct 3, 2007 IP
  2. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i'm not familiar with windows server, but hopefully i can help

    i think it involved with your variables for directory:

    $upload_directory = "C:/Documents and Settings/input/";
    $upload_uri = $folder_directory."C://Documents and Settings//input//";

    looking at your error code, your directory is

    C:\Inetpub\wwwroot\http\members_manager_script\upload1.php

    now, you should find our first, at which directory you reside and have permission ( do the hosting company gave you permission on using C:/Documents and Settings/input/ )

    the easiest way i think is to change the upload_directory to one directory below where your scripts reside

    if you put upload.php ( let say this is your script name )
    in C:\inetpub\chrisj

    create a directory maybe c:\inetpub\chrisj\upload_folder

    and change all your $upload_folder accordingly

    hope it helps.
     
    kendo1979, Oct 3, 2007 IP
  3. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thanks for your help. I'd like to ask, is there an advantage to putting a file "one directory below where your scripts reside"?
     
    chrisj, Oct 3, 2007 IP
  4. ravish_83

    ravish_83 Peon

    Messages:
    221
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes that is always helpful as it keeps your directory structure neat. Specially when the uploaded files are very large in numbers
     
    ravish_83, Oct 4, 2007 IP