[moka同學代碼]PHP初級知識:上傳檔案源碼

來源:互聯網
上載者:User

標籤:

1.目錄結構

     

2.index.php

<html><head>    <meta charset="utf-8">    <title>上傳檔案</title></head><body><form action="upload.php" method="post" enctype="multipart/form-data">    <label for="file">檔案名稱</label>    <input type="file" name="file" id="file"><br>    <input type="submit" value="提交"></form></body></html>

3.upload.php

<?php//上傳檔案代碼//核心代碼:/*if($_FILES[‘file‘][‘error‘]>0){    echo   ‘錯誤:‘.$_FILES[‘file‘][‘error‘].‘<br>‘;}else{    echo ‘上傳檔案名稱:‘.$_FILES[‘file‘][‘name‘].‘<br>‘;    echo ‘檔案名稱類型:‘.$_FILES[‘file‘][‘type‘].‘<br>‘;    echo ‘檔案大小:‘.($_FILES[‘file‘][‘size‘]/1024).‘kb<br>‘;    echo ‘檔案臨時存放地址:‘.$_FILES[‘file‘][‘tem_name‘];}*///上傳檔案所需要的代碼,但是在上傳之前需要對檔案進行判斷限制,大小,類型等//允許上海竄的圖片尾碼$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]); //截取檔案尾碼$extension = end($temp); //檔案尾碼$type = $_FILES["file"]["type"];$size = $_FILES["file"]["size"];$error = $_FILES["file"]["error"];$name = $_FILES["file"]["name"];$tmp_name = $_FILES["file"]["tmp_name"];if (($type == "image/gif") || ($type == "image/jpeg") || ($type == "image/jpg") || ($type == "image/pjpeg") ||    ($type == "image/x-png") || ($type == "image/png") && $size < 2048 && in_array($extension, $allowedExts)) {    if ($error > 0) {        echo "錯誤:" . $error . ‘<br>‘;    } else {        echo "檔案名稱:" . $name . "<br>";        echo "檔案類型:" . $type . "<br>";        echo "檔案大小:" . ($size / 1024) . "kb<br>";        echo "臨時存放位置:" . $tmp_name;        //判斷存放檔案的目錄中是否存在該檔案,如果沒有,需要建立它,file的目錄許可權為777        if (file_exists("file/" . $name)) {            echo $name . "檔案已經存在。";        } else {            move_uploaded_file($tmp_name, ‘file/‘. $name);            echo "檔案儲存體在:" . "file/". $name;        }    }} else {    echo "非法檔案";}

 

[moka同學代碼]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.