php文件上传入门代码

php文件上传的入门代码。
<?php
/**
 * php 文件上传
 * by www.jbxue.com
*/ 
 if(!empty($_FILES["uploadImage"])) {
    // get file name
  $filename = basename($_FILES['uploadImage']['name']);
  
  // get extension
    $ext = substr($filename, strrpos($filename, '.') + 1);
    
    // check for jpg only
    if ($ext == "jpg") {
        // generate unique file name
     $newName = 'images/'.time().'.'.$ext;
     
     // upload files
         if ((move_uploaded_file($_FILES['uploadImage']['tmp_name'], $newName))) {
   
          // get height and width for image uploaded
          list($width, $height) = getimagesize($newName);
          
          // return json data
             echo '{"image" : "'.$newName.'", "height" : "'.$height.'", "width" : "'.$width.'" }';
         }
         else {
             echo '{"error" : "An error occurred while moving the files"}';
         }
    }
    else {
       echo '{"error" : "Invalid image format"}';
    }
 }
?>

你可能感兴趣的:(文件上传)