2014-03-24 3 views
0

우리 시스템은 사용자 이미지를 사용자 폴더에 저장함으로써 작동합니다. 사용자 폴더는 client_folders라는 하나의 폴더에 저장되는 마스터 사용자 폴더에 저장됩니다. client_folder/user_30/client_130/image.jpg를에게PHP 강제 다운로드, 전체 경로 이름을 파일 이름 지정없이 다운로드

: 나는 전체 파일 경로

예의 이름으로 다운로드받을 파일 (PHP 힘 다운로드 사용) 우리의 다운로드 스크립트를 구축하려고

매번 나는 단지 파일을 말하기를 원한다 image.jpg

나는 여러 가지를 시도했다. 배열 팝, basename()로 폭발했지만, readfile() 함수가 읽는 변수는이 옵션 중 하나를 시도 할 때마다 비어있다.

이제 전체 파일 경로 (파일 이름 포함)를 하나의 열에 저장하는 중입니다.이 작업을 수행하는 올바른 방법입니까? 난 그냥 전체 경로 이름 :(에서 ReadFile에서

ob_clean(); 
if(isset($_POST['file_name'])){ 
$file = $_POST['file_name']; 
header("Content-type: application/octet-stream"); 
header('Content-Disposition: attachment; filename="'.$file.'"'); 
readfile($file); 
exit(); 
} 

답변

5

당신이 통과해야와 함께 .... 전체 경로가이 성공적으로 다운로드 코드입니다 도움이된다면

없이 파일을 다운로드하는 방법 하지만, 파일 이름 파일 이름 헤더에 사용자에 대한 전체 경로 :

ob_clean(); 
if(isset($_POST['file_name'])){ 
$file_for_user = $_POST['file_name']; 
$full_path_file = "STORAGE_PATH".$file_for_user; 
header("Content-type: application/octet-stream"); 
header('Content-Disposition: attachment; filename="'.$file_for_user.'"'); 
readfile($full_path_file); 
exit(); 
} 
+0

주석 버전입니다; 이 경우 실제로 도움이되지 않으므로 http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/ – mando222

0
동적으로 MySQL의에서 참조 된 파일 경로를 사용하여 다운로드 할 파일을 당기는 경우

하고 파일 이름이 아닌 경로를 가지고 다운로드를 원하는 여기에 무엇을 하시겠습니까

여기
ob_clean(); 
if(isset($_POST['file_name'])){ 
$file = $_POST['file_name']; 
$filename = basename($file); 
header("Content-type: application/octet-stream"); 
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
readfile($file); 
exit(); 
} 

내가 헤더 ("콘텐츠 유형 : 응용 프로그램/octet-stream을")를 제거하는 것입니다

//This cleans the cache (just a precaution) 
ob_clean(); 

//Grab the file path from the POST global 
if(isset($_POST['file_name'])){ 

//Put the global variable into a regular variable 
$file = $_POST['file_name']; 

//Use basename() to separate the name of the file from the reference path and put it in a variable 
$filename = basename($file); 

//I have no idea if this is needed but everyone uses it 
header("Content-type: application/octet-stream"); 

//Use this header to name your file that is being downloaded, use the variable that is storing the file name you grabbed using basename() 
header('Content-Disposition: attachment; filename="'.$filename.'"'); 

//useread file function to send info to buffer and start download, use the variable that contains the full path 
readfile($file); 
exit(); 
}