PHP code example of xjw / upload

1. Go to this page and download the library: Download xjw/upload library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xjw / upload example snippets


// 请使用异常捕获
try{
    // 配置说明
    $config = [
               'allowExt'       => ['mp3', 'mp4'], // 允许上传的文件类型;非必传,默认['*']
               'allowSize'      => '2M', // 允许上传的文件大小;非必传,默认2M;可选单位B,K,M,G
               'uploadRoot'     => '../resources/uploads/', // 文件上传的根目录;必传,末尾带/
               'savePath'       => 'path/to/xxx/', // 文件保存的子目录;必传,开头不带/,末尾带/
               'saveNameMode'   => 'random', // 文件名生成模式;非必传,默认random,可选值random--随机,original--源文件名,custom--自定义,需指定saveName
               'saveName'       => 'test.mp4', // 保存的文件名;saveNameMode=custom时必传,其他情况不用传
               'dayDir'         => true, // 是否按 /年月/日 的形式生成目录;非必传,默认true
               'isCover'        => false, // 是否覆盖同名文件;非必传,默认false,有同名文件时抛出异常
          ];
    $upload = new Xjw\Upload\Upload($config);
    $result = $upload->upload(); // 上传结果
    //var_dump($result);
    $r = $upload->getReturnData(); // 上传成功后返回的信息
    //var_dump($r);
}catch(Exception $e){
    var_dump($e->getMessage());
}