Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

File Upload

file upload php file upload max_file_size

  • Please log in to reply
1 reply to this topic

#1 Irbis

Irbis

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Programming Language:PHP, JavaScript, Transact-SQL

Posted 27 July 2012 - 08:51 PM


?php



/**  

 * @copyright © 2012 www.prifantom.com

 */  

    function uploadHandle($max_file_size = 100, $valid_extensions = array(), $upload_dir = '.')  

    {  

      

        $error = null;  

        $info  = null;  

        $max_file_size *= 1024;  



        if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK)  

        {  

            // checking the file extension

            $file_extension = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);  

            if (in_array($file_extension, $valid_extensions))  

            {  

                // checking the file_size

                if ($_FILES['userfile']['size'] < $max_file_size)  

                {  

                    $destination = $upload_dir .'/' . $_FILES['userfile']['name'];  

      

                    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $destination))  

                        $info = 'File successful uploaded!';  

                    else  

                        $error = 'Cannot upload file at this time. Please try again later.';  

                }   

                else  

                    $error = 'File size more then it need 100mb';  

            }   

            else  

                $error = 'Invalid file extension!';  

        }   

        else  

        {  

            // array of errors

            $error_values = array( 

                UPLOAD_ERR_OK        => "No errors.", 

   UPLOAD_ERR_INI_SIZE    => "Larger than upload_max_filesize.", 

   UPLOAD_ERR_FORM_SIZE    => "Larger than form MAX_FILE_SIZE.", 

   UPLOAD_ERR_PARTIAL    => "Partial upload.", 

   UPLOAD_ERR_NO_FILE        => "No file.", 

   UPLOAD_ERR_NO_TMP_DIR    => "No temporary directory.", 

   UPLOAD_ERR_CANT_WRITE    => "Can't write to disk.", 

   UPLOAD_ERR_EXTENSION     => "File upload stopped by extension.", 

   UPLOAD_ERR_EMPTY        => "File is empty." // add this to avoid an offset



                                  );  

      

            $error_code = $_FILES['userfile']['error'];  

      

            if (!empty($error_values[$error_code]))  

                $error = $error_values[$error_code];  

            else  

                $error = 'Something strange happened';  

        }  

      

        return array('info' => $info, 'error' => $error);  

    }  

      

/////////////////////////////////////////////////////////////////////      

    $extensions = array('jpg', 'jpeg', 'png', 'gif');  

    $upload_dir = 'images';  

      

    // run the function

    if(!empty($_POST['upload_submit'])) 

    {      

        $message = uploadHandle(200, $extensions, $upload_dir);  

    

        // Выводим сообщение  

        echo $message['error'] ? $message['error'] : $message['info'];  

    } 

?>  



<form action="file_upload.php" method="post" enctype="multipart/form-data"> 

<input type="file" name="userfile" /> 

<input type="submit" value="Upload" name="upload_submit" /> 

</form>


  • 0

#2 surajkumardotin

surajkumardotin

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Ludhiana
  • Programming Language:PHP, JavaScript
  • Learning:C, C++, PHP, JavaScript

Posted 19 August 2015 - 10:49 PM

What is this? please write that what do you want?


  • 0





Also tagged with one or more of these keywords: file upload, php file upload, max_file_size

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download