1

我使用以下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 文件。但不知道我需要进行哪些更改。

4

1 回答 1

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;
        }
    }
于 2013-02-13T13:47:13.980 回答