PHP- Force download file from URL - php

I have problem. I created a web page with upload files, I have database (MySQL) with correctly URL to file (file path) (in database is only url to the file, file is in folder on the server). And now I trying write/search php script to download this file what was uploaded. All at web works well, but this download scripts don't work. I read and I think best way for me will be download with 'header', but I was trying and nothing. File is download to our disk, name is correctly, file extension ok, file is open in correct program, but if file have in name chars (';',':' etc.) file is download without these chars and is incorrectly (lack of extension, bad name). And second problem: all downloaded files are empty (0 b size), all is ok but they are empty, some tips ? Thanks in advance for help
My bad download codes:
<?php
$data=date('Y-m-d_H:i:s');
$nazwa=$_POST['downtitle'];
$urlek=$_POST['downurl'];
$extrozsz=$_POST['downext'];
$filePath = $urlek;
$fileName = $nazwa.$data.".".$extrozsz;
//------------------------------------------------------------------------
// $fd = fopen($filePath,"r");
// $size = filesize($filePath);
// $contents = fread($fd, filesize($filePath));
// fclose($fd);
// header("Content-Type: application/octet-stream");
// header("Content-Length: $size;");
// header("Content-Disposition: attachment; filename=$fileName");
// echo $contents;
//----------------------------------------------------------------------
// set_time_limit(0);
// while( $urlek = $response->fetch() )
// {
// $my_pliki = file_get_contents("$urlek");
// $my_file = fopen("$urlek","w+");
// fwrite($my_file,$my_pliki);
// fclose($my_file);
// }
//----------------------------------------------------------------------------
$quoted = $filePath;
$size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
?>

The filename parameter has some limits which are well described in RFC6266
So when you want use characters that are not present in the ISO-8859-1 character set then you need to use either filename* header or you need to strip/replace these characters from the filename. Nice solution to do so can be found here

Ok all is now ok, but another problem. I have code for showing news at website. All news are showing at same time (too long site). I wish to have at bottom of website numbers (at all websites under are this for forwarding on second subpage with older news). But how do it? Sup or sub is for small word, numbers etc, but all is retrieved form database:
$zapytanie='SELECT * FROM filetable WHERE Section="Other" ORDER BY ID DESC LIMIT 10';
This is in my php code query for showing latest 10 news, but if I add normally small numbers at bottom how construct code for showing next 10 news (no at where id="10-11" because if I will be have 1 thousand news or more it is too primitive) I must have automated code for showing pages with nextly 10 or 20 news how ?

Thanks, I edit my php at this:
<?php
$data=date('Y-m-d_H:i:s');
$nazwa=$_POST['downtitle'];
$urlek=$_POST['downurl'];
$extrozsz=$_POST['downext'];
$filePath = $urlek;
$fileName = $nazwa.$data.".".$extrozsz;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . $fileName."\"");
header("Content-Description: File Transfer");
readfile($filePath);
?>
And all is now ok, but I have one question now: In MySQL database I have title, description, added date, premiere date and I wish to have search field. I done it in input type="search", but what script now use for searching in this specified tables in database, firstly open database this I know, but what later what command is for search in database ? I know about 'like' but some better? And after all return correct result and show all from database where this search word is ok Some tips? Sorry for weak english and thanks.

Related

PHP- display a value in bracket in excel

I am converting HTML to excel file.
<?php
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=test.xls");
?>
<tr><td>(8.47%)</td></tr>
Here i want to display (8.47%) in display in excel but when i open the excel file it automatically convert and display as -8.47 .
I want my value to be display as (8.47%) not as -8.47.
Does quoting works ? Like you do in excel.
<tr><td>('8.47%)</td></tr>
Edit:
<?php
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=test.xls");
?>
<table>
<tr><td>(8.47%)</td></tr>
</table>
I just add <table></table> around and works for me

header("Content-Type: application/zip") and header("Content-Disposition: attachment; filename=$fileName") not working in wordpress?

The above two function not working in wordpress. I want to download file and i am running wordpress in xampp also i have tried it in another online server with wordpress framework still not working.
but this is working in another online server where i have not used wordpress framework.
Is there is problem with wordpress using the above two function?
(below code just take get request which is the path to the file to be downloaded from the server and after validating token the path is given from database)
<?php
ini_set('display_errors', -1 );
require('wp-blog-header.php');
require('wp-config.php');
$token = ($_GET["token"]);
$platform = ($_GET["platform"]);
$resolution = ($_GET["resolution"]);
$assetName =($_GET["assetName"]);
$currentTime = date("ymdHi" , time());
if($wpdb->query("SELECT * FROM wp_token_table WHERE token='$token'")){
$result = $wpdb->get_results("SELECT (path) FROM wp_path_table WHERE os='$platform' AND res = '$resolution' AND bundle_name= '$assetName'");
if($result){
$path = $result[0]->path;
$fileName = basename($assetName);
$filePath = $path;
if(!empty($fileName) && file_exists($filePath)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/zip");
header("Content-Length:".filesize($filePath));
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Transfer-Encoding: binary");
readfile($filePath);
exit;
}
}
}else echo "false";
?>
first of all lets verify my assumption is correct. In the wordpress index.php file, right at the top add this ( obviously after the <?php tag though )
ini_set('display_errors', -1 );
Let me know what that says when you try to download the file.
SQL Injection would let me
do this with your url
$token="'; SELECT * FROM wp_token_table WHERE 1 LIMIT 1; --";
And then your query would be this
"SELECT * FROM wp_token_table WHERE token=''; SELECT * FROM wp_token_table WHERE 1 LIMIT 1; --'"
The -- is start of comment to discard the ending ' then i would essentially select the first entry from that table. Or worse.
It's very important to prevent that.
For the error, I would do this
<?php
echo "hello";
/* -- rest of code */
And make sure the page works first. Once you know that you can rule out problems with the url, then uncomment bits of the code tell it breaks. That will show you where the error is. Unfortunately error reporting wont generally work if its on a page with a syntax error, because php cant even parse the page, so it cant run anything on it.

KML data manipulation using PHP

I have a big kml file I would just like to get certain parts of it.
<td>MUNICITY PSGC</td>
<td>153606000</td>
</tr>
<tr bgcolor="#D4E4F3">
<td>MUNICITY NAME</td>
<td>BUBONG</td>
<coordinates> 121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0</coordinates>
The end file should be (in an excel or csv file
Municity PSGC Municity Name Coordinates
153606000 BUBONG 121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0
Does anyone know of a PHP or Python Script to get this.
Thanks again guys.
I think you want to export to excel without HTML tags and show the result this way , so try this one :
<?php
header( "Content-type: application/vnd.ms-excel; charset=UTF-16LE" );
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='".date("Y-m-d")."'.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
?>
<table><tr><td>MUNICITY PSGC</td><td>MUNICITY NAME</td><td>coordinates</td></tr>
<tr align=center><td valign=top>153606000</td><td valign=top>BUBONG </td><td valign=top>121.9116664538692,6.448888587819113,0 121.9119650206403,6.449515350125353,0 121.9119422292838,6.449720472334889,0 121.9122248421055,6.45000080602091,0 121.9122248421055,6.450554635986066,0 121.912635086524,6.450967159540255,0 121.9127809512066,6.451110745086737,0 121.9127809512066,6.4513887996373,0 121.9124983383845,6.451666854187351,0 121.9124983383845,6.451944908737914,0 121.9122248421055,6.452222963288534,0 121.9122248421055,6.45305484780431,0 121.9116664538692,6.453610956904925,0 121.9116664538692,6.454440562285072,0 121.9108345693535,6.455277005071878,0 121.9108345693535,6.455555059622498,0 121.9105519565314,6.455833114172549,0 121.9105519565314,6.456111168723112,0 121.9102784602525,6.45639150240919,0 121.9091571255082,6.45639150240919,0 121.9077759692987,6.455001229657285,0 121.9077759692987,6.454720895971263,0 121.9075001938841,6.454445120556159,0 121.9075001938841,6.453610956904925,0 121.9076301046167,6.452879354358174,0 121.9078283894188,6.452177380575449,0 121.9080266742212,6.451500477284355,0 121.9082249590239,6.450880552385172,0 121.9083320783998,6.450292535384961,0 121.9083639862988,6.449711355791806,0 121.9084323603685,6.44918715459022,0 121.9088904666361,6.448610533268493,0 121.909163962915,6.44833247871793,0 121.9097223511518,6.44833247871793,0 121.9099730560743,6.448090890338392,0 121.9116664538692,6.448888587819113,0</td></tr><table>

How to give save dialog box by clicking on download button in php?

I am working on content based WAP application in which I will have to put
download button on selected content. Content can be (MP3,WAV,MP4,3gp,image). Content name is 9-digit random numeric character like 132432498.mp3, 814274691.mp4.
I tried below code
<?php if(isset($_GET['fileId'])){
$fileid = $_GET['fileId'];
{
i retrieve content content path and name together
like $filename="myserverip/1/3/4/132432498.mp3"
}
then
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$filename.";");
header("Content-Length: ".filesize($filename));
readfile($filename);
exit;
?>
< a href= < ? php echo $filename; ?>>Download < /a> '
But file automatically gets downloading with name 132432498.mp3.
I need help to write name of above content as I desire. How to do this in php?
It could be the case you are providing a name with has spaces, you have to prevent that enclosing it with double quotes, like this:
header('Content-Disposition: attachment; filename="'.$filename.'"');

Parse php generated png in html

I got a class that generates a png showing the result on a single page
header('Content-Type: image/png');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
imagepng($png,'test.png');
imagedestroy($png);
Instead of above, that prints the png successfully, I need to use the script so it generates several dynamically created pngs and be able to parse them in an html page.
Therefore I tried to uncomment the headers and return $png:
return $png;
and on the other side parse it
$png = $obj->pngGeneratorFuntion(1,30,'blank');
imagepng($png);
imagedestroy($png);
The results looks like this
�PNG IHDR.i ��PLTE���U��~tRNS#��fIDAT�c�$=�*y ���K�S����)IEND�B`��PNG IHDR.i ��PLTE���U��~tRNS#��fIDAT�c�$i]���a�P�{O��;>IEND�B`��PNG IHDR.i ��PLTE���U��~tRNS#��fIDAT�c�$Y����a�P�OMY�)"IEND�B`��PNG IHDR.i ��PLTE���U��~tRNS#��fIDAT�c�$yZ/y ���TKX{U#8MIEND�B`��PNG IHDR.i ��PLTE���U��~tRNS#��fIDAT�c�$<�2y ���>�Sq��M�IEND�B`�
and print_r($png) gives
Resource id #7
How can I achieve this. Thanks!
You can try base64_encode() this if you want to insert the image directly in HTML tags:
$png = $obj->pngGeneratorFuntion(1,30,'blank');
echo '<img src="data:image/png;base64,'.base64_encode($png).'">';
But it's not a good solution to insert image data directly into HTML
You can make a separate file image_process.php and send the data to that file with $_GET parameters and return image content. Then you can use:
<img src="image_process.php?text=ImageText" alt="" />

Categories