PHP code example of woodlsy / upload

1. Go to this page and download the library: Download woodlsy/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/ */

    

woodlsy / upload example snippets



use woodlsy\upload\Upload;

    try {
         $size      = '2M';         //上传文件最大尺寸
         $path      = '/upload';    //上传文件保存地址
         $fileName  = time();       //上传文件名称,可不填,自动生成唯一文件名
         $fieldName = 'file';       //上传字段名称
         $data = (new Upload())
                ->setFieldName($fieldName)
                ->setMaxSize($size)
                ->setUploadPath($path)
                ->compressImg(1024, 0, true) // 图片压缩
                ->upload($fileName);
    }catch (\Exception $e){
         echo '错误提示:'.$e->getMessage();
    }

$serverUrl = 'http://www.img.com/upload/img?project=test';
$data = (new Upload())->setServerUrl($serverUrl)->upload();

try {
    $project = $_GET['project'];
    $relativePath = $project.'/'.date('Ymd');
    $path = '/data/upload/'.$relativePath;
    $data = (new Upload())->setMaxSize('2M')->setUploadPath($path)->upload();
    $data['url'] = $relativePath.'/'.$data['name'];
}catch (\Exception $e){
    echo '错误提示:'.$e->getMessage();
}
//图片访问地址为 http://www.img.com/$data['url']