note 30383 deleted from features.file-upload by philip
| From: | philip@php.net | Date: | Fri, 21 Mar 2003 16:33:44 +0000 |
| Subject: | note 30383 deleted from features.file-upload by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-45611@lists.php.net to get a copy of this message | ||
Here's a simple piece of code to allow for uploads on most unix servers, complete with error
checking. Note, the hidden MAX_FILE_SIZE is not included at all, because it's flakey at best.
The check for max file size is included in the PHP. Working example: http://www.fftrealm.com/upload.php
=== START COPING HERE ===
<HTML>
<TITLE>Upload</title>
<body>
<b>Upload!</b> (max size: 30kb)
<form action="<?PHP echo $PHP_SELF ?>" method="post"
enctype="multipart/form-data" name="form1" id="form1">
Send this file:
<input name="userfile" type="file">
<input type="submit" name="submit" value="Upload">
</form>
</body>
<?
$domain = GetHostByName($REMOTE_ADDR);
?>
<?PHP
/*
$userfile - The temporary filename in which the uploaded file was stored on the server machine.
$userfile_name - The original name or path of the file on the sender's system.
$userfile_size - The size of the uploaded file in bytes.
$userfile_type - The mime type of the file if the browser provided this information. An example
would be "image/gif".
*/
$dir="./upload/";
// upload is the directory to which all the files will be found.
$maxsize=30000;
// size is in bytes
$unique_id = time();
// As an alternative you can set the unique_id to the user's ip address ($domain) for easier
tracking of who is uploading what.
if (isset($submit)){
if ($userfile != "none") {
if ($userfile_size < $maxsize) {
copy($userfile,$dir.$unique_id."-".$userfile_name);
if (!is_uploaded_file ($userfile)){
echo "
<b>Error</b>: $userfile_name couldn't be uploaded. Either the file is too large
or null, the server is busy, or you are using an invalid extension. Please try
again.<br>";
} else {
echo "
http://www.your-sitename.com/upload/$unique_id-$userfile_name
uploaded succesfully.<br>";
}
} else {
echo "
<b>Error</b>: Sorry the file could not be uploaded. The max file size is $maxsize
bytes. Please try again. (your file: $userfile_size bytes)<br>";
}
} else {
echo "
<b>Error</b>: You must choose a file to upload. Please try again.<br>";
}
}
?>
</html>
=== STOP COPING HERE ===