How To Rename Files that Being Downloading using Javascript and PHP Header? - javascript

just like my title how can I do that thing?
I tried to code like below (in file view_report.php)
<?php
header('Content-Disposition: inline;filename=report_loader.txt');
?>
<div id="reportTable">
<table>
<thead>
<tr>
<th>No.</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php foreach($reports->result() as $rows): ?>
<tr>
<th><?php $rows->no; ?></th>
<th><?php $rows->name; ?></th>
</tr>
</tbody>
</table>
</div>
<div class="row">
<input type="button" class="btn btn-success btn-sm" value="Generate Excel" id="exportExcel" />
</div>
<script>
$("#exportExcel").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#reportTable').html());
e.preventDefault();
});
</script>
EDIT
That is my entire code. I already tried it. I referred some questions in this Site, but I don't fine clean solution for me. I actually have tried to use Content-Disposition: inline and Content-Disposition: attachment. But still not working. The browser (I tried on Chrome and Firefox), it returns random file name, not the file name I want. Can anyone help me to find this solution?

I think there is no "inline" for Content-Disposition.
Can you try:
header('Content-Disposition: attachment;filename=report_loader.txt');
Update:
Sry, there IS inline for Content-Disposition. But you should use that if you want the browser to display the content, not download it. Therefore it will probably ignore the filename attribute.
Source:
http://www.ietf.org/rfc/rfc1806.txt
Update 2:
Try this:
var blob = new Blob([$('#reportTable').html()], {type: "text/html;charset=utf-8"});
saveAs(blob, "table.html");
This should save the HTML from #reportTable as table.html
Note: If you want to create an Excel file, you will need more than just the html source.

Example:
Download Your Downloading Thingy
Please, check this documentation:
https://developer.mozilla.org/en/docs/Web/HTML/Element/a
It states:
download
This attribute, if present, indicates that the author intends the hyperlink to be used for downloading a resource so that when the user clicks on the link they will be prompted to save it as a local file. If the attribute has a value, the value will be used as the pre-filled file name in the Save prompt that opens when the user clicks on the link (the user can change the name before actually saving the file of course). There are no restrictions on allowed values (though / and \ will be converted to underscores, preventing specific path hints), but you should consider that most file systems have limitations with regard to what punctuation is supported in file names, and browsers are likely to adjust file names accordingly.

You can try this:
$file_name='abc.txt';
header("Content-Disposition: attachment; filename=" .$file_name);

Related

How to download big size images using <a> tag

I'm trying to connect to the database and view pictures from there. Despite the fact that all pictures are shown, it is not possible to download them. To download I use tag .
Here is my code.
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td align="center">
<a href="data:image/jpeg;base64,'.base64_encode($row['Pilt'] ).'" download>
<img src="data:image/jpeg;base64,'.base64_encode($row['Pilt'] ).'" width=400 class="img-thumnail" />
</a>
</td>
</tr>
';
}
Here is a small part of the database where I get pictures. https://imgur.com/yZGsFzb . So I noticed that only one photo is being downloaded, which size is 150.4KiB.
I expect that by clicking on the image (any size) it will be downloaded. Right now when I click on image appears error like this https://imgur.com/yZrlIH8 whereas I would like to see something like this https://imgur.com/4vkUukp .
I will be very grateful for any help.
I recommend you stop printing large quantities of big images that way, they will be hardcoded in the HTML code and downloaded all at once to the browser's cache, 2 times (3 with the click included). This wastes RAM and bandwidth. The network error you get is probably caused by the abnormally big size of the href in your link (might even look a bit like malware to your browser).
Instead, you want to serve these images dynamically on a PHP script first (by setting the header to 'Content-Type: image/png' and printing the image contents as obtained from your database) and provide a direct URL to the script.
This example works just as described in your screenshots, I'm indicating where you should replace your SQL queries to obtain the image:
while(for all your rows)
{
echo '
<tr>
<td align="center">
<a href="image.php?id=' . $id . '" download>
<img src="image.php?link=' . $id . '" width=400 class="img-thumnail" />
</a>
</td>
</tr>';
}
And image.php would look something similar to:
<?php
$id = htmlspecialchars($_GET['id']);
header('Content-Type: image/png');
$content = store the image contents from the BD here ($row['Pilt'] in your code);
echo $content;
Notice that image.php serves images dynamically and I'm supposedly identifying each image with an id (perhaps your row id? You can think of more secure identifiers too if ids are not public already). This is a common security practice in PHP so that you're not providing absolute file paths or other information to attackers.

How to export dynamic html table to doc file using php?

I'm new using PHP and I need a bit of help here.
I'd like to send a HTML table to another PHP file, and then, be to able to use this information (specifically I want to download this like DOC file).
I've seen a lot of information how to do it. But I haven't seen how to do without <tbody></tbody>. I have a dynamic table, so, the data is loading from an array. By the way, I'm using DataTable-jQuery to do it.
I have the following HTML code:
<form action="sectoresTable.php" method="post">
<table id="sectoresTable">
<thead>
<tr>
<th><b>#</b></th>
<th><b>Numero</b></th>
<th><b>Nombre</b></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<button type="submit" style="margin-top: 20px;">Exportar</button>
</form>
and sectoresTable.PHP:
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=TablaSectores.doc");
echo '';
?>
By the way, to load the data into the table, I'm using the following script:
<script>
$('#sectoresTable').DataTable({
data: arraySectores
});
</script>
In general all this is working good, I download a doc file but without information (and That is right because my echo is printing nothing.).
I understand that I need to use a foreach in my HTML code? But really, I'm not sure.
try to use an api,to export dynamic html table to doc file in php
http://www.phpclasses.org/package/2763-PHP-Convert-HTML-into-Microsoft-Word-documents.html

Submitting embedded pdf in html form

I've searched in vain for days, but haven't found a solution for my problem yet.
Ideally, I would like to embed a fillable pdf form into an intranet html form for submission to the server for processing (ability to parse the field/values would be gravy, but not required). The files are all in the same domain so no cross-domain issues. I know I could add submission functionality to the pdf form itself, but 1) scripting is beyond the ability of the pdf document administrator and I don't want to take that on, 2) there are hundreds of pdf documents, 3) I need additional scripted fields/values submitted with the form, 4) I want the pdf document to be contained within the login session. So far, the server log shows all the field/values, except the PDFInput parameter which is passed, but the value is empty.
Here's what I have so far:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(uploadForm).on("submit", function(event) {
var iframe = document.getElementById('PDFObj');
var iframeDocument = [iframe.contentDocument || iframe.contentWindow.document];
var pluginData = iframeDocument;
$(this).append('<input type="file" name="PDFInput" id="PDFInput" value="' + pluginData + '" style="visibility:hidden"/>');
return true;
});
});
</script>
and
<form enctype="multipart/form-data" method="post" name='uploadForm' id='uploadForm'>
<input type='hidden' name='rm' id='rm' value='uploadFile'>
<table align='center'>
<tr>
<td align='left'>
<strong>Notes:</strong>
<br>
<textarea cols='80' rows='2' name='notes' id='notes'></textarea>
<br>
<br>
</td>
</tr>
<tr>
<td colspan=2 align='center'>
<input type='submit'>
</td>
</tr>
<tr>
<td colspan=2>
<br>
<input type='hidden' name='formid' id='formid' value='6F45B3AF-91F3-108C-D3D9-701F541B49DC'>
<iframe type='application/pdf' src="url.pl?formid=6F45B3AF-91F3-108C-D3D9-701F541B49DC.pdf" height='800' width='1000' name='PDFObj' id='PDFObj'>
</td>
</tr>
</table>
</form>
I've tried embedding it using iframe and object along with setting input type="object", but I can't get any combination to work.
Is this even possible? Is there a better approach?
As far as I know, you're not going to be able to capture the PDF data directly from HTML like that. Your best bet is going to be to add submit functionality to the PDFs, then process the resulting FDF data with a server-side script.
You will need either add Submit a Form buttons to your PDFs, or modify the existing buttons. Make sure the form action in the PDF has #FDF after the URI (eg https://example.com/process.php#FDF).
Parsing the data server side is simple. I'm not sure what server side language you are using, but here is a PHP snippet
<?php // process.php, report the data we received
echo '<h2>GET Data</h2>';
foreach( $_GET as $key => $value ) {
echo '<p>Key: '.$key.', Value: '.$value.'</p>';
}
echo '<h2>POST Data</h2>';
foreach( $_POST as $key => $value ) {
echo '<p>Key: '.$key.', Value: '.$value.'</p>';
}
Note that a PDF only interacts with a web server properly when viewed inside of a web browser.
I do not know of a reliable way to programmatically add submit buttons to PDFs, nor do I know of a reliable conversion method. You're between a rock and a hard place here IMHO.
More info:
http://www.w3.org/TR/WCAG20-TECHS/PDF15.html
http://etutorials.org/Linux+systems/pdf+hacks/Chapter+6.+Dynamic+PDF/Hack+74+Collect+Data+with+Online+PDF+Forms/

Get an image in php using the <img src="" tag

<html>
<form action="image_test.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody>
<tr>
<td><img src="digital.jpg" name="image1"></td>
</tr>
<tr>
<td><input name="Upload Now" type="submit" value="Upload Image"></td>
</tr>
</tbody>
</table>
</form>
<html>
<?php
header('Content-type: image/jpg');
print "<pre>".print_r($_FILES,1)."</pre>";
$file=$_FILES['image1']['tmp_name'];
?>
How do I retrieve the image in php to then upload it later. This is important as I am getting the image from my webcam
You can draw captured image from webcam into canvas and then send canvas image data to PHP server via ajax by method getImageData.
I could not understand what you are doing here , first you create an html form ok then you start a session and sending Content type header, that is complelety stupid, header must be send first, other wise you can face "Header all ready send Error", then the main thing is that you set the content type to image/jpg but seding html data, :o ?
I think what you want is :
you want that tag image on server, but it seems that file is already on server ? O.o ,, or it may be any other server ?
If it is one the same server then you can do like this :-
<td>
<img src="digital.jpg" name="image1">
<input type="hidden" name="file_location" value="digital.jpg">
</td>
Then you can check on server like this :-
if(isset($_GET['file_location'])){
if(file_exists($_GET['file_location'])){
// process on image
// if image is remote, then you can use file_get_contents('') { remote must be allowed in php.ini }
} // exists
} //
And Last line :- It is completely stupid Code and you must find other way to solve your problem :) ,, Thanks

Creating unique URLs/Pages from MySQL DB on HTML form submit

The forums have been a huge help on this project so far. I'm looking for some guidance on the next step of my project here.
What I have is a form that feeds user submitted information into a MySQL database. This database then feeds this information to a main page displaying all of the information in the DB. What I am looking to do is add something to my form that creates a new unique URL/page when the form is submitted. I have already designed the HTML/CSS template for this page and it is designed to display only one set of information as opposed to the entire DB worth.
I am looking for some guidance as to how I can create the pages and unique URLs on the form submit. What is the best way to get this fresh information feeding from the DB immediately?
I need to somehow automatically recreate the HTML and CSS files as well on the server, this I am unfamiliar with.
EDIT: After #Jacky Cheng pointed out that this was possible without creating new versions of the HTML/CSS files I would be inclined to go about having a single HTML file on the server that is dynamic.
Thanks for any help as you guys have been great so far.
Including code for the form which I am submitting to the DB from, and the page which I will be pulling info from.
This is the form:
<?php
include_once 'post_func.inc.php';
connect();
?>
<!DOCTYPE html>
<html>
<head>
<title>Event Register</title>
</head>
<body>
<div style="text-align: center">
<h2>Event Register</h2>
<form id="eventregister"action="eventtestconnect.php" method="post">
<table style="border: 0; margin-left: auto; margin-right: auto; text-align: left">
<tr>
<td>Event Name:</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Event Type:</td>
<td>
<select name="eventtype">
<?php query_eventtype() ?>
</select>
</td>
</tr>
<tr>
<tr>
<td>Venue:</td>
<td>
<select name="venue">
<?php query_venue() ?>
</select>
</td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</div>
</body>
<?php close() ?>
</html>
This is the page I want filling with information from the DB after the form is submitted and the url is generated.
<?php
include_once 'event_func.inc.php';
connect();
?>
<html>
<head>
<title>
<?php query_eventname() ?>
</title>
<link href="eventstest.css" rel="stylesheet" type="text/css"/>
</head>
<body id="body">
<div id="maincontainer">
<div id="header">
</div>
<div id="content">
<div id="eventname">
<?php query_eventname() ?>
</div>
<div id="eventvenue">
<?php query_eventvenue() ?>
</div>
<div id="eventicon">
<?php query_eventtype() ?>
</div>
</div>
</div>
</body>
<?php close() ?>
</html>
What changes need to be made to the form in order for the url to be generated on submit and the event page to be able to jump between urls/sets of data dynamically, per-say?
Sorry for the beginner questions but this site really seems to be the best resource for these sorts of things and I haven't found anything this specific on here!
Thanks again for the help!
I am still half guessing what you want, so bear with me here.
from the description of your question, you seems to have a system that would generate an actual html file per form submit? That doesn't look good to me.
maybe try something like this :
redesign a web page that would take http GET request parameter as input (mydomain.com/display.php?id={input1}) and display only 1 set of info.
from the comments I see you have a unique id per form submit, I'd suggest avoid using it directly in the request as it'll be extremly easy to get someone else's info. Instead try somthing like MD5 encoding for that id and then sending that out to user.
so the overall system would be:
1) you'll only ever have 1 html file in your server, which will dynamically change it's content according to input, which save you a lot of space.
2) you'll have a unique & slightly more secure URL per form submit
edit:
here are some fake code to show the general idea.
form response:
$uniqueId=mysql_query("SELECT unique_id FROM my_db");
echo "http://yourdomain.com/display.php?urlid=".$uniqueId;
display.php
<?php
$uniqueId=$_GET['urlid'];
mysql_query("SELECT info_you_need FROM your_tables WHERE unique_id = $uniqueId");
?>
<html><body>your display page html here</body></html>
I guess...
You want to create product catalog page like this:
www.abc.com/Electronics/Product-Motorola-moto-g-at-Rs6999-only.html
and this will display all the product information from the database.
If the above is your case then you can use url rewrite in your project.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^Product-/?$ Product-Motorola-moto-g-at-Rs6999-only.html [NC,L] # Handle requests for "Product-"
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single RewriteRule.
^/Product/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.
Product-Motorola-moto-g-at-Rs6999-only.html - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.
Handle requests for "Product" - Comment explaining what the rule does (optional but recommended)
Hope this will work for you.
Feel free to ask any help.
Happy programming :)
Bear with me too. Your description is pretty bad. So if I am correct, you want form=>mysql=>confirmation
So, form should be action="process.php" method="post"
Create a process.php file where you do your validation, escaping, serializing, etc. Insert into the MySQL table. If returns true redirect (header(location:yourdomain.com)) and then on the redirected page, select the information from the Database.

Categories