guessExtension example



        foreach ($files as $file) {
            if ($file === null) {
                return false;
            }

            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
                return true;
            }

            if (in_array($file->guessExtension()$params, true)) {
                return false;
            }
        }

        return true;
    }

    /** * Checks an uploaded file to verify that the dimensions are within * a specified allowable dimension. */
    


    /** * Extract the file information from the uploaded file, into the internal properties */
    private function setFileInfo()
    {
        if ($this->file === null) {
            return;
        }

        $extension = (string) $this->file->guessExtension();
        $name = $this->file->getBasename();

        if ($this->file instanceof UploadedFile) {
            // Load file information             $fileInfo = pathinfo($this->file->getClientOriginalName());
            $name = $fileInfo['filename'];

            if (isset($fileInfo['extension'])) {
                $extension = $fileInfo['extension'];
            }
        }

        
class FileTest extends TestCase
{
    public function testGetMimeTypeUsesMimeTypeGuessers()
    {
        $file = new File(__DIR__.'/Fixtures/test.gif');
        $this->assertEquals('image/gif', $file->getMimeType());
    }

    public function testGuessExtensionWithoutGuesser()
    {
        $file = new File(__DIR__.'/Fixtures/directory/.empty');
        $this->assertNull($file->guessExtension());
    }

    public function testGuessExtensionIsBasedOnMimeType()
    {
        $file = new File(__DIR__.'/Fixtures/test');
        $this->assertEquals('gif', $file->guessExtension());
    }

    public function testConstructWhenFileNotExists()
    {
        $this->expectException(FileNotFoundException::class);

        

    public function getExtension(): string
    {
        return $this->guessExtension() ?: $this->getClientExtension();
    }

    /** * Attempts to determine the best file extension from the file's * mime type. In contrast to getExtension, this method will return * an empty string if it fails to determine an extension instead of * falling back to the unsecure clientExtension. */
    public function guessExtension(): string
    {
        return Mimes::guessExtensionFromType($this->getMimeType()$this->getClientExtension()) ?? '';
    }
$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;
        }

        return (string) $extension;
    }
Home | Imprint | This part of the site doesn't use cookies.