PHP - Readfile() Without Executing Html Code
In a PHP file [php_one.php], I wrote some PHP code that contained a form. I used ECHO to write the HTML inside of the PHP tags. On another PHP page [php_two.php], I wanted to show the code that was used to create the first page [php_one.php]. My first effort provided an interesting result, since the PHP code was readable. However, all HTML code was visible in its executed form. There was an actual input field and a submit button (not the code used to produce them) in the context of my coding. I have tried using:
file_get_contents("FILE") All results were similar and carried the same problem. How can I view code so that even the HTML is visible as code? Similar TutorialsHi all, Yesterday I transferred my hosting to a new provider. Everything is up and running apart from the following message I get on my index page Warning: readfile() [function.readfile]: Filename cannot be empty in /home/XXXX/public_html/imgsize.php on line 64 . This error is only on the index page. When I look at line 64 it states the following: readfile ($img); I have tried replacing the imgsize.php file but that still does not work. And on my old hosting this error does not occur. If you could please help, I would appericate it. Cheers Steve I'm trying to use the readfile() function to read a html page which has a video embedded (.swf file). When I call the function, it seems like it just loads the html file but without the dependent files (css,js, etc) When I call it directly it loads just fine. Created a hyperlink 'openvideo.php?id=video' that calls this function below: if($_REQUEST['id'] == 'video') { readfile('/usr/home/site.com/htdocs/content/videofolder/video.html'); } else { echo 'nada'; } Any help is appreciated. I am trying to use some php code inside of an html document and it does not work. I have a script.php file that executes perfectly when I try and use it. Here is the code I am currently using: Code: [Select] <?php echo "<title>"; echo "the Homepage!"; echo "</title>"; ?> Any suggestions would be great. I am playing around with php but the script doesnt execute to the last point and with no error...
What i am trying to do is checking the input of the user and comparing it what is in db and then applying conditions. So i have 2 files. update.php and home.php. home.php is my html form page and i included 'update.php' in it In my update.php, i have these codes
<?php error_reporting(E_ALL); include_once('database.php'); if(isset($_POST['submit'])) { $q = $_GET['q']; $next = $q + 1; if($q == 26) { echo '<h2>You have Completed the Exam.Congrats!!</h2>'; } elseif($q <= 25){ $sql = "SELECT * FROM Students WHERE Email=?"; $stmt = $conn->prepare($sql); $stmt->bind_param('s', $_SESSION['login']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $Mark = $row['Mark']; $Correct = $Mark + 1; $Fail = $Mark + 0; $sqlb = "SELECT * FROM Answers WHERE qid = ?"; $stmtb = $conn->prepare($sqlb); $stmtb->bind_param('s', $q); $stmtb->execute(); $resultb = $stmtb->get_result(); $rowb = $resultb->fetch_assoc(); $Answer = $rowb['Answer']; $Pick = $_POST['ans']; if($Pick == $Answer) { $sqld = "UPDATE Students SET Mark=? WHERE Email = ?"; $stmtd = $conn->prepare($sqld); $stmtd->bind_param('ss', $Correct,$_SESSION['login']); $stmtd->execute(); $resultd = $stmtd->get_result(); echo "correct" ; } } else { echo 'Ate'; echo "<script type='text/javascript'>document.location.href='home.php?q='.$next.';</script>"; } }//post submit ?> The else statement doesnt run. so it keeps reloading the same page. Also i want to ask why i keep getting the value of 2 for my $Correct variable stored on the database into column Mark for a single correct question instead of 1. What could be wrong? Lastly, i dont want the script to run if the browser is edited by the user by changing the value of $q from the browser, because i am using get method, is there a way to do that. Please be nice with your comments. Thanks in advance.!!! Hi, this is my first time posting here. I am just delving into PHP and I am learning about foreach loops. I have written code in Notepad++ EXACTLY the way I saw it in a tutorial video I watched (I wish I could show the tutorial video to you, but it is on Lynda.com and you have to pay to watch) I attached the file with my code. The example 1 code works just fine. The example 2 code is the one that is not working for some reason. However, it worked for the guy that wrote it in the video, so I am not sure where I am going wrong? *The comments in green are mainly for myself, I explain things to myself so that I don't forget what the code does forloops.php 1.74KB 2 downloads I would appreciate some help. Thank you!!! Hi All Basically I have 4 fields in my database image2, image3, image4, image5 I have an image uploader to upload the files but I need what I need is if image2 is empty put in image3, if image3 is empty put in image4 so far I have this //so first is the query to find the right row which gives the variable $row //now here is what im doing if($row['image2']!==""){ //run this code } elseif($row['image3']!==""){ //run this code } elseif($row['image4']!==""){ //run this code } elseif($row['image5']!==""){ //run this code } Now the problem I'm having is the code that is always executing is the first one of $row['image2'] even if image2 has a value in the database it still runs that code and over writes the field in the database. It never moves on to field 3 or 4. Can someone please tell me if I've done something wrong. I'm not storing the image in the database just the name. Thanks Danny Hey, having some troblems using the readfile function.. I have the following code. I want to download the file "blabla.gif", but the file is in a certain directory (let's say: "/ExampleFile/Files/" . How do I set the path to "/ExampleFile/Files/" ? $file ='blabla.gif'; 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; } Thanks, Ruben Hi guys Im new here. Ive got a small problem. I have a simple uploading script, had problems with that too but its working fine now to a certain extent. When I set a download link for the files that have been uploaded and click to download, it downloads but I get the following errors when I open the file, its a .txt file. But it seems to happen on all files also, it allows me to download .png, .php, .jpg bout it wont actually open them once they are downloaded (Ive change the domain to an example, its not my actual domain within the warnings below): The following warnings occurred: Warning [2] filesize() [function.filesize]: stat failed for http://mydomain/expiry_files/downloads/text.txt" - Line: 69 - File: expiry_download.php PHP 5.2.17 (Linux) Warning [2] readfile(http://mydomain/expiry_files/downloads/text.txt") [function.readfile]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found - Line: 70 - File: expiry_download.php PHP 5.2.17 (Linux) Here my download file, most of it (Ive also attached an image of the text file after I downloaded it and opened it, below): if (isset($_GET['id'])){ $file_id = (int)$_GET['id']; $file = $db->query("SELECT * FROM ".TABLE_PREFIX."expiredownloads WHERE `id` = {$file_id}"); $row = $db->fetch_array($file); if ($row['expire_end'] < time()){ //// Do nothing Yet }else{ $link = ''.$mybb->settings['bburl'].'/expiry_files/downloads/'.$row['expire_file'].'"'; $filename = $row['expire_file']; header('Content-type: application/octetstream'); header('Content-type: application/octet-stream'); header('Content-Description: File Transfer'); header("Content-Disposition: attachment; filename=\"$filename\""); header('Content-Length:'.filesize($link)); readfile($link); } } Hope you guys can spot a problem, If you require anymore information, just shout. Thanks in advance. So to stop third-party traffic on certain media files my site does, I did a download.php type script that just took in the media id, and found the filename and did a readfile on it for the user. The problem is that when you start doing massive amounts of traffic with it, I think the readfile stream is taking up a lot of cpu. Is there any alternative to readfile that creates a direct link between the user and the file, instead of it having to go through the php? I'm afraid that this is something I need to do on the server end, and not in PHP, but I figured I'd ask. i'm trying to import my print-on-demand shop without using frames. pg i'm importing - http://byuhonorcode.spreadshirt.com/ my site - http://byuhonorcode.com/index2.php Code: [Select] <?php $pg=file_get_contents('http://byuhonorcode.spreadshirt.com/'); echo $pg; ?> the page appears, but the these features no longer function: - switchColor javascript changes shirt color onClick of swatch (located @ http://cache.spreadshirt.com/Public/Shop/Js-packaged/shop_de23789016327809b00d204c6add79ba.js) - the arrows that change the color swatches - the size option box any ideas? Thanks! I have an FLV player and I am using a php file to read an FLV that is above webroot. It reads the FLV just fine, however, during its cache process or while it is still downloading, if I click on the home button (home button of the current site), the server does not respond until the FLV has been fully cached. Now this is only occuring in firefox, chrome and IE. In safari it works fine. Who know, maybe it is a header issue. Here is my script that I pass to my FLV player <?php include("connect.php"); if ($_SESSION['loggedIn'] == true) { $id = $_GET['id']; $query = mysql_query("SELECT video FROM reviews WHERE id = '$id'"); $row = mysql_fetch_array($query); $filename = "../../../armsmedia/videos/".$row['video']; header( 'Content-Description: File Transfer' ); header( 'Content-Type: application/octet-stream' ); header( 'Content-Disposition: attachment; filename='.basename( $filename ) ); 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( $filename ) ); ob_clean(); flush(); readfile( $filename ); exit; } ?>
An image is saved in the root directory. $file = '../image.jpg'; // IN ROOT DIRECTORY $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($file)); readfile($file);
The image then gets embedded into an html page and displayed like this:
QUESTION: in the event of an error, how do I dipslay the readfile error on the customer's html page? Make sense? No. I probably have to rewrite my question 😃 Edited Monday at 01:07 AM by ChenXiuI wrote a small application to force downloads of various large video files using readfile. This worked fine. However, the users have changed hosts and now it doesn't work. Small files are fine, but large files will cut off before finishing. Test file is 114MB, but it is always cut off at exactly 67.2MB or 51.4MB depending on a couple criteria. I've stripped the application down to the key part that's not working and run it on a test page the just immediately goes the file - no logins or any of that shenanigans and htaccess blocking either: $file = some/file/on/the/server.wmv; //114MB hea der('Content-Description: File Transfer'); hea der("Content-Type: application/octet-stream"); hea der("Content-Disposition: attachment; filename=" .basename($file)); hea der("Content-Transfer-Encoding: binary"); hea der('Expires: 0'); hea der('Cache-Control: must-revalidate, post-check=0, pre-check=0'); hea der('Pragma: public'); hea der('Accept-Ranges: bytes'); hea der("Content-Length: ".filesize($file)); ob_clean(); flush(); @readfile($file); (gaps in the header words are to prevent forum breakage) I've tried the following variations: transfer encoding - chunked (this results in getting a 51.4 MB download rather than 67.2) content-type application/force-download tried the default force-download code from the php.net manual tried the chunking function variations on php.net readfile page tried without accept ranges originally (only spotted that on this site) set_time_limt to one hour set max memory to 300MB (phpinfo has shown these changes did get accepted) used htacess to disable gzip and deflate (this results in getting a 51.4 MB download rather than 67.2) (SetEnv no-gzip dont-vary or RewriteEngine On RewriteRule . - [E=no-gzip:1] or RemoveOutputFilter DEFLATE html txt xml css js php wmv) None of these have solved the issue. The code worked fine on the old site, works fine on my site, and works fine on my test server. Direct downloads from the broken site also work fine, just not readfile downloads. The filesize is reading correctly if I echo just that, and it reads properly on the progress bar when downloading through readfile. The hosting company are blaming my coding. The htaccess is the bit I'm least sure of and I find it hard to get solid info on this part on the web. Is there anything wrong with my code that could be causing this? Am I missing something? What could be causing the problem? Many Thanks Which is better? Why and why not? An image is secured in the root directory, to be displayed to customer in html document.
This image can be displayed as a link to a php readfile page Thank you. An image is secured in the root directory, to be displayed to customer in html document.
This image can be displayed as a link to a php readfile page Which is better? Why and why not? Thank you. Edited yesterday at 01:15 PM by ChenXiuI am wanting HTML to render within a table and sourcing the data from another file. In an attempt to do this, I tried: Code: [Select] <TABLE class="main"> <TR> <TD> <?php readfile('./1/index.html'); ?> </TD> </TR> </TABLE> This does not work and I am wondering if anyone knows how it can be done - or even if it can be done? I have the following code in html: <html> <head> <script type="text/javascript"> <!-- function delayer(){ window.location = "http://VARIABLEVALUE.mysite.com" } //--> </script> <title>Redirecting ...</title> </head> <body onLoad="setTimeout('delayer()', 1000)"> <script type="text/javascript"> var sc_project=71304545; var sc_invisible=1; var sc_security="9c433fretre"; </script> <script type="text/javascript" src="http://www.statcounter.com/counter/counter.js"></script><noscript> <div class="statcounter"><a title="vBulletin statistics" href="http://statcounter.com/vbulletin/" target="_blank"><img class="statcounter" src="http://c.statcounter.com/71304545/0/9c433fretre/1/" alt="vBulletin statistics" ></a></div></noscript> </body> </html> Is a basic html webpage with a timer redirect script and a stascounter code. I know a bit about html and javascript, but almost nothing about php. My question is: How a can convert this html code into a php file, in order to send a variable value using GET Method and display this variable value inside the javascript code where says VARIABLEVALUE. Thanks in adavance for your help. I have a form that users input user name, password, and email... all writes to csv fine. My problem is I need to concatenate a a string with a variable and some html code will preserving the html when written to the csv.. I need the csv to be this username,password,email,user,category,text with http://sub.domain..com/splash/,something,something here's what i have Code: [Select] <?php if($_POST['formSubmit'] == "Submit") $varUserName = $_POST['username']; $varPW = $_POST['PW']; $varEmail = $_POST['email']; { $fs = fopen("testcsv.csv","a"); fputcsv($fs, array($varUserName,$varPW,$varEmail,"user","title",",category","some text '<a href="http://$varUserName.url.com/splash/>site.com</a>',)); fclose($fs); exit; } ?> and of course I'm getting Parse error: syntax error, unexpected T_STRING, expecting ')' on line 9 Can I combine also HTML code in PHP function? For example, can a PHP function include HTML form and the PHP code to handle this form? If yes, this will make my main code much more smaller and readable. If not, is there a way to define an "external macro" like, which allow me to replace pre-defined lines of code with short alias?
Circumstance:
Question:
This works by having the actual image name stored in a hidden session variable, using the following lines of code on my readfile page "label.php" like this: Since I do not want customers to be allowed to ever see the image again after logging out, is it okay to rely on a hidden session variable for the filename as I described above? (Potential hackers will be less tempted to "try different image names and numbers" like "id1.png, id2.png, id3.png" because they will have no idea if the image will be a gif, jpeg, or png, nor have any idea of a numbering sequence.) Thank you!!
|