Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从服务器或本地xampp下载文件?php_Php_Download - Fatal编程技术网

如何从服务器或本地xampp下载文件?php

如何从服务器或本地xampp下载文件?php,php,download,Php,Download,我想从我的网站文件夹下载一个.pptx文件,以便下载或获得“另存为”弹出窗口,用户可以在其中选择要保存的文件夹。但是当我调用我的函数时,它会在console.log中打印内容 我使用了不同的代码,但没有人工作过 PK����Uy�G��-j����P����_rels/.rels���������������������JA��>Ő{w�D��^D�Md}�0���;3!�}{���B)��������l>Hʔ��uӂ��s�����{Z݃)�)��98Q��n�B3j])���TF

我想从我的网站文件夹下载一个.pptx文件,以便下载或获得“另存为”弹出窗口,用户可以在其中选择要保存的文件夹。但是当我调用我的函数时,它会在console.log中打印内容

我使用了不同的代码,但没有人工作过

PK����Uy�G��-j����P����_rels/.rels���������������������JA��>Ő{w�D��^D�Md}�0���;3!�}{���B)��������l>Hʔ��uӂ��s�����{Z݃)�)��98Q��n�B3j])���TF*FU~����"�&3�:��D�Z�`�d7m{g�'�Ls��`����6�b@E��Њ�n�N�ӡ�B�ϵ]Ή���^��^(����1��HI/y�Q)
ו�����-�2.�jY���9�%D����主键����Uy�G�我�����+����docProps/core.xml������������������M��J�@E�� ��-;�RL���J��4.�3.��<代码>����wb���R�西南���O�gq1�5-T(Z��ߞ�'�DM�F���E��~G�����C⬎�*� =�Ϊ�G:�7.�"��3fo��Y�D�ˌ���}D�J�Q�佤邦�五#�+-�����Eyb�A.�������-..... 还有更多

这是我的代码:

        function downloadProject($file, $db)
        {
            $path = "../assets/convertables/Presentaties/"; // change the path to fit your websites document structure
            $dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $file.'.pptx'); // simple file name validation
            $dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
            $fullPath = $path.$dl_file;
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
            header("Content-Transfer-Encoding: Binary");
            header("Content-length: ".filesize($fullPath));
            header("Content-disposition: attachment; filename=\"".$dl_file."\"");
            readfile($fullPath);          
        }
编辑:我已经检查了其他问题和答案,但对我来说不起作用!

您可以尝试以下方法:

首先,您需要定义下载文件的正确mime类型/内容类型

在您的例子中是:application/vnd.ms-powerpoint

因此,代码将是:

    $mime = 'application/vnd.ms-powerpoint';

   // Generate the server headers
    if( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) ) //id browser is IE
    {
        header( 'Content-Type: "'. $mime .'"' );
        header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
        header( 'Expires: 0' );
        header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
        header( "Content-Transfer-Encoding: binary" );
        header( 'Pragma: public' );
        header( "Content-Length: ".filesize( $data ) );
    }
    else
    {
        header( "Pragma: public" );
        header( "Expires: 0" );
        header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header( "Cache-Control: private", false );
        header( "Content-Type: ".$mime, true, 200 );
        header( 'Content-Length: '.filesize( $data ) );
        header( 'Content-Disposition: attachment; filename='.$filename);
        header( "Content-Transfer-Encoding: binary" );
    }
    readfile( $data );
    exit;

你可以在网站上了解更多信息

我如何否决你的评论…没有办法@TheJokerAez,我将删除此内容。不必担心下载文件会去哪里?我的ajax请求发送成功,但我看不到任何下载。哦,你在使用ajax…所以我有个主意:你可以使用ajax打开新的浏览器窗口(或选项卡)使用填充的下载url或使用document.location(download_url);我发送所选行名称的id,在php中,我通过$post获得此名称。我使用类和函数。请给我一个小例子?您是否从代码中下载了url?如果有,请在web address->enter上尝试过它,然后看看会发生什么(是否显示保存文件对话框)