Friday 16 May 2014

How to upload & Save Files with new name




<?php

 /* 
   //files array details
    $_FILES['easyfile']['name'] - the actual name of the uploaded file.

    $_FILES['easyfile']['size'] - the size in bytes of the uploaded file.

    $_FILES['easyfile']['tmp_name']- the uploaded file in the temporary directory on the web server.
   
    $_FILES['easyfile']['type'] - the MIME type of the uploaded file. file type like image,documnet,excel file

    $_FILES['easyfile']['error'] - the error code associated with this file upload.

*/
$msg="";
$upload_limit=1024000;
$valid_file=false;
echo $dirpath = dirname(__FILE__);
if (isset($_POST['submit']))
{

if($_FILES['easyfile']['name'])
{
    //if no errors...
    if(!$_FILES['easyfile']['error'])
    {
   
    //here is file destils
   
    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
   
    if($_FILES['easyfile']['size'] < $upload_limit ) //can't be larger than  upload limit
        {
            $valid_file = true;
           
        }else
        {

            $msg = 'Your file size is to big.';
        }
       
        //if the file has passed the test
        if($valid_file)
        {
            echo $file_new_name =  time()."_".strtolower($_FILES['easyfile']['name']); //rename your file

            //move/copy it to where we want it to be

            move_uploaded_file($_FILES['easyfile']['tmp_name'], $dirpath.'/uploads/'.$file_new_name);

            $msg = 'Congratulations!  Your file was upload successfully!.';
        }
   
   
    }
}

}// if submit button is hit

if ($msg!="")
echo $msg;
?>


<form action="" method="post" enctype="multipart/form-data">
    Your file: <input type="file" name="easyfile" size="40" />
    <input type="submit" name="submit" value="Submit" />
</form>

No comments :

Post a Comment

Freelance Jobs