PHP二次渲染

來源:互聯網
上載者:User

標籤:基本   sse   creat   前端   ima   使用   gravity   bst   瀏覽器   

首先是前端代碼,upload.html

 

[html] view plain copy 
  1. <html>  
  2.     <head>  
  3.     </head>  
  4.     <body>  
  5.         <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upload.php">  
  6.             <label>Choose an image to upload  
  7.                 <input type="file" name="uploaded" />  
  8.             </label>  
  9.             <p>  
  10.             <label>  
  11.                 <input type="submit" name="upload" value="upload"/>  
  12.             </label>  
  13.             </p>  
  14.         </form>  
  15.     </body>  
  16. </html>  


後端代碼,upload.php

 

 

[php] view plain copy 
  1. <?php  
  2. if (isset($_POST[‘upload‘]))  
  3. {  
  4.     // 獲得上傳檔案的基本資料,檔案名稱,類型,大小,臨時檔案路徑  
  5.     $filename = $_FILES[‘uploaded‘][‘name‘];  
  6.     $filetype = $_FILES[‘uploaded‘][‘type‘];  
  7.     $filesize = $_FILES[‘uploaded‘][‘size‘];  
  8.     print "<pre> File name : $filename </pre>";  
  9.     print "<pre> File type : $filetype </pre>";  
  10.     print "<pre> File size : $filesize </pre>";  
  11.     $tmpname = $_FILES[‘uploaded‘][‘tmp_name‘];  
  12.     print "<pre> Temp File path : $tmpname </pre>";  
  13.   
  14.     $uploaddir=‘c:/wamp/www/xpic/upload/‘;  
  15.   
  16.     $target_path=$uploaddir.basename($filename);  
  17.   
  18.     // 獲得上傳檔案的副檔名  
  19.     $fileext= substr(strrchr($filename,"."),1);  
  20.     print "<pre>File extension : $fileext </pre>";  
  21.   
  22.     $serverip = $_SERVER[‘SERVER_ADDR‘];  
  23.   
  24.     //判斷檔案尾碼與類型,合法才進行上傳操作  
  25.     if(($fileext == "jpg") && ($filetype=="image/jpeg"))  
  26.     {  
  27.   
  28.         if(move_uploaded_file($tmpname,$target_path))  
  29.         {  
  30.             //使用上傳的圖片產生新的圖片  
  31.             $im = imagecreatefromjpeg($target_path);  
  32.   
  33.             //給新圖片指定檔案名稱  
  34.             srand(time());  
  35.             $newfilename = strval(rand()).".jpg";  
  36.             print "<pre>new file name $newfilename </pre>";  
  37.             $newimagepath = $uploaddir.$newfilename;  
  38.             imagejpeg($im,$newimagepath);  
  39.             //顯示二次渲染後的圖片(使用使用者上傳圖片產生的新圖片)  
  40.             print ‘<b>New image</b><img src="http://‘.$_SERVER[‘SERVER_ADDR‘].‘/xpic/upload/‘.$newfilename.‘"/>‘;  
  41.             unlink($target_path);  
  42.         }  
  43.         else  
  44.         {  
  45.             print "<pre>Your image was not uploaded3. </pre><br/>";  
  46.         }  
  47.   
  48.     }  
  49.     else  
  50.     {  
  51.         print "<pre>Your image was not uploaded2.</pre> <br/>";  
  52.     }  
  53.   
  54. }  
  55.   
  56. else  
  57. {  
  58.     print "<pre>Your image was not uploaded1.</pre> <br/>";  
  59. }  
  60.   
  61. ?>  

 

 

實驗過程

首先選擇一張lena圖,在圖片末端寫入一句話後門代碼,最終儲存格式為jpg

查看它的檔案源碼

上傳圖片

結果瀏覽器中顯示

 

此時,在程式的同路徑下的upload檔案夾內就存有上傳後的照片

此時再看圖片源碼發現之前尾碼在圖片中的PHP代碼已經不見了。

實驗成功,圖片木馬中的php代碼被破壞。

在使用php中那些與影像處理有關的函數時,需要將GD2的DLL檔案php_gd2.dll作為一個擴充包含在php.ini中。
在上述實驗中,php的版本為5.3.10,其預設已經將DLL檔案包含,即extension=php_gd2.dll
原始碼中有兩個重要的函數分別是imagecreatefromjpeg()和imagejpeg()。函數細節如下:

1.imagecreatefromjpeg:由檔案或 URL 建立一個新圖象。
resource imagecreatefromjpeg ( string $filename )
參數:filename:JPEG映像的路徑。
傳回值:成功後返回映像資源,失敗後返回FALSE。

2.imagejpeg:輸出圖象到瀏覽器或檔案。
bool imagejpeg(resource $image[,string $filename[,int $quality]])
imagejpeg()從image映像以filename為檔案名稱建立一個JPEG映像。
參數:image:由圖象建立函數(例如imagecreatetruecolor())返回的圖象資源。
      filename:檔案儲存的路徑,如果未設定或為NULL,將會直接輸出原始圖象流。如果要省略這個參數而提供 quality 參數,使用NULL。
      quality:quality 為可選項,範圍從0(最差品質,檔案更小)到100(最佳品質,檔案最大)。預設為IJG預設的品質值(大約75)。
傳回值:成功時返回TRUE,或者在失敗時返回FALSE。

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.