Saturday, April 03, 2010

php upload script


<?php
if ($_SERVER['REQUEST_METHOD']=='POST')
{
    //echo ini_get('post_max_size');
    //echo ini_get('upload_max_filesize');
    //echo ini_get('max_input_time');
    //echo ini_get('max_execution_time');
    //echo ini_get('max_file_uploads');
    //echo ini_get('file_uploads');
    $errors = array(
      UPLOAD_ERR_OK =>'There is no error, the file uploaded with success.',
      UPLOAD_ERR_INI_SIZE =>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
      UPLOAD_ERR_FORM_SIZE =>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
      UPLOAD_ERR_PARTIAL =>'The uploaded file was only partially uploaded.',
      UPLOAD_ERR_NO_FILE =>'No file was uploaded.',
      UPLOAD_ERR_NO_TMP_DIR =>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.',
      UPLOAD_ERR_CANT_WRITE =>'Failed to write file to disk. Introduced in PHP 5.1.0.',
      UPLOAD_ERR_EXTENSION  =>'A PHP extension stopped the file upload',
    );
    if ($_FILES['userfile']['error']==UPLOAD_ERR_OK)
    { 
        $uploads_dir = '/srv/www/htdocs/files/';
        $tmp_name = $_FILES["userfile"]["tmp_name"];
        $name = $_FILES["userfile"]["name"];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
        die("complete");
    }
    else
    {
        die($errors[$_FILES['userfile']['error']]);
    }  
}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo 200*1024*1024;?>" />
<input name="userfile" type="file" />
<input type="submit" value="Upload" />
</form>

No comments: