php上傳圖片到伺服器

來源:互聯網
上載者:User

標籤:圖片   upload   php   

php檔案上傳中會用到$_FILES系統函數

一、$_FILES系統函數

PHP程式設計語言中的常見的$_FILES系統函數用法有:
$_FILES[‘myFile‘][‘name‘] 顯示用戶端檔案的原名稱。
$_FILES[‘myFile‘][‘type‘] 檔案的 MIME 類型,例如"image/gif"。
$_FILES[‘myFile‘][‘size‘] 已上傳檔案的大小,單位為位元組。
$_FILES[‘myFile‘][‘tmp_name‘] 儲存的臨時檔案名稱,一般是系統預設。

二、常見函數

PHP程式設計語言中的常見的$_FILES系統函數用法有:$_FILES[‘myFile‘][‘name‘] 顯示用戶端檔案的原名稱。$_FILES[‘myFile‘][‘type‘] 檔案的 MIME 類型,例如"image/gif"。$_FILES[‘myFile‘][‘size‘] 已上傳檔案的大小,單位為位元組。$_FILES[‘myFile‘][‘tmp_name‘] 儲存的臨時檔案名稱,一般是系統預設。$_FILES[‘myFile‘][‘error‘] 該檔案上傳相關的錯誤碼。以下為不同代碼代表的意思:0; 檔案上傳成功。1; 超過了檔案大小php.ini中即系統設定的大小。2; 超過了檔案大小MAX_FILE_SIZE 選項指定的值。3; 檔案只有部分被上傳。4; 沒有檔案被上傳。5; 上傳檔案大小為0。三、php檔案上傳

通過form上傳檔案,首先需要標記<form action="setting.php" enctype="multipart/form-data" method="post">。下面的html代碼:

<form action="setting.php" enctype="multipart/form-data" method="post">      <div class="left">        商家店鋪名:<br/>        <input name="sjname" class="input1" type="text" ><br/>聯絡電話:<br/>        <input name="sjphone" class="input1" type="text" ><br/>QQ:<br/>        <input name="sjqq" class="input1" type="text" ><br/>        廣告語:<br/>        <input name="sjad" class="input1" type="text" placeholder="20字以內" ><br/>      </div>      <div class="right">        上傳封面圖:(建議320*120)<br/>        <input type="text" readonly="readonly" name="file_name" id="file_name" class="fileinput"/><input type="file" name="img_file" style="display:none" onchange="file_change(this.value)"/><input type="button" value="瀏 覽" onclick="img_file.click();" name="get_file" class="liulan"/><br/>商家簡介:<br/>        <textarea name="sjbrief" class="textarea" placeholder="100字以內" ></textarea>      </div>      <div class="clear"> </div>        <p>        <input class="buttonsave" type="submit" value="儲存設定" />|<a style='text-decoration:none;' href="binding.php?ac=del" target ="_top"><input class="buttonquit" type="button" value="解除AP綁定"/></a>        </p>      </form>

php代碼如下:

<?php/** * Created by PhpStorm. * User: funmi * Date: 14-9-10 * Time: 下午6:57 */include_once('./common.php');if($_SGLOBAL['login']==true){    $cover_pic = upload_store_pic();    $store_name = $_POST['sjname'];    $tel_number = $_POST['sjphone'];    $ad_words = $_POST['sjad'];    $store_introduce = $_POST['sjbrief'];    $qq_number = $_POST['sjqq'];    $setarr=array(        'cover_pic'=>$cover_pic,        'store_name'=>$store_name,        'tel_number'=>$tel_number,        'ad_words'=>$ad_words,        'store_introduce'=>$store_introduce,        'qq_number'=>$qq_number    );    updatetable(tname('open_member_weixin_ap'),$setarr,array('apid'=>$_COOKIE['apid'],'id'=>$_COOKIE['id']));    $smarty->assign('loginuser',$_COOKIE['loginuser']);    $smarty->display('setting.dwt');    exit();}gourl('binding.php');function upload_store_pic(){    //檔案儲存路徑    $save_path = './uploads/store_pic/';    if (!file_exists($save_path)) {        mkdir($save_path);    }    //定義允許上傳的副檔名    $ext_arr = array(        'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp')    );    //最大檔案大小    $max_size = 1000000;    //PHP上傳失敗    if (!empty($_FILES['img_file']['error'])) {        switch($_FILES['img_file']['error']){            case '1':                $error = '超過php.ini允許的大小。';                break;            case '2':                $error = '超過表單允許的大小。';                break;            case '3':                $error = '圖片只有部分被上傳。';                break;            case '4':                $error = '請選擇圖片。';                break;            case '6':                $error = '找不到臨時目錄。';                break;            case '7':                $error = '寫檔案到硬碟出錯。';                break;            default:                $error = '未知錯誤。';        }        showmessage($error);        return '';    }    //有上傳檔案時    if (empty($_FILES) === false) {        //原檔案名稱        $file_name = $_FILES['img_file']['name'];        //伺服器上臨時檔案名稱        $tmp_name = $_FILES['img_file']['tmp_name'];        //檔案大小        $file_size = $_FILES['img_file']['size'];        //檢查檔案名稱        if (!$file_name) {            showmessage("請選擇檔案。");            return '';        }        //檢查目錄        if (@is_dir($save_path) === false) {            showmessage("上傳目錄不存在。");            return '';        }        //檢查目錄寫入權限        if (@is_writable($save_path) === false) {            showmessage("上傳目錄沒有寫入權限。");            return '';        }        //檢查是否已上傳        if (@is_uploaded_file($tmp_name) === false) {            showmessage("上傳失敗。");            return '';        }        //檢查檔案大小        if ($file_size > $max_size) {            showmessage("上傳檔案大小超過限制。");            return '';        }        //檢查目錄名        $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);        if (empty($ext_arr[$dir_name])) {            showmessage("目錄名不正確。");            return '';        }        //獲得副檔名        $temp_arr = explode(".", $file_name);        $file_ext = array_pop($temp_arr);        $file_ext = trim($file_ext);        $file_ext = strtolower($file_ext);        //檢查副檔名        if (in_array($file_ext, $ext_arr[$dir_name]) === false) {            showmessage("上傳副檔名是不允許的副檔名。");            return '';        }        //新檔案名稱        $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '_apid_' . $_COOKIE['apid'] . '.' . $file_ext;        //移動檔案        $file_path = $save_path . $new_file_name;        if (move_uploaded_file($tmp_name, $file_path) === false) {            showmessage("上傳檔案失敗。");            return '';        }        @chmod($file_path, 0777);        return $file_path;    }else{        return '';    }}


php上傳圖片到伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.