如何转换成功能

时间:2016-07-25 10:35:41

标签: php arrays function phpmyadmin

我是php的新手,我需要一些帮助,我想将我的代码转换为函数,任何人都可以建议我如何将其转换为函数。

//ERRORS
$Errors = array(
        2=>'The uploaded file exceeds',
        3=>'The uploaded file was only partially uploaded.',
        4=>'No file selected',
        6=>'Missing a temporary folder.',
        7=>'Failed to write file to disk.'  
    );
//UPLOAD FILE
    if(isset($_POST['submit']) && isset($_FILES['file'])){
        $File = $_FILES['file'];        
        $OutFiles = array();
        foreach($File as $Index=>$Items){
            foreach($Items as $Key=>$Item){
                $OutFiles[$Key][$Index] = $Item;
            }
        }
        if($OutFiles[0]['error']){
            $error = $Errors[$OutFiles[0]['error']];
        }else{
            foreach($OutFiles as $Index=>$File){
                $UploadDir = $DocRoot.'/upload/';
                $name = $File['name'];
                //FILE EXTENTION GETTING
                $file_ext = explode('.',$name);
                $file_ext = $file_ext[count($file_ext)-1];
                //FILE NAME
                $filename = (rand()).'-'.(time()).'.'.$file_ext;
                //FILE EXTENTION ERROR
                if($file_ext != "jpg" && $file_ext != "png" && $file_ext != "jpeg" && $file_ext != "gif"){
                    $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                }elseif(move_uploaded_file($File['tmp_name'],$UploadDir.$filename)){
                    $OutFiles[$Index]['name'] = $filename;
                    $uploadok++;
                    $done = "uploaded";
                }elseif($uploadok == 0){
                    $error = "Sorry File is Not Upload";
                }else{
                    $uploadok--;
                    $error = "Sorry File is Not Upload";
                }
            }
        }
    }

请有人可以建议我如何转换为功能我对array之类的错误变量有点混淆,以及我如何能够从函数控制和回显我的html

HTML

<?php if(!$ok):?>
    <?php echo $error; ?>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="file[]" multiple/>
        <br />
        <input type="submit" value="Upload" name="submit"/>
    </form>
    <?php else: ?> 
    <div><h3>Done</h3></div>
        <?php
            foreach($filename as $file){
                echo "<div style='display:block; width:100px; height:100px; margin:0 10px; float:left; border:1px solid #eee;'><img style='width:auto; height:auto; max-width:100%; max-height:100%;' src='../upload/".$file['name']."' /></div>";
            }
        ?>
    <?php endif; ?>

0 个答案:

没有答案