PHP upload file, create a recursive directory instance code _php instance

Source: Internet
Author: User
Tags create directory

Copy Code code as follows:

<?php

$uid =$_request[' uid '];

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

//Create directory After success move temporary file
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);

//Returns the path to be stored by the picture
function Get_avatar ($uid, $size = ' middle ', $type = ') {
$size = In_array ($size, Array (' Big ', ' mi Ddle ', ' small ')? $size: ' Middle ';
$uid = ABS (Intval ($UID));
$uid = sprintf ("%09d", $uid);
$dir 1 = substr ($uid, 0, 3);
$dir 2 = substr ($uid, 3, 2);
$dir 3 = substr ($uid, 5, 2);
$typeadd = $type = = ' REAL '? ' _real ': ';
return $dir 1. ' /'. $dir 2. ' /'. $dir 3. ' /'. substr ($uid,-2). $typeadd. " _avatar_$size.jpg ";
}
///recursively create a directory, if the $dir is not an absolute path, then the directory that runs this method
function mkdirs ($dir) {
if (!is_dir ($dir)) {
If!mkdirs (    DirName ($dir))) {
return false;
    
if (!mkdir ($dir, 0777)) {
return false;

}
return true;
}
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.