Automatic download launch - javascript

Is there any other way to launch an automatic download. Currently we do it this way.
If download does not start click here:
Filename (121 MB)
And much later on in the page we have this code.
<script type="text/javascript">
window.onload=function(){
setTimeout(function(){
window.location.href='http://www.example.com/file.zip';},2000);
};
</script>
Problem is that it sometime launches the same file to download multiple times to end users. Is there a better way to do this?

One option is to have an IFrame hidden in page with src pointing to zip file to download.
<div style="display:none;">
<iframe id="frmDld" src="http://www.example.com/file.zip"></iframe>
</div>
Another option will be to write the file to response stream when sending the page back(Don't know what you are using on server side. If it is asp.net/mvc then I can help.).

If
Filename (121 MB)
works then this should work too:
<?php
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL= http://www.example.com/file.zip \">";
?>

Related

Having problems with setTimeout & onload

I am trying to check if a cookie exists, and if not, load a file with a 3 second delay.
I've checked and the php bit works ok, but the page doesn't load.
This is in the main index.php file and the popup.php is a modal newsletter sign up popup.
Any help much appreciated.
<div id="newsletter"></div>
<?php
if(!isset($_COOKIE['somecookie'])) {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
setTimeout(function(){
$("#newsletter").load("inserts/popup.php");
},3000);
});
</script>
<?php
} else {}
?>
Is the path where file placed is correct? Have you privileges to access that folder and file?
If you are on Linux, try
sudo chmod -R 777 *Your path goes here*

Issue with Iframe function

I have an iframe that contains an upload.php file in my page. So i want my page to refresh once when an upload in the iframe is complete. However, my page refreshes continuously whether or not i have clicked the upload button..Help please
Here is my code...
<script>
var up = document.getElementById("upload");
function iloaded(){
window.location.reload();
}
up.onload = iloaded();
</script>
and here is the iframe
<iframe style="display:none" id="upload" src="upload.php"></iframe>
the upload is successful though but the page refresh is the issue here..
I have got the answer simply add this code to the upload.php file and leave out all the other js functions.
Here is the code..
if(upload==true){?>
<script>
window.top.location.reload();
</script>
<?php}?>
Does the job.

No output to iframe after using window.location.href

I am using window.location.href in a php function which forces a file download without opening an additional window. The file download works and the rest of the php script executes. However, nothing that I output after the line with the javascript in the function will show up in the iframe. Is there a way to redirect the output back to the original php script?
Here is the javascript line from the php file:
echo "<script>window.location.href='download.php';</script>";
Here is the code from download.php:
<?php
session_start(); // Starts new or resumes existing session
$filename = $_SESSION['filename']; // Creates variable from session variable
header("Content-Type: text/plain"); // Output text file
header("Content-Disposition: attachment; filename=$filename"); // Output file name
readfile($filename); // Outputs the text file
?>
This is an example of something in the php file that will not output to the iframe after the line of javascript:
echo 'Test Output';
Thanks in advance,
Jay
EDIT
I replaced the line of javascript with the following and it works perfectly:
echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";
You don't need to put the redirect inside the iframe page.
The best way I can think of atm is to use an invisible frame as mentioned in Andrew Dunn's answer to this question
Quoting from that answer:
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
Have that inside your main page. url is basically the download url ("download.php" in your case). You can use a button to make the download manual. If you want it forced. Add src=url to the iframe and remove the script.
I would advice researching and using jQuery.

Is it possible to put a javascript function in a php file?

Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>

PHP file_get_contents() in setInterval() not working

I am trying to use get_file_contents() in a setInterval() to repeatedly update some text which is displaying the contents of a file. Here's the code:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
setInterval(function(){
$("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
console.log("<?php echo file_get_contents('file.txt'); ?>")
},500);
});
</script>
The problem is: it seems that the text is not updating until I reload the page.
the file_get_contents() is NOT set to reload by setInerval. You mixed up client-side JavaScript & server-side PHP.
You probably need the following:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('file.txt');
},500);
});
You have to create another php file to output the content and use ajax to get it.
Example:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('/echo_the_content.php');
},500);
});
Create echo_the_content.php with:
<?php
echo file_get_contents('file.txt');
If your file.txt is in public dir (web accessible), then you could load it directly without another php file.
the setInterval get executed in the client side where as the php is evaluated in the server side so the command echo file_get_contents('file.txt'); is evalulated only once.
You need to use an ajax request where it returns the contents of the file
$("#theDiv").load("file.txt");
where the server side resource file.txt returns the desired response that has to be displayed in the div

Categories

Resources