PHP强制下载.png文件

时间:2016-04-01 00:34:21

标签: c# php unity3d download ftp

您好,谢谢您的时间。

我正在尝试使用以下代码强制下载从我开发的Unity应用程序创建到用户计算机上的.png图像。

sort -k3,3

我有这个,但它没有弹出下载确认框,或者对提供的图像做任何事情。

在浏览器中运行我的程序时,在调用上面的脚本时,我会遇到两个错误之一。由于我从dropbox托管应用程序,我得到"无法从https发送到http",当我将php文件的地址更改为https时超时。

首先,有没有办法获得https,http允许两者之间的通信,我宁愿不必在ftp服务器上托管我的统一项目,因为我有一些项目以相同的方式设置。第二,PHP脚本是否正常工作,如果有的话,我可以删除一些东西吗?

1 个答案:

答案 0 :(得分:0)

以下代码将成功强制下载文件“example.png”:

<?php

// requested file's name
$file_name = "example.png";

// make sure it's a file before doing anything
if(is_file($file_name))
{
  // required for IE
  if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

  // get the file mime type using the file extension
  switch(strtolower(substr(strrchr($file_name,'.'),1)))
  {
      case 'pdf': $mime = 'application/pdf'; break;
      case 'zip': $mime = 'application/zip'; break;
      case 'jpeg':
      case 'jpg': $mime = 'image/jpg'; break;
      case 'png': $mime = 'image/png'; break;
      default: $mime = 'application/force-download';
  }
  header('Pragma: public');   // required
  header('Expires: 0');       // no cache
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Cache-Control: private',false);
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.filesize($file_name));    // provide file size
  readfile($file_name);       // push it out
}

?>