Upload pictures, upload pictures in bulk

Source: Internet
Author: User

1, Upload files

/**
  upload file
  $file
 int|string
* Create a star model in models
*/ Public functionUploadFile ($file) { if($_files [$file]) { if($_files [$file] [ ' ERROR '] > 0) return0; $path = ' upload/'. Date "Ym") . '/' . Date ' d ') . '/'; Picture upload Path if(!file_exists (DirName (dirname ($path))) mkdir (DirName (dirname ($path))); if(!file_exists (DirName ($path))) mkdir (DirName ($path)); if(!file_exists ($path)) mkdir ($path); Allow uploading of file formats $pic = Array( "Image/gif", "Image/jpeg", "Image/jpg", "Image/png"); Check whether the uploaded file is in the type allowed to upload if(!in_array ($_files [$file] [ "Type"], $pic)) return0; Not properly formatted if($_files [$file] [ "Name"] {$file _name = $_files [$file] [ "Name"]; $name = $path. Uniqid (). $file _name; if(Move_uploaded_file ($_files [$file] [ "Tmp_name"] ($name)) {//move uploaded files to a new location return$name; } Else{ return0; } } } Else{ return0; } }

/**

* Called in the Controller

* @name Call the UploadFile function method here

*/


Actioncreatestar ()
{
    if(!  Isset($_files[' img ')) return 0; Picture does not exist
    //upload picture
     Star ();
    $upload = $img->uploadfile (' img '); Upload personal cover
    if0;  Upload picture failed, then return 0
}

2. Upload pictures in bulk


/**
 *@nameBulk Upload Pictures *@param$file _name *@returnArray|bool|int * Create the STAR model in models * *Public functionUploadmorefile ($file _name) {$files = $_files[$file _name];if(!is_array ($files [' name '])) return0; $dir =' upload/'. Date (' Ym ').'/'. Date (' d ');if(!file_exists (DirName (dirname ($dir))) mkdir (DirName (dirname ($dir)));if(!file_exists (DirName ($dir))) mkdir (DirName ($dir));if(!file_exists ($dir)) mkdir ($dir); $img _url =Array();foreach($files [' name '] as$k => $v) {if($_files[$file _name][' ERROR '[$k] > 0)Return false; FileName $dir =' upload/'. Date (' Ym ').'/'. Date (' d '); $tmp _name = $_files[$file _name][' Tmp_name '[$k]; $name = Uniqid (). STRSTR ($_files[$file _name][' name '[$k],'.'); Picture name $dir = $dir.'/'. $name;if(!move_uploaded_file ($tmp _name, $dir)) {if($img _url) {foreach($img _url as$k 1=> $v 1) {if(File_exists ($v 1)) unlink ($v 1); }unset($img _url); }Return false;
    } $img _url[] = $dir; } return$img _url; }
  /**
      release Dynamic
     * Call the Uploadmorefile () function method in the models in the Star controller *
     *

     actiondynamic ()
     {
    
         if(!  Isset($_files[' img ')) $this->returnjson (0,' please upload picture ');
         Upload pictures to Dynamic picture table
          Star ();
         $upload = $img->uploadmorefile (' img ');
         if (! $upload) $this->returnjson (0,' picture upload failed ');
       

     }



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.