2016-08-03 9 views
0

파일을 업로드하고 업로드 된 파일을 첨부 파일로 메일로 보내고 웹 페이지에서 파일을 다운로드해야합니다. 파일 내용은 mediumblob 데이터 유형으로 데이터베이스에 저장됩니다. 파일을 업로드하고 파일을 첨부 파일로 보내면 정상적으로 작동합니다. 파일 내용을 읽을 때 읽을 수있는 형식이 아닙니다. 여기 sendfile 파일을 첨부 파일로 업로드하고 파일을 다운로드하십시오.

PD9waHANCg0KLy8gTWFrZSBhIE15U1FMIENvbm5lY3Rpb24NCiRob3N0PSJUZWthbGFkYi5kYi45 
MTM1MTgyLmhvc3RlZHJlc291cmNlLmNvbSI7DQokdXNlcj0iVGVrYWxhZGIiOw0KJHBhc3N3b3Jk 

코드

upload.php로

<?php 
session_start(); 
// Read POST request params into global vars 
include_once "db.php"; 

$name1=mysql_real_escape_string(stripslashes($_POST['name1'])); 
$subject1=mysql_real_escape_string(stripslashes($_POST['subject1'])); 
$email1=$_POST['email1']; 
$phone=$_POST['phone']; 
$username=mysql_real_escape_string($_SESSION['sess_user4']); 
    $to = "[email protected]"; 
$from=$_POST['email1']; 
$fileatt  = $_FILES['fileatt']['tmp_name']; 
$fileatt_type = $_FILES['fileatt']['type']; 
$fileatt_name = mysql_real_escape_string($_FILES['fileatt']['name']); 
$fileatt_size = $_FILES['fileatt']['size']; 
$aa=filesize($fileatt); 
$headers = "From: $from"; 
if (is_uploaded_file($fileatt)) { 
    // Read the file to be attached ('rb' = read binary) 
$name1=mysql_real_escape_string(stripslashes($_POST['name1'])); 
    $file = fopen($fileatt,'r'); 
    $content = fread($file,filesize($fileatt)); 


fclose($file); 
$message="Name:$name1\n\n 
      Email:$email1\n 
      Subject:$subject1\n 
      Phone No:$phone"; 
    // Generate a boundary string 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 


    // Add the headers for a file attachment 
    $headers .= "\nMIME-Version: 1.0\n" . 
       "Content-Type: multipart/mixed;\n" . 
       " boundary=\"{$mime_boundary}\""; 

    // Add a multipart boundary above the plain message 
    $message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mime_boundary}\n" . 
      "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      stripslashes($message). "\n\n"; 

    // Base64 encode the file data 
    $content = chunk_split(base64_encode($content)); 

    // Add file attachment to the message 
    $message .= "--{$mime_boundary}\n" . 
       "Content-Type: {$fileatt_type};\n" . 
       " name=\"{$fileatt_name}\"\n" . 
       //"Content-Disposition: attachment;\n" . 
       //" filename=\"{$fileatt_name}\"\n" . 
       "Content-Transfer-Encoding: base64\n\n" . 
       $content . "\n\n" . 
       "--{$mime_boundary}--\n"; 

} 

// Send the message 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<script>window.open('display.php','_self')</script>"; 
} else { 
    echo "<p>Mail could not be sent. Sorry!</p>"; 
} 
$query = "INSERT INTO register (name,type,size,content,name1,email1,subject1,phone,username) VALUES ('$fileatt_name','$fileatt_type','$fileatt_size','$content','$name1','$email1','$subject1','$phone','$username')"; 

mysql_query($query) or die('Error, query failed'); 

?> 

가 download.php

<html> 
     <head> 
      <title>Download File From MySQL Database</title> 
      <meta http-equiv="Content-Type" content="text/html; 
        charset=iso-8859-1"> 
     </head> 
     <body> 
     <?php 
    include_once "db.php"; 
    if(isset($_GET['id'])) { // if id is set then get the file with the id from database 
    $id = $_GET['id']; 
$content = chunk_split(base64_decode($content)); 
    $query = "SELECT name, type, size, content FROM register WHERE id = $id"; 
    $result = mysql_query($query) or die('Error, query failed'); 
    list($name, $type, $size, $content) = mysql_fetch_array($result); 
    header("Content-length: $size"); 
    header("Content-type: $type"); 
    header("Content-Disposition: attachment; filename=$name"); 
    echo $content; exit; 
    } 
    $query = "SELECT id, name FROM register"; 
    $result = mysql_query($query) or die('Error, query failed'); 
    if(mysql_num_rows($result) == 0) 
    { 
    echo "Database is empty"; 
    } 
    else 
    { 
    while(list($id, $name) = mysql_fetch_array($result)) 
    { 
    ?> 
    <a href="download.php?id=<?php echo $id;?>"><?php echo $name; ?></a> 
    <?php 
    } 
    } 
    ?> 
    </body> 
    </html> 

답변

0

나는 문제가 헤더, 이미 세트에 생각대로 내용이 표시됩니다 .

<?php 
include_once "db.php"; 
if (isset($_GET['id'])) { // if id is set then get the file with the id from database 
    $id = $_GET['id']; 
    $query = "SELECT name, type, size, content FROM register WHERE id = $id"; 
    $result = mysql_query($query) or die('Error, query failed'); 
    list($name, $type, $size, $content) = mysql_fetch_array($result); 
    header("Content-length: $size"); 
    header("Content-type: $type"); 
    header("Content-Disposition: attachment; filename=$name"); 
    echo base64_decode(str_replace("\n", "", $content)); 
    exit; 
} else { 
?> 
<html> 
<head> 
    <title>Download File From MySQL Database</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 
<body> 
<?php 
$query = "SELECT id, name FROM register"; 
$result = mysql_query($query) or die('Error, query failed'); 
if (mysql_num_rows($result) == 0) { 
    echo "Database is empty"; 
} 
else { 
    while (list($id, $name) = mysql_fetch_array($result)) { 
     ?> 
     <a href="download.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> 
     <?php 
    } 
} 
} 
?> 
</body> 
</html> 
+0

위 코드를 추가해도 문제는 동일합니다. 업로드 된 파일은 내용을 읽지 만 다운로드 된 파일은 내용을 다음과 같이 읽습니다. UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA – user6582683

+0

당신이 base64로 파일을 인코딩 된대로, 내가 여기에 누락 된 유일한 것은 base64로 디코딩 –

+0

생각 오프 물론 chunk_split을 되돌려 야, 내가 그것을 download.php에서 base64로 디코딩을 사용하는 경우 새로운 라인 –

관련 문제