php图像文件下载代码 [英] php image file download by code

查看:136
本文介绍了php图像文件下载代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于使用PHP下载文件

This concerns downloading files with PHP

我的php版本是5.3.5,我的apache是​​2.2.17

My php version is 5.3.5 and my apache is 2.2.17

我正在尝试下载我已经在我的服务器上传的文件(pdf,jpg,tiff),并且它们以相同的大小和类型下载但是看不到它们。我猜他们没有被拷贝正确。但是当我打开原始上传的文件时,他们工作的很好。
我已经看到几乎所有的建议出现的问题,没有人回答这个问题,只有类似的一个是这个,但仍然没有回答我的问题。

I am trying to dowload files (pdf,jpg,tiff) that I have uploaded in my server, and they download with the same size and type but I can not see them. I am guessing they are not copied right. But when I open the original uploaded ones they work just fine. I have seen almost all the questions that appeared as suggested and none of them answered the question, te only similar one is this, but still doesnt answer my question.

下载我正在使用此代码

       header("Content-type: application/force-download"); 
       header('Content-Disposition: inline; filename="' . $dir . '"'); 
       header("Content-Transfer-Encoding: Binary"); 
       header("Cache-Control: no-store, no-cache, must-revalidate");  
       header("Cache-Control: post-check=0, pre-check=0", false);  
       header("Pragma: no-cache"); 
       header("Content-length: ".filesize($dir)); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename="' . $file . '"'); 
       readfile("$dir"); 

其中 $ dir =62756d616769636e63 / 646973736572746174 / ehddggh / 1.JPG
$ file =1.JPG

给我一个提示我做错了什么,或者给我一个更好的解决方案来下载文件?

can anyone give me a hint on what I am doing wrong, or give me a better solution to download files?

推荐答案

答案。我正在将这个下载功能称为一个文件中的其他功能,所以最后我必须单独写一个脚本,除了其他文件。我的问题是,我需要它是安全的,只有下载一个文件,如果它属于用户和用户登录,所以我发送我需要的所有数据,加密和脚本里面我使用一系列的东西看看所有者是否真的是登录的用户。所以如果有人想知道这是我使用的代码,并且工作完美。

Thank you all for the answers. I was calling the download as a function in a file with other functions in it so in the end I had to write a script alone, apart from other files. My problem was that I needed it to be safe and to only download a file if it belonged to the user and the user was logged in, so I send all the data I need, ciphered and inside the script I use a series of things to see if the owner is really the logged user. So if anyone wants to know this is the code I used and works perfectly.

<?php
session_start();
$a=$_GET['a'];
$parts=explode("-",$a);
$tres=$parts[0];
$nombre=$partes[1];
$dbcodletra=substr($tres,2);
if($dbcod!=$_SESSION["username"])$boolt=0;
if($ext==1) $extl=".jpg";
if($ext==2) $extl=".jpeg";
if($ext==3) $extl=".tif";
if($ext==4) $extl=".tiff";
if($ext==5) $extl=".pdf";
if($ext==6) $extl=".doc";
if($ext==7) $extl=".docx";
if($tipoproy==1) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==2) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==3) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==4) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if (file_exists($dir) && $boolt) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($dir));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($dir));
    ob_clean();
    flush();
    readfile($dir);
    exit;
}else echo "<meta http-equiv=\"Refresh\" content=\"0;url=misdocumentos.php\">";
?>

这篇关于php图像文件下载代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆