<?php/** !!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE ImageMagick or some other quality image processing libraries*/    $imagePath = "temp/"; $allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG"); $temp = explode(".", $_FILES["img"]["name"]); $extension = end($temp); if ( in_array($extension, $allowedExts))   {   if ($_FILES["img"]["error"] > 0) {  $response = array( "status" => 'error', "message" => 'ERROR Return Code: '. $_FILES["img"]["error"], ); echo "Return Code: " . $_FILES["img"]["error"] . "<br>";
}   else {   $filename = $_FILES["img"]["tmp_name"];   list($width, $height) = getimagesize( $filename );   move_uploaded_file($filename,  $imagePath . $_FILES["img"]["name"]);   $response = array( "status" => 'success', "url" => $imagePath.$_FILES["img"]["name"], "width" => $width, "height" => $height   );    }   } else   {    $response = array( "status" => 'error', "message" => 'something went wrong', );   }      print json_encode($response);?>
以上是上传文件的代码 上传中文名字变成乱码 例如:閲戦攣.jpg 怎么把上传文件名字变成日期加随机数字例如:2016101800001.jpg 

解决方案 »

  1.   

    move_uploaded_file($filename,  $imagePath . $_FILES["img"]["name"]);
    这句改为
    move_uploaded_file($filename,  $imagePath . date('YmdHis') . '.' . pathinfo($_FILES["img"]["name"], PATHINFO_EXTENSION));
      

  2.   

    $destination = $imagePath.time().mt_rand(1000,9999).$extension;
    move_uploaded_file($filename,  $destination);
    $response = array(
    "status" => 'success',
    "url" => $destination,
    "width" => $width,
    "height" => $height
    );