检查文件是否已使用php slim 3上传到服务器

问题描述 投票:1回答:1

嗨,以下代码在slim保存文件

我想确保文件已上传到服务器,然后才返回true。或者是假的

我怎么能用Slim或PHP做到这一点?

文件日志的结果始终为空,并且正在上载文件

public function saveFiles(Array $files, $location) {
    try
    {
        /** @var UploadedFileInterface $file */
        foreach ($files as $file) {
            $fileLog = $file->moveTo($location . DIRECTORY_SEPARATOR . $file->getFilename());
        }

        return true;
    }
    catch(\Exception $e) {
        throw new Exception($e->getMessage());
php slim
1个回答
1
投票

根据这个PSR的精简实现,如果在上传文件时出现问题,它总是会抛出异常。

我不知道你是否因某种原因而抛出另一个异常,但你可以处理它:

public function saveFiles(Array $files, $location) {
    $result = true;

    foreach ($files as $file) {
        try {
            $fileLog = $file->moveTo($location . DIRECTORY_SEPARATOR . $file->getFilename());
        } catch(\Exception $e) {
            // Exception on file uploading happened, but 
            // we still continue loading other files
            $result = false;

            // Or just `return false;` if you don't want
            // to upload other files if exception happened
            // return false;
        }
    }

    return $result;
}

当然,可以扩展此方法以收集异常消息并返回它们。

© www.soinside.com 2019 - 2024. All rights reserved.