Home  >  Article  >  Backend Development  >  PHP upload files and create recursive directory example code_PHP tutorial

PHP upload files and create recursive directory example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:55810browse

Copy code The code is as follows:

$uid=$_REQUEST['uid'];

$avatar = 'D:/avic/discuz/uc_server/data/avatar/'.get_avatar($uid, $size, $type);
$dir=dirname($avatar);

//Move the temporary files after successfully creating the directory
if(mkdirs($dir)){
 if($_FILES["pic"]["error"] >= 0){
if(move_uploaded_file($_FILES['pic']['tmp_name'],$avatar)){
  $errorcode=1;
  }else{
  $errorcode=0;
  $errormsg= "File move failed";
  }
 }else{
  $errorcode=0;
  $errormsg=$_FILES['pic']['error'];
  }
}
$back=array("errorcode"=>$errorcode,'errormsg'=>$errormsg);
echo json_encode($back);

//Return the path where the image is to be stored
function get_avatar($uid, $size = 'middle', $type = '') {
 $size = in_array($size, array('big' , 'middle', 'small')) ? $size : 'middle';
$uid = abs(intval($uid));
$uid = sprintf("%09d", $uid);
$dir1 = substr($uid, 0, 3);
$dir2 = substr($uid, 3, 2);
$dir3 = substr($uid, 5, 2);
$typeadd = $type == 'real' ? '_real' : '';
return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, - 2).$typeadd."_avatar_$size.jpg";
}
//Create a directory recursively. If the $dir passed is not an absolute path, it will be at the same level as the directory running this method
function mkdirs($dir){
 if(!is_dir($dir)){
 if(!mkdirs(dirname($dir))){
  return false;
  }
 if( !mkdir($dir,0777)){
   return false;
  }
  }
return true;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824901.htmlTechArticleCopy the code as follows: ?php $uid=$_REQUEST['uid']; $avatar = 'D: /avic/discuz/uc_server/data/avatar/'.get_avatar($uid, $size, $type); $dir=dirname($avatar); //After the directory is successfully created...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn