Code

needs a filelock

<html>
<head></head>
<body>
<?php
if (!isset($_POST['userfile'])) {
   print "<h3>Eastern Sun 3.00 R Skill and Stats Point Resetter</h3>\n";
   print "<p>Backup the character first!</p>\n";
   print "<form enctype=\"multipart/form-data\" ";
   print "action=\"d2chr_reset.php\" method=\"POST\">\n";
   print "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />\n";
   print "D2 Character File: <input name=\"userfile\" type=\"file\" /><br><br>\n";
   print "<input type=\"submit\" value=\"Send File\" />\n";
   print "</form>\n";
} else {
    $uploaddir = '/characters/';
    $filename = $uploaddir . basename($_FILES['userfile']['name']);
    if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) {
        print "File upload error!<br>\n";
    } else {
        $handle = fopen($filename, "r");
        $contents = fread($handle, filesize($filename));
        fclose($handle);

        // main();

        if (!$handle = fopen($filename, 'w')) {
            print "Cannot open file ($filename)<br>\n";
            exit;
        } 
        if (fwrite($handle, $contents) === FALSE) {
            print "Cannot write to file ($filename)<br>\n";
            exit;
        }
        fclose($handle);
        print "Your character's skill and stats soints are reset.<br>\n";  
        print "Download your character and copy it to \"es300r\\save\" folder.<br>\n";  
        print "<a href=\"http://miyoshino.la.coocan.jp/reset$filename\">";
        print "Download your character</a><br>\n";
    }
}
?>
</body>
</html>

PHP reference

File Upload

Example 38-1. File Upload Form

A file upload screen can be built by creating a special form which looks something like this:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form> 

The __URL__ in the above example should be replaced, and point to a PHP file.

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted. This is an advisory to the browser, PHP also checks it. Fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. The PHP settings for maximum-size, however, cannot be fooled. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too big and the transfer failed.

Copy The Temp File To A Work File

Example 38-2. Validating file uploads

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES. 

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}
print "</pre>";

?>  

fread

string fread ( resource handle, int length )

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when up to length bytes have been read, EOF (end of file) is reached, (for network streams) when a packet becomes available, or (after opening userspace stream) when 8192 bytes have been read whichever comes first.

Returns the read string or FALSE in case of error.

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

fwrite

int fwrite ( resource handle, string string [, int length] )

fwrite() writes the contents of string to the file stream pointed to by handle. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

fwrite() returns the number of bytes written, or FALSE on error.

Note that if the length argument is given, then the magic_quotes_runtime configuration option will be ignored and no slashes will be stripped from string.

Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

Example 1. A simple fwrite() example

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) { 

   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence 
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   } 

   // Write $somecontent to our opened file.
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   
   echo "Success, wrote ($somecontent) to file ($filename)";
   
   fclose($handle);

} else {
   echo "The file $filename is not writable";
}
?>  

ord

ord ($str)
returns the ascii code (0-255)


Front Page   Edit Freeze Diff Backup Attach Copy Rename Reload   New All Pages Search Recent Changes   Help   RSS of recent changes
Last-modified: 12-14-2006 (Thu) 10:31:14 (4541d)