Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

displaying files

Recommended Posts

How do I get a .txt, .doc, .pdf file display as regular text in a webpage.
I wanted to change them all over
If pdf and doc aren't possible without using a pdf file then I just want to display txt files, any advice, using php.I know how ot access the file and url I just have to know what functions to use to get it to display
I tried
fopen
and file_get_contents
but neither of them did what I wanted.

Share this post


Link to post
Share on other sites
as far as i know, there are two things involved here.

one is how your web server decides to serve those files.
the other is how your browser is configured to accept the files.


your web server can  be configured to send a .txt file as plaint/text and if your browser is configured to download a file with a MIME type of plain/text then that's what it will do. by the way, you can also configure your web server to serve a .txt file as audio/mp3 but it won't work as you expect...just wanted to let you know its possible.


is this what you wanted to know? i think i might have misunderstood your question.

Share this post


Link to post
Share on other sites
I have written a script finally to handle my files, it's a display file, and I made modifications to php ht access
What I am needing to ask, is I am trying to get the contents of the file via the url, http://www.funnyemailforwards.com/apex/uploads/file.ext
in this case .txt, and .doc and .pdf
But I am just wanting to pull all the text out of those files, and throw it onto the screen.  In one giant p tag
I was hoping to do<
<p>
Work with the file and make it display
</p>
But I don't know what functions to use.

Share this post


Link to post
Share on other sites
care to show me the file_get_contents() snippet you used? That should work for you. It's better that you use htmlspecialchars() before you stick the data in a <p> tag though.

i don't know why you want to display the raw data of a .pdf or a .doc file...you do realize that all the text will seem garbled, right?

Share this post


Link to post
Share on other sites
I don't know much about tihis yet
I just got past the difficulty of trying to get my barebones script set up this is what I tried
get_file_contents($urloffile);
it didn't work
I also tried
$file = fopen($urloffile);
echo "$file";

Share this post


Link to post
Share on other sites
ok thanks.

but please define "it didn't work"
what exactly hapenned?

can you reproduce the error message you recieved?

Share this post


Link to post
Share on other sites
Nothing white screen.

Here is what I have if it helps, but I will be reposting it later in another thread, I was going to keep one massive thread later for help, but here is some of the code.
This is the page that you come to to handle viewing files, and htaccess is fixed to make this work properly.
[code]<?php
# getLoc() grabs the current URL and parses the fake folders into variables.
function getLoc(){
$dirname = split("/", getenv("REQUEST_URI"));
foreach ($dirname as $var) {
if (($var!="BidRent")&&($var!="display")&&($var!="")){
$p[] = $var;
}
}
return $p;
}
# They are called and set here.
$pages = getLoc();
$folder = trim($pages[0]);
$pagename = trim($pages[1]);
$site = "http://www.funnyemailforwards.com/apex/";

$pageTitle = "Page For $page1";
mysql_connect("localhost", "####", "####")or die(mysql_error());
mysql_select_db("funnyemailforwards")or die(mysql_error());

$select = "SELECT * FROM fileinfo WHERE nameoffunny = '$pagename'";
$query = mysql_query($select);
$count = mysql_num_rows($query);
$fetch = mysql_fetch_assoc($query);
if ($count > 0){
if(extract($fetch)) {
$successful = true;
}else
$successful = false;
}else{
echo "The file Does not Exist";
}

/*
Ok so now that you have the basic environment set up, you want to take $page1 and search the database for the
URINAME (which you need to insert upon data submit, URL friendly -- They need to be unique!)
preg_match("/[.doc|.txt|.wod]{4}/i",$string)
Once you find one, grab its ID and table its under (if the types of data are in different tables).
Once these global variables are set you will include premade files that handle each type of data, think template
that will auto populate the page in specific areas with that datas information from the database.
(by global variables i mean inside those includes will all use the same variables set here).

So once you figure out what type it is, grabbed its id etc the page continues to load, maybe grab the title while you
check and the type to build $pageTitle -- since this is a gateway you need to do as such.

so lets say this is a picture under aaron. $area = "picture"; $uriname = "aaron"; grab the ID or just use the unique
uriname and grab its information. the file will parse it as a picture with its info.

You may notice commented includes for header and footer, i dont know how your doing it but i usually cut the top
and bottom of my sites off and stick them in their own files, and include on main files, this allows me to not
have to edit a hundred files if i just need to change 1 thing at the top of the site.
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo "{$nameoffunny}" ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<link href="style.css" rel="stylesheet" type="text/css" />-->
</head>
<body>
<?php
$funnyurl = $site . $funnyurl;
if (preg_match("/[.txt]{4}/i", $funnyurl)){
require_once("./includes/view_text.php");
}
?>
</body>
</html>[/code]

Below here is the code for viewing the txt files.
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo "{$nameoffunny}" ?></title>
</head>
<?php
echo "{$funnyurl}";
$open = fopen($funnyurl, "r");
echo "{$open}";
?>
<body>
</body>
</html>[/code]
$funnyurl ends up being the full file path.
The current error messages are nothing, it just doesn't do anything.
When I try to echo it like that
I get Resource Id #5 or something
here is my db info
id   type  nameoffunny  keywords  funnyurl  entrydate  approval 
     
Those are my columns in my table fileinfo
The funnyurl for this specific file is
/uploads/dickhead.txt
then the url is
http://www.funnyemailforwards.com/apex/
when I add the strings together I get the url with whatever the file name is

Share this post


Link to post
Share on other sites
you mentioned using file_get_contents()
do you have the code for that?
make sure your URL is ok. echo the URL and check if it's valid

Share this post


Link to post
Share on other sites
http://www.funnyemailforwards.com/apex/uploads/websitereport.txt
That is the url exactly, and when I tripled check it's exactly the samething, it will change with each file, but it will still always point to the right file.
The stuff I used with getfilecontents was the following code
[code]
$file = file_get_contents($funnyurl);
echo "$file";
[/code]
That is what I used
I must have done it wrong because now it does something but nothing like what I expected.
http://www.funnyemailforwards.com/file/dickhead
for one the report had 2 times as much in it as that, it seems, adn another it looks like crap, I thought it would print it out like it was in the txt file.

Share this post


Link to post
Share on other sites
[code]<?php
$file = file_get_contents($funnyurl);
$file = nl2br($file);
echo "$file";
?>[/code]
That worked
now how do I handle .pdf, or .doc files, or are those worthless, or overly complicated to get to work properly.

Share this post


Link to post
Share on other sites
PHP can create PDF's but i'm not aware of any way in which it can read and render it.

doc's are a no go.

Share this post


Link to post
Share on other sites
I finished my txt script but I can't get my images to access for some reason, I tried the following 2 codes on the appropriate page.
echo '<img src="{$funnyurl}" />';
and
echo "<img src='{$funnyurl}' />;
and
<img src="<?php echo '{$funnyurl}'>;
I cna't get any of them to work.

Share this post


Link to post
Share on other sites
Try:
[code]<?php
echo '<img src="' . $funnyurl . '" />';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
It didn't work for some reason, I know it works some, because I had another one access the txt file earlier, and it worked perfectly.  But now the image isn't showing up, I have it in another page, aside from my pages up there this is what I have in my view image file, if you want to see how they get here look in the first post.  I have one php file for each grouped types
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo "{$nameoffunny}" ?></title>
</head>
<body>
<?php
echo '<img src="' . $funnyurl . '" />';
?>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites
When you do a "show source" with the browser, what does the generated source look like?

Ken

Share this post


Link to post
Share on other sites
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>testpicture</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<link href="style.css" rel="stylesheet" type="text/css" />-->
</head>
<body>
</body>
</html>
[/code]
This is what I have so far, but that is with the code you gave me, I don't understand why it isn't showing up there.

Share this post


Link to post
Share on other sites
[code]<object classid="{$clsid}" type="{$type}" data="{$data}">[/code]
This is the code I have to insert my windows media player thing.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.