getClientOriginalExtension example

public function uploadEsdFileAction()
    {
        $overwriteMode = $this->Request()->query->get('uploadMode');
        $file = $this->Request()->files->get('fileId');

        if (!$file instanceof UploadedFile) {
            $this->View()->assign(['success' => false]);

            return;
        }

        $extension = strtolower($file->getClientOriginalExtension());
        $blacklist = $this->esdFileUploadBlacklist;

        $blacklist = $this->container->get('shopware.event_manager')->filter(
            'Shopware_Controllers_Backend_Article_UploadEsdFile_Filter_EsdFileUploadBlacklist',
            $blacklist,
            [
                'subject' => $this,
            ]
        );

        if (\in_array($extension$blacklist, true)) {
            

        $uploadedFile = $this->createUploadedFile('An uploaded file.', \UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');
        $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
        $size = $symfonyUploadedFile->getSize();

        $uniqid = uniqid();
        $symfonyUploadedFile->move($this->tmpDir, $uniqid);

        $this->assertEquals($uploadedFile->getSize()$size);
        $this->assertEquals(\UPLOAD_ERR_OK, $symfonyUploadedFile->getError());
        $this->assertEquals('myfile.txt', $symfonyUploadedFile->getClientOriginalName());
        $this->assertEquals('txt', $symfonyUploadedFile->getClientOriginalExtension());
        $this->assertEquals('text/plain', $symfonyUploadedFile->getClientMimeType());
        $this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$uniqid));
    }

    public function testCreateUploadedFileWithError()
    {
        $this->expectException(FileException::class);
        $this->expectExceptionMessage('The file "e" could not be written on disk.');

        $uploadedFile = $this->createUploadedFile('Error.', \UPLOAD_ERR_CANT_WRITE, 'e', 'text/plain');
        $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);

        

    public function uploadAction()
    {
        $file = Symfony\Component\HttpFoundation\Request::createFromGlobals()->files->get('fileId');
        $system = new Filesystem();

        if (strtolower($file->getClientOriginalExtension()) !== 'zip') {
            $name = $file->getClientOriginalName();

            $system->remove($file->getPathname());

            throw new Exception(sprintf('Uploaded file %s is no zip file', $name));
        }
        $targetDirectory = $this->container->get(PathResolver::class)->getFrontendThemeDirectory();

        if (!is_writable($targetDirectory)) {
            $this->View()->assign([
                'success' => false,
                
$fileEntity = new ImportExportFileEntity();
        $fileEntity->assign($fileData);

        return $fileEntity;
    }

    public function detectType(UploadedFile $file): string
    {
        // TODO: we should do a mime type detection on the file content         $guessedExtension = $file->guessClientExtension();
        if ($guessedExtension === 'csv' || $file->getClientOriginalExtension() === 'csv') {
            return 'text/csv';
        }

        return $file->getClientMimeType();
    }

    public function getWriter(): AbstractWriter
    {
        return $this->writer;
    }

    

    public function uploadAction()
    {
        $file = Request::createFromGlobals()->files->get('emotionfile');
        $fileSystem = $this->container->get('file_system');

        if ($file->getClientMimeType() !== 'application/zip' && strtolower($file->getClientOriginalExtension()) !== 'zip') {
            $name = $file->getClientOriginalName();

            $fileSystem->remove($file->getPathname());

            $this->View()->assign([
                'success' => false,
                'message' => sprintf(
                    'Uploaded file %s is no zip file',
                    $name
                ),
            ]);

            
$this->View()->assign(['success' => true]);
    }

    /** * @throws Exception */
    private function prepareUploadedFile(array $params): array
    {
        // Check for a POSTed file         if ($this->Request()->files->has('file')) {
            $file = $this->Request()->files->get('file');
            $fileExtension = $file->getClientOriginalExtension();
            $fileName = $file->getClientOriginalName();

            if ($file->getError() !== UPLOAD_ERR_OK) {
                throw new \Exception(sprintf('Could not upload file "%s"', $file->getClientOriginalName()));
            }

            // use custom name if provided             if (!empty($params['name'])) {
                // Use the provided name to overwrite the file name, but keep the extensions to allow                 // automatic detection of the file type                 $fileName = $params['name'] . '.' . $fileExtension;
            }
trait MimeTypeValidationTrait
{
    /** * @param array<string, string[]> $allowedMimeTypes */
    protected function checkMimeType(UploadedFile $file, array $allowedMimeTypes): bool
    {
        foreach ($allowedMimeTypes as $fileEndings => $mime) {
            $fileEndings = explode('|', $fileEndings);

            if (!\in_array(mb_strtolower($file->getExtension())$fileEndings, true)
                && !\in_array(mb_strtolower($file->getClientOriginalExtension())$fileEndings, true)
            ) {
                continue;
            }

            if (\is_array($mime) && \in_array($file->getMimeType()$mime, true)) {
                return true;
            }
        }

        return false;
    }
}
$media->setPath($newFileName);
        }

        $this->modelManager->flush();
    }

    /** * @return string */
    private function getExtension(UploadedFile $file)
    {
        $extension = $file->getClientOriginalExtension();
        if (!$extension) {
            $extension = (string) $file->guessExtension();
        }

        $extension = strtolower($extension);

        switch ($extension) {
            case 'jpeg':
                $extension = 'jpg';
                break;
        }

        


    public function testGetClientOriginalExtension()
    {
        $file = new UploadedFile(
            __DIR__.'/Fixtures/test.gif',
            'original.gif',
            'image/gif',
            null
        );

        $this->assertEquals('gif', $file->getClientOriginalExtension());
    }

    public function testMoveLocalFileIsNotAllowed()
    {
        $this->expectException(FileException::class);
        $file = new UploadedFile(
            __DIR__.'/Fixtures/test.gif',
            'original.gif',
            'image/gif',
            \UPLOAD_ERR_OK
        );

        

    public function upload(UploadedFile $file, string $folder, string $type, Context $context, bool $isPrivate = false): string
    {
        $this->checkValidFile($file);

        $this->validator->validate($file$type);

        $mediaFile = new MediaFile(
            $file->getPathname(),
            $file->getMimeType() ?? '',
            $file->getClientOriginalExtension(),
            $file->getSize() ?: 0
        );

        $mediaId = $this->mediaService->createMediaInFolder($folder$context$isPrivate);

        $context->scope(Context::SYSTEM_SCOPE, function DContext $context) use ($mediaFile$mediaId): void {
            $this->fileSaver->persistFileToMedia(
                $mediaFile,
                pathinfo(Uuid::randomHex(), \PATHINFO_FILENAME),
                $mediaId,
                $context
            );
Home | Imprint | This part of the site doesn't use cookies.