Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to display the file content, when a link to a file is stored in mysql DB?

Posted on 2005-04-22
10
Medium Priority
?
207 Views
Last Modified: 2008-07-03
Hi!
It might be a simple one:
I have mysql DB, and in one of the fields there's a link to a text file. File itself is sitting somewhere in the file system (instead of storing the text in the DB).
Now, when I fetch the data from DB, for exemple:

while ($word_data = mysql_fetch_array($rh))
{
 print '$word_data['text'];
}

It prints the link stored in the DB, no problem.
My question is how to OPEN the file? How to display the text instead of the link only?
Thanks!
0
Comment
Question by:athanasius296
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 300 total points
ID: 13845752
You want to show the content of the text file?

try

while ($word_data = mysql_fetch_array($rh))
{
  if(file_exists($word_data['text']))readfile($word_data['text']);
}

0
 
LVL 5

Assisted Solution

by:prsupriya
prsupriya earned 200 total points
ID: 13845795
You may also need to give the path of the gile in readfile(/path/to/filename)

S:
0
 
LVL 6

Assisted Solution

by:quad341
quad341 earned 100 total points
ID: 13847847
if you were looking to read the contents of the file in and possibly do something with it, you could change Batalf's code to the following:

while ($word_data = mysql_fetch_array($rh))
{
  if(file_exists($word_data['text'])){
    $fo=fopen($word_data['text'],'r');
    $FILE_CONTENTS=fread($fo,filesize($word_data['text']));
  }
}

you could also use the function file_get_contents($word_data['text']) if you have a late enough version of php.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:designbai
ID: 13848665
You must specify the full path to the file (from the root folder) to accomplish.

try quad341 said, if doesn't work and if you are getting any error message, let us know.

0
 

Author Comment

by:athanasius296
ID: 13872784
Hi!
Thanks for your solutions. I tried both, and they work great.
Just one more question: what function would I need if I want to use URL instead of a full path to the file (from the root folder)?

www.abc.com/somethinghere/filename
instead of
/path/to/filename

0
 
LVL 5

Expert Comment

by:prsupriya
ID: 13872829
you can use readfile('http://www.abc.com/somethinghere/filename').
0
 
LVL 3

Expert Comment

by:designbai
ID: 13873381
another way to do this which identical to readfile.

// Another example, let's get a web page into a string.
$getdata = implode ('', file ('http://www.example.com/'));

hope this helps.

0
 

Author Comment

by:athanasius296
ID: 13881739
>you can use readfile('http://www.abc.com/somethinghere/filename').

Well, readfile doesn't work for me with URL, only with /path/to/filename. I don't know why.
Anyway, do you know how to display a part of the file (let's say, first 50 words from the text) using readfile (or another function).
Thanks!
0
 
LVL 5

Assisted Solution

by:prsupriya
prsupriya earned 200 total points
ID: 13882014
try this...

$getdata = file_get_contents('http://www.abc.com/somethinghere/filename');
$getdata =  explode(" ",strip_tags($getdata));
for($i=0;$i<=50;$i++){
      echo $getdata[$i]."<bR>";
}
0
 
LVL 6

Expert Comment

by:quad341
ID: 13882079
for a bette4r explanation, readfile will read in the specified number of bytes of the file.  by using filesize(), we are telling it to read all of this, but you could change that if you wanted.

using explode(), you can create arrayx out of the data, though just simply a word count is probably not what you want.  you can explode on any string (ie, separate the main string into array entries based upon any string).  the code above gives you separate words when the words are separated by a space.  if you used "\n" as your string, it would break it apart on new lines.

it really depends on how you wish to split up the file, but that is your best ideas for doing this.  remember, explosde returns an array with indexes starting at 0 with all the parts that were between the separators.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question