WooCommerce - 尝试使Up Up文件中的链接可点击

时间:2015-05-14 12:37:19

标签: php wordpress woocommerce

我在我的WordPress网站上使用Woo-Commerce插件。在这我使用计算器接收订单。每当用户上传任何文件时,我都会在后端获取有关该文件的路径,就像您在此链接中看到的那样http://promo.inkgility.com/wp-content/uploads/2015/05/Screenshot_2.png

上传文件:产品的路径显示
现在我要做的是使这个链接可以点击,但我找不到正确的代码。

1 个答案:

答案 0 :(得分:0)

请将此代码放入您的文件中,然后下载。创建新文件download.php并在下面的链接上获取此路径。

<a href="download.php?file=path/<?php echo row['file_name']?>">Download</a>

使用以下功能,您的图片将开始下载。

<?php
function download_file( $fullPath ){

  // Must be fresh start
  if( headers_sent() )
    die('Headers Sent');

  // Required for some browsers
  if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');

  // File Exists?
  if( file_exists($fullPath) ){

    // Parse Info / Get Extension
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);

    // Determine Content Type
    switch ($ext) {
      case "pdf": $ctype="application/pdf"; break;
      case "exe": $ctype="application/octet-stream"; break;
      case "zip": $ctype="application/zip"; break;
      case "doc": $ctype="application/msword"; break;
      case "xls": $ctype="application/vnd.ms-excel"; break;
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
      case "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
      default: $ctype="application/force-download";
    }

    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$fsize);
    ob_clean();
    flush();
    readfile( $fullPath );

  } else
    die('File Not Found');

}
?>

也可以这样下载。

<a href="/images/myw3schoolsimage.jpg" download>
  <img border="0" src="/images/myw3schoolsimage.jpg" alt="W3Schools" width="104" height="142">
</a>

一旦此代码正常运行,请告诉我。