【问题标题】:PHP download from outside htdocs从外部 htdocs 下载 PHP
【发布时间】:2013-06-23 06:34:05
【问题描述】:

我正在对下载和放入 zip 文件进行测试,当它位于 webroot/htdocs 中的单个文件(例如 images/)中时,我下载它没有问题,但我无法在 htdocs 或 images/ 之外下载它新建文件夹。这个表单代码现在工作正常,

    <form name="zips" method="post" action='downloadFile.php'>

        <input type="checkbox" name="files[]" value="makeZipinPHP.jpg" />
        <img src="images/makeZipinPHP.jpg" /><br>

        <input type="checkbox" name="files[]" value="tick.jpg" />
        <img src="images/tick.jpg" />

        <input type="submit" name="createpdf" value="Download as ZIP" />&nbsp;
        <input type="reset" name="reset"  value="Reset" />

    </form>

但是我可以将源代码更改为示例

   C:/images/New_Folder/tick.jpg

这是用于downloadFile.php的,它现在也可以使用,但我想更改

 $file_folder = "images/"

例如 $file_folder="C:/image/New_Folder/" 有可能吗?

    <?php
    $error = ""; //error holder
    if(isset($_POST['createpdf']))
    {
        $post = $_POST;
        $file_folder = "images/";// folder to load files
        if(extension_loaded('zip'))
        {
            // Checking ZIP extension is available
            if(isset($post['files']) and count($post['files']) > 0)
            {
                // Checking files are selected
                $zip = new ZipArchive(); // Load zip library
                $zip_name = time().".zip"; // Zip name
                if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
                {
                    // Opening zip file to load files
                    $error .= "* Sorry ZIP creation failed at this time";
                }
                foreach($post['files'] as $file)
                {
                    $zip->addFile($file_folder.$file); // Adding files into zip
                }
                $zip->close();
                if(file_exists($zip_name))
                {
                    // push to download the zip
                    header('Content-type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                    readfile($zip_name);
                    // remove zip file is exists in temp path
                    unlink($zip_name);
                }

            }
            else
                $error .= "* Please select file to zip ";
        }
        else
           $error .= "* You dont have ZIP extension";
    }
    ?>

【问题讨论】:

    标签: php html download directory


    【解决方案1】:

    $file_folder 更改为完整的系统文件夹路径应该可以正常工作,只要它为您的服务器环境(c:\\image\\New_Folder\\c:/image/New_Folder/ 在 Microsoft Windows 上)正确表达并且对 PHP 没有强制执行权限问题或限制(例如open_basedir 等)。

    【讨论】:

      【解决方案2】:

      您创建的服务器 (localhost) 的根目录位于“htdocs”,它无权访问您计算机的任何文件....

      因此,如果您想从服务器(Apache 或任何其他)下载/渲染任何内容,您必须将其放在 htDocs 文件夹中..

      【讨论】:

      • 即使文件位于 htdocs/image/New_folder 也无法访问?只有 htdocs/image?
      • 不可以访问....只要在 HTDOCS 文件夹中就可以完全访问
      • 而且您不必在文件路径中提及 htdocs 只需使用“/images/New_Folder”
      • 我仍然无法访问 /images/New_Folder 内部
      • 你用的是哪台服务器?
      猜你喜欢
      • 2013-09-11
      • 2021-11-19
      • 2012-10-10
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多