Laravel使用Accessors和Mutators上传多个文件

时间:2019-04-17 06:56:49

标签: php html laravel

我有一个访问器,用于设置上传图像的文件名和路径,以及一个用于检索照片URL的增幅器。它适用于单个文件上传,但我无法想到一种将其转换为用户库的多个文件上传的方法。

这是我在刀片中输入的内容

static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) {
    SecPolicyRef policy = SecPolicyCreateBasicX509();
    CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust);
    NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:(NSUInteger)certificateCount];
    for (CFIndex i = 0; i < certificateCount; i++) {
        SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);

        SecCertificateRef someCertificates[] = {certificate};
        CFArrayRef certificates = CFArrayCreate(NULL, (const void **)someCertificates, 1, NULL);

        SecTrustRef trust;
        SecTrustCreateWithCertificates(certificates, policy, &trust);

        SecTrustResultType result;
        SecTrustEvaluate(trust, &result);

        [trustChain addObject:(__bridge_transfer id)SecTrustCopyPublicKey(trust)];

        if (trust) {
            CFRelease(trust);
        }

        if (certificates) {
            CFRelease(certificates);
        }

        continue;
    }
    CFRelease(policy);

    return [NSArray arrayWithArray:trustChain];
}

static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) {
    return [(__bridge id)key1 isEqual:(__bridge id)key2];
}

static id AFPublicKeyForCertificate(NSData *certificate) {
    id allowedPublicKey = nil;
    SecCertificateRef allowedCertificate;
    SecCertificateRef allowedCertificates[1];
    CFArrayRef tempCertificates = nil;
    SecPolicyRef policy = nil;
    SecTrustRef allowedTrust = nil;
    SecTrustResultType result;

    allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificate);

    allowedCertificates[0] = allowedCertificate;
    tempCertificates = CFArrayCreate(NULL, (const void **)allowedCertificates, 1, NULL);

    policy = SecPolicyCreateBasicX509();
    SecTrustCreateWithCertificates(tempCertificates, policy, &allowedTrust);
    SecTrustEvaluate(allowedTrust, &result);

    allowedPublicKey = (__bridge_transfer id)SecTrustCopyPublicKey(allowedTrust);

    if (allowedTrust) {
        CFRelease(allowedTrust);
    }

    if (policy) {
        CFRelease(policy);
    }

    if (tempCertificates) {
        CFRelease(tempCertificates);
    }

    if (allowedCertificate) {
        CFRelease(allowedCertificate);
    }

    return allowedPublicKey;
}

这是我的模特

<input type="file" id="deal_img_input" class="form-control" name="featured_image> 

这是我的控制器

const PATH_PREFIX = 'public/deals';

public function getPhotoNameAttribute()
{
    if ($this->id && $this->featured_photo_extension) {
        return "$this->id.$this->featured_photo_extension";
    }

    return null;
}

public function getPhotoPathAttribute()
{
    if ($name = $this->getPhotoNameAttribute()) {
        return self::PATH_PREFIX . "/$name";
    }

    return null;
}

public function getPhotoUrlAttribute()
{
    $path = $this->getPhotoPathAttribute();
    if (Storage::exists($path)) {
        return Storage::url($path) . '?t=' . Storage::lastModified($path);
    }

    return '/assets/app/store/img/placeholder.jpg';

}

public function setPhotoExtensionAttribute($value)
{
    $path = $this->getPhotoPathAttribute();

    if (Storage::exists($path)) {
        Storage::delete($path);
    }

    $this->attributes['photo_extension'] = $value;
}

如果我有多个输入,如何修改代码?

    if ($photo_featured = $request->file('feautured_image')) {
        $deals->featured_photo_alt = 'Featured Photo';
        $deals->featured_photo_extension = $photo_featured->extension();
        $photo_featured->storeAs(Deal::PATH_PREFIX, $deals->photo_name);

    }
    $deals->save();

1 个答案:

答案 0 :(得分:0)

您是否尝试将“ multiple”属性添加到您的输入中?

<input type="file" class="form-control" name="gallery_image" multiple="multiple">

https://stackoverflow.com/a/1593259/9291504