I'm trying to download a XML file from my DB. The file is stores in a BLOB field, but when I open the page it dowloads a blank xml file.This is my function:
<?php
$conexao = mysql_connect("db","user","pss") or die('Not connected : ' . mysql_error());
mysql_select_db("db", $conexao) or die (mysql_error());
$result = mysql_query("select * from xmlNFe where xml_id = 1", $conexao);
$row = mysql_fetch_array($result);
$xml = $row['xml_xml'];
$nome = mysql_result($result, 0, 'xml_chNFe');
header('Content-type: application/octet-stream');
header('Content-Description: File Transfer');
header("Pragma: public");
header("Content-Disposition: attachment;filename=".$nome.".xml");
header('Content-Transfer-Encoding: binary');
header("Content-length: ".filesize($xml));
header('Accept-Ranges: bytes');
ob_clean();
flush();
echo $xml;
mysql_close($conexao);
exit;
?>
Does anyone have any idea?
header("Content-length: ".filesize($xml));
^^^^
$xml is your actual xml data, it's NOT a filename, so filesize() will FAIL and return boolean FALSE. You cannot get the size of a file which does not exist.
Try
header("Content-length: ".strlen($xml));
^^^^^^
instead.
Related
I am trying to download the files from database. but it shows "failed to load" message.Uploading is working fine. i can upload pdf, jpg, dox file. but unable to open when i download.IF there are three href links in my file. first is for "upload" second is for "delete". every link download the file from image even other links are for other work but every file calls the downld.php file
<?php echo $name;?>
downld.php
<?php
if (isset($_GET['id']))
{
$project_id = $_GET['id'];
$myConnection= mysqli_connect("localhost","root","", "extra") or die ("could
not connect to mysql");
mysqli_set_charset($myConnection,'utf-8');
global $wpdb;
$sql = "SELECT name, type, size, content " .
"FROM wp_postprojectwin WHERE project_id = $project_id";
$result = mysqli_query($myConnection,$sql) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
mysqli_close($connection);
ob_clean();
flush();
echo $content;
exit;
}
else{
echo "sorry";
}
?>
I have the following PHP code using headers to download a file from the server:
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
This code works fine, however I have it in a file with the HTML form and it is ran using an if isset all of the HTML code is placed in the file, as well as anything I echo out in PHP; and the data I actually want in the file is there at the end.
I can't figure out what in the headers is causing it to write everything that is on screen to the file when downloaded. The file on he server isn't changed and is how it should be.
Thanks for any help.
Heres all the code...
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"><br>
Order Number: <input type="number" name="orderNo" size="10"/>
<input type="submit" name="Download" value="Download" />
</form>
</body>
</html>
<?php
if(isset($_POST['Download'])){download();}
function download()
{
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn);
$result = mysql_query("SELECT ID, PurchaseDate, BuyerName, ShipCity, ShipState, ShipPostalCode, ShipCountry,
ShipAddress1, ShipAddress2, ShipAddress3 FROM imported_orders");
$orderNo = $_POST['orderNo'];
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if($orderNo>0&&$orderNo<=count($row))
{
$file = fopen('Order.txt', 'w');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if($row['ID']==($orderNo))
{
echo "Hello";
fwrite($file, $row['BuyerName'].PHP_EOL .$row['ShipAddress1'].PHP_EOL .$row['ShipAddress2'].PHP_EOL .$row['ShipAddress3'].PHP_EOL .$row['ShipCity'].PHP_EOL .$row['ShipState'].PHP_EOL .$row['ShipPostalCode'].PHP_EOL .$row['ShipCountry'].PHP_EOL);
}
}
fclose($file);
$file = 'Order.txt';
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
else{echo "Please enter a valid Order Number"; echo $orderNo;}
}
move this code at very begin of file, so you will have:
<?php
if (your condition to output file) {
$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
die();
}
the rest of your file....
another solution, at the very top of file add:
<?php
ob_start() or die('Cannot start output buffering');
... your page
and just before the first header add:
$file = 'Order.txt';
ob_end_clean();
header("Cache-Control: public");
...
readfile($file);
die();
I have a download script written in PHP. My view file script links to the ids and then selects all the data that matches the ID.
The data is then used to download the photo. Does it matter that my photo is in a folder? It is moved to a folder and then the directory is uploaded to the MYSQL database.
The code at the moment now allows some files to download perfectly and then the majority to be damaged. Any reason why?
Mysql table info...
$cool = $_GET['id'];
$sql = "SELECT id, type, name, size FROM upload WHERE id='$cool'";
$result = mysql_query($sql, $db);
$data = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
$size = mysql_result($result, 0, "size");
$type = mysql_result($result, 0, "type");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($name);
exit();
This happens because PHP send some information after you echo the data, the solution to this is to stop processing right after you echoed the data, for this add exit(); right after echo $data.
I want to download blob file from oracle, that is PDF file, this is my code to get and download file:
<?php
$conn = ocilogon('user', 'pass', '//localhost/XE');
$sql = "SELECT PDFFILE FROM TFILE";
$stid = ociparse($conn,$sql);
ociexecute($stid);
$rowResult = ocifetch($stid);
settype($arrayResult,"array");
if($rowResult != null){
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
}
?>
but when i run this code,i not get pdf file..
something wrong with my code??
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . OCIResult($stid,'PDFFILE') . '"');
header("Content-Length: " . filesize(OCIResult($stid,'PDFFILE')->load()));
header('Content-Disposition: attachment; header("Content-Transfer-Encoding: binary\n");
You're sending the Content-Disposition header twice. You probably don't even need the second one, the client should know all it needs to know about the stream from the Content-Type header. Omit the second Content-Disposition so you're not over-writing the header that has the filename.
I am recording the time at which the user downloaded a specific file using the following code. However, in this code, initially the download time is coming but later it's disconnecting the data base connection between client and server. If I remove the 'exit' (as shown), everything is coming fine but the downloaded file can be corrupted or damaged.
Can anyone check this code and explain what is wrong with it? I think the problem is with the exit, but what can I use instead of exit?
<?php
$f_name = $_POST["fn"];
$file = "../mt/sites/default/files/ourfiles/$f_name";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
//ob_clean();
// flush();
readfile($file);
// exit;
}
$con = mysql_connect("localhost","mt","mt");
if (!$con) {
die('Could not connect: ' . mysql_error());
} else {
echo "Connected";
}
// Create table
mysql_select_db("mt", $con);
mysql_query("INSERT INTO down_time (FileName,DateTime)
VALUES ('".$f_name."',NOW())");
mysql_close($con);
?>
If that happens on large files and/or slow connections, try to tweak max_execution_time in php.ini or from script using ini_set function.
well if you include the exit, your code just doesn't come to the point where it should insert the filename into db.
if you don't include the exit, you send the file contents and append "Connected" to it so the file has to be corrupted.
maybe you can try ob_start and ob_end_clean around your db stuff: http://php.net/manual/en/function.ob-start.php
this prevents anything from being sent to the output, so you don't have to use exit but nothing gets sent to the output after your file so it doesn't get corrupted
something like:
readfile($file);
}
ob_start();
$con = mysql_connect("localhost","mt","mt");
//all the DB stuff
mysql_close($con);
ob_end_clean();
?>
you can include exit after the ob_end_clean() just to be sure but this should work just fine.
Try:
<?php
$f_name = $_POST["fn"];
$file = "../mt/sites/default/files/ourfiles/$f_name";
if (!file_exists($file)) { die('File not found'); }
if (!$con = mysql_connect("localhost","mt","mt")) { die(mysql_error()); }
if (!mysql_select_db("mt")) { die(mysql_error()); }
$q = "INSERT INTO `down_time` (`FileName`, `DateTime`) VALUES ('"
. mysql_real_escape_string($f_name) . "',NOW())";
if (!mysql_query($q)) { die(mysql_error()); }
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($file);