Skip to content
Advertisement

Download file not working well in safari with PHP

The code below is working well with all browsers but extension not show in safari. Maybe Content Type Issue ?

public function CheckRoutingURLAction($filename){
    $filename = 'https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-2000w.JPG';

try{
        $content = file_get_contents($filename, true, stream_context_create(array(
            "ssl" => array(
                "verify_peer" => false,
                "verify_peer_name" => false,
            ),)));
    }
    catch (Throwable $e){
        $logger = $this->get('logger');
        $logger->err($e->getMessage());
        throw new NotFoundHttpException('Page not found');
    }

    $response = new Response();
    $response->headers->set('Content-Type', 'mime/type');
    $response->headers->set('Content-Disposition',
        'attachment;filename=Noman.png'));
    $response->setContent($content);
    return $response;
}

Advertisement

Answer

I got the answer, that work in all browsers for me 🙂

        $response->headers->set('Content-Description',' File Transfer');
        $response->headers->set('Content-Disposition',' attachment; filename=' . $quoted);
        $response->headers->set('Content-Transfer-Encoding',' binary');
        $response->headers->set('Connection',' Keep-Alive');
        $response->headers->set('Expires',' 0');
        $response->headers->set('Cache-Control',' must-revalidate, post-check=0, pre-check=0');
        $response->headers->set('Pragma',' public');
        $response->headers->set('Content-Length', $size);
        $response->headers->set('Content-Type', mime_content_type($filename));
        $response->setContent($content);
        return $response;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement