I use the following php file to upload data into a local storage and store the path in the db.
The files are getting uploaded.But when i try to download it i am not getting the content of the file. I get only blank page. i cant use File_get contents method since there may be bigger size data uploaded. What can i do for that?
here is my Uploading code 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";
}
The Stored Procedure sp_upload_file is as follows.
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 ;
The following is the code for Downloading the file
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;
}
The following is the sp for downloading data:
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 ;
please help in this.I know i need to change in download.php file.But dont know what changes i need to make.
Here is a simple download script. You can use your query to get the full path of the file (or relative). It has its own buffer built in so no need to use
ob_start