first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

PHP Code: File Upload with a Single PHP Script File

<?php /* This code is loosely based on john@php-dude.com, the post 27-Jul-2001 12:00 at http://www.php.net/manual/en/features.file-upload.php. */

$HTML = NULL;

if(count($HTTP_POST_FILES) &gt; 0){
    $allowed_types = array(&quot;application/pdf&quot;,&quot;text/plain&quot;,&quot;text/html&quot;); 
    $size_limit = &quot;100000&quot;; //bytes

    $file = $HTTP_POST_FILES[&quot;file&quot;][&quot;name&quot;]; 
    $type = $HTTP_POST_FILES[&quot;file&quot;][&quot;type&quot;]; 
    $size = $HTTP_POST_FILES[&quot;file&quot;][&quot;size&quot;]; 
    $temp = $HTTP_POST_FILES[&quot;file&quot;][&quot;tmp_name&quot;]; 

    $path_info = pathinfo($PATH_TRANSLATED);

    //Web server anonymous user must have
    //write permissions to this path:
    $write_path = $path_info[&quot;dirname&quot;] . &quot;/&quot; . $file;

    if ($file){ 
        if ($size &lt; $size_limit){ 
            if (in_array($type,$allowed_types)){ 
                if(move_uploaded_file($temp,$write_path)){
                    $HTML = &quot;&lt;div&gt;The file &lt;tt&gt;$file&lt;/tt&gt; was sucessfully uploaded.&lt;/div&gt;&quot;;
                }
                else{
                    $HTML = &quot;&lt;div&gt;The file &lt;tt&gt;$file&lt;/tt&gt; upload failed.&lt;/div&gt;&quot;;
                }
            }
            else { 
                $HTML = &quot;&lt;div&gt;Files of type &lt;tt&gt;$type&lt;/tt&gt; are not permitted.&lt;/div&gt;&quot;; 
            }
        }
        else { 
            $HTML = &quot;&lt;div&gt;File exceeds the size limit of $size_limit bytes.&lt;/div&gt;&quot;;
        }
    } 

    $HTML .= &quot;&bsol;&bsol;n&quot;;
}

?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <meta name=VS_TARGETSCHEMA content="HTML 4.0"> <meta name="editor" content="Bryan Wilhite"> <meta name="reply-to" content="bwilhite@ucla.mednet.edu"> <meta name="keywords" content=""> <meta name="description" content=""> <title>PHP File Upload Test</title> </head> <body> <?php echo $HTML ?> <form enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>" method="POST"> Upload a File: <input name="file" type="file"> <input type="submit" value="Upload"> </form> </body> </html>

mod date: 2002-01-16T07:17:52.000Z