php圖片上傳實現代碼

來源:互聯網
上載者:User

先瞭解files函數

$_files數組內容如下:

$_files['myfile']['name']   用戶端檔案的原名稱。
$_files['myfile']['type']   檔案的 mime 類型,需要瀏覽器提供該資訊的支援,例如"image/gif"。
$_files['myfile']['size']   已上傳檔案的大小,單位為位元組。
$_files['myfile']['tmp_name']   檔案被上傳後在服務端儲存的臨時檔案名稱,一般是系統預設。可以在php教程.ini的upload_tmp_dir 指定,但 用 putenv() 函數設定是不起作用的。
$_files['myfile']['error']   和該檔案上傳相關的錯誤碼。['error'] 是在 php 4.2.0 版本中增加的。下面是它的說明:(它們在php3.0以後成了常量)
  upload_err_ok
    值:0; 沒有錯誤發生,檔案上傳成功。
  upload_err_ini_size
    值:1; 上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值。
  upload_err_form_size
    值:2; 上傳檔案的大小超過了 html 表單中 max_file_size 選項指定的值。
  upload_err_partial
    值:3; 檔案只有部分被上傳。
  upload_err_no_file
    值:4; 沒有檔案被上傳。
    值:5; 上傳檔案大小為0.

 

php代碼

 

<?php
     $mkdir_file_dir    = mkdir('./img/'.$_post['title'],0777);           //上傳檔案的時候就開始建立一個圖片相關的目錄
     $tmp_file_name     = $_files['file']['tmp_name'];                        //上傳成功之後取的臨時檔案名稱
     $file_name             = $_files['file']['name'];                        //原始的檔案名稱
   
     $file_dir     = './img/'.$_post['title'].'/';                            //把建立的一個目錄賦值給你一個變數作為最終的儲存目錄
    
     if(is_dir($file_dir))
     {
         move_uploaded_file($tmp_file_name,$file_dir.$file_name);        //開始移動檔案
     }
 ?>

html代碼

<html>
    <head>
        <title>
            my is upfile app!!
        </title>
        <meta    http-equiv="content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <form enctype="multipart/form-data" method="post" action="upfile_add.php">
                標題:     <input type="text" name="title" />
                上傳檔案: <input type="file" name="file" />
                <input type="submit" vlaue="提交" />
        </form>
    </body>
</html>

非常重要的一點是:檔案通過post提交後是儲存在c:windowstemp臨時檔案夾中通過$_files["photo"]["tmp_name"],我們可以輕鬆獲得上傳的臨時檔案,將它儲存到我們指定的路徑中 下面是解決方案move_uploaded_file($_files["photo"]["tmp_name"],$path)

聯繫我們

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