duanmianxue2687 2013-04-26 21:40
浏览 27

如何保护此PHP上载脚本

I've read the Secure PHP Upload Scripts thread but I'm having difficulty getting this known good script to accept changes. I want this script to only allow .jpeg, .png, and .gif files. Could someone advise me on how to modify this script to do so?

<?php
$result=0;
if (trim($_POST["action"]) == "Upload File") { //**** User Clicked the Upload File Button

   //*********** Execute the Following Code to Upload File *************
   $imagename = basename($_FILES['image_file']['name']);  // grab name of file 
   $result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $imagename); // upload it 
   if ($result==1) echo("Successfully uploaded: <b>".$imagename."</b>"); // did it work?

} // end if
?>
<?php
if ($result==1) echo("<img src='".$imagename."'>"); // display the uploaded file
?>
  • 写回答

3条回答 默认 最新

  • dongwei2882 2013-04-26 21:46
    关注

    For the sake of brevity, i'm not doing any error checking.. but you can evaluate the extension of a file like this:

    $filename = $_FILES['image_file']['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if($ext !== 'jpg' && $ext !== 'png' && $ext !== 'gif') {echo 'error';}
    
    评论

报告相同问题?