檔案上傳操作

來源:互聯網
上載者:User
<?php$updir="./upload/";//上傳目錄$dirtype="2";//目錄儲存方式1:年/月/日;2:年/月;預設:年$renamed="1";//是否重新命名1表示重新命名0表示用原來的檔案名稱$overwrite="1";//是否覆蓋1表示覆蓋0表示不覆蓋if (isset(FILES["Filedata"]) && is_uploaded_file(FILES["Filedata"]["tmp_name"]) && FILES["Filedata"]["error"] == 0) {    //上傳檔案賦值給$upload_file    $upload_file=FILES["Filedata"];    //擷取檔案類型    $file_info=pathinfo($upload_file["name"]);    //擷取副檔名    $file_ext=$file_info["extension"];    //設定路徑方式    switch($dirtype){        case '1':            $m_dir=date(Y)."/".date(m)."/".date(d)."/";            break;        case '2':            $m_dir=date(Y)."/".date(m)."/";            break;        default:            $m_dir=date(Y)."/";            break;    }    //設定上傳的路徑    $upload_path=$updir.$m_dir;    //建立檔案夾    create($upload_path);    //需要重新命名的    if($renamed){        list($usec, $sec) = explode(" ",microtime());        $upload_file['name']=substr($usec,2).'.'.$file_ext;        unset($usec);        unset($sec);    }    //設定預設服務端檔案名稱    $upload_file['filename']=$upload_path.$upload_file['name'];    //檢查檔案是否存在    if(file_exists($upload_file['filename'])){        if($overwrite){            @unlink($upload_file['filename']);        }else{            $j=0;            do{                $j++;                $temp_file=str_replace('.'.$file_ext,'('.$j.').'.$file_ext,$upload_file['filename']);            }while (file_exists($temp_file));            $upload_file['filename']=$temp_file;            unset($temp_file);            unset($j);        }    }//按照效果來說程式在這就結束下,在下面輸出測試語句的時候什麼都不會輸出    if(@move_uploaded_file($upload_file["tmp_name"],$upload_file["filename"])){    }else{        echo '';    }} else {    echo ' '; // I have to return something or SWFUpload won't fire uploadSuccess}//建立檔案夾,裡面可以有多級目錄function create($dir){    if (!is_dir($dir))    {        $temp = explode('/',$dir);        $cur_dir = '';        for($i=0;$i<count($temp);$i++)        {            $cur_dir .= $temp[$i].'/';            if (!is_dir($cur_dir))            {                @mkdir($cur_dir,0777);            }        }    }}?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.