PHP-文件上载和下载,不使用file_get_contents

时间:2013-02-13 13:30:23

标签: php

我使用以下php文件将数据上传到本地存储并将路径存储在db中。 文件正在上传。但是当我尝试下载它时,我没有得到该文件的内容。我只得到空白页。 我无法使用File_get内容方法,因为上传的大小数据可能会更大。我能为此做些什么?

这是我的上传代码Upload.php

$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{
    $filename = $_FILES["uploaded_file"]["name"];
    $location = "D:\\Uploads/";
    $path = $location . basename( $_FILES['uploaded_file']['name']); 
    move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
    $result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
    if ($result) 
    {
        do {
            if ($temp_resource = $mysqli->use_result()) 
            {
                while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
                    array_push($rows, $row);
                }
                $temp_resource->free_result();
            }
        } while ($mysqli->next_result());
    }
    echo "Successfully Uploaded";
}
else
{
    echo " Invalid File Type";
}

存储过程sp_upload_file如下所示。

 DELIMITER $$

USE `test`$$

DROP PROCEDURE IF EXISTS `sp_upload_file`$$

CREATE PROCEDURE `sp_upload_file`(IN id INT,IN filename VARCHAR(200),IN path VARCHAR(200))
BEGIN
IF EXISTS(SELECT `Training_Id`, `Material_Name`,Material_Path FROM `training_material` WHERE `Training_Id` = id AND `Material_Name` = filename AND `Material_Path` = path )
THEN

 UPDATE 
    `training_material` 
 SET 
    Training_Id =  id,
    Material_Name = filename,

    Material_Path = path,
    Modified_Date = NOW()
WHERE 
    `Training_Id` = id AND 
    `Material_Name` = filename AND 
    `Material_Path` = path ;
 ELSE
 INSERT INTO 
 `training_material`
    (`Training_Id`,
    `Material_Name`,
    `Material_Path`,

    `Created_Date`,
    `Modified_Date`)
 VALUES 
    (id,
    filename,
    path,

    NOW()
    ,NOW());
END IF;
END$$

DELIMITER ;

以下是下载文件的代码

if(isset($_GET['id']))
{
$id=$_GET['id'];
$mid=$_GET['mid'];
$query = "call sp_download_file('".$mid."')";
$result = mysql_query($query) or die('Error, query failed');
list($name) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
ob_clean();
flush();

exit;

}

以下是用于下载数据的sp:

DELIMITER $$

USE `test`$$

DROP PROCEDURE IF EXISTS `sp_download_file`$$

CREATE DEFINER=`root`@`%` PROCEDURE `sp_download_file`(IN id INT)
BEGIN
 SELECT Material_Name FROM training_material WHERE `Training_Material_Id` = id;

END$$

DELIMITER ;

请帮忙。我知道我需要更改download.php文件。但是不知道我需要做什么改变。

1 个答案:

答案 0 :(得分:0)

这是一个简单的下载脚本。您可以使用查询来获取文件(或相对)的完整路径。它内置了自己的缓冲区,因此无需使用ob_start

 if ($fd = fopen ($fullPath, "r")) {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }