Home >Backend Development >PHP Tutorial >Example of php uploading files and creating recursive directories

Example of php uploading files and creating recursive directories

WBOY
WBOYOriginal
2016-07-25 08:57:34897browse
  1. /**

  2. * Upload files Create recursive directories
  3. * edit: bbs.it-home.org
  4. */
  5. $uid=$_REQUEST['uid'];
  6. $avatar = 'D:/www/discuz/uc_server/data /avatar/'.get_avatar($uid, $size, $type);
  7. $dir=dirname($avatar);

  8. //Move the temporary file after the directory is successfully created

  9. if(mkdirs ($dir)){
  10.  if($_FILES["pic"]["error"] >= 0){
  11.   if(move_uploaded_file($_FILES['pic']['tmp_name'],$avatar)){
  12.    $errorcode=1;
  13.   }else{
  14.    $errorcode=0;
  15.    $errormsg="File move failed";
  16.   }
  17.   }else{
  18.   $errorcode=0;
  19.   $errormsg=$_FILES['pic'][ 'error'];
  20.  }
  21. }
  22. $back=array("errorcode"=>$errorcode,'errormsg'=>$errormsg);
  23. echo json_encode($back);

  24. < p>//Return the path where the image is to be stored
  25. function get_avatar($uid, $size = 'middle', $type = '') {
  26.  $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
  27. $uid = abs(intval($uid));
  28. $uid = sprintf("%09d", $uid);
  29. $dir1 = substr($uid, 0, 3);
  30. $dir2 = substr($uid, 3, 2);
  31. $dir3 = substr($uid, 5, 2);
  32. $typeadd = $type == 'real' ? '_real' : ' ';
  33.   return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
  34. }
  35. //Recursively create a directory. If the $dir passed is not an absolute path, it will be at the same level as the directory where this method is run.

  36. function mkdirs($dir){
  37.  if(!is_dir($dir)){
  38.   if(!mkdirs(dirname($dir))){
  39.    return false;
  40.   }
  41.  if(!mkdir($dir,0777)){
  42.    return false;
  43.   }
  44.   }
  45. return true;
  46. }
  47. ?> ;

Copy code


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