Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-13-2008, 05:37 PM fopen help!
Super Talker

Posts: 129
Trades: 1
Hi guys,
I am in a bit of a problem. I want to know whether I can get the contents of a file from that current time and have it read in a file.

I have a simple txt file that at one given time only contains one number. So just one number in the file (that's it). I have a bunch of files that read from that one file to get the id number contained in it. But them files will only need to open that file once, get the number and keep it. Then when one file has got its number from the file, the next file will get its number from it, and so on.

This may sound confusing, so here's a little more...

The txt file is called id.txt
I have three files called file1.php, file2.php, file3.php

first file1.php will look in id.txt and get its number and save it. (same for others as well)

Now, you know I have three php files but only one txt, yet the number in the txt file will keep changing. If I used fread, all three php files will get the same number as they will open the txt file every time.

I want the php file to get the number from the txt file and keep it (assigned if you like). Then, it doesn't matter if the txt file changes as file1.php has its number.

I thought about file_get_contents but wasn't too sure. I would really appreciate the help.

Thanks.
__________________

Please login or register to view this content. Registration is FREE
yourmediaking is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-14-2008, 03:35 AM Re: fopen help!
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
What are you trying to do? Use database.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-14-2008, 06:29 AM Re: fopen help!
Super Spam Talker

Latest Blog Post:
PSD Squirrel Launched
Posts: 932
Trades: 7
Hello,

I don't understand what you are doing - I don't understand three php files... one txt file.. maybe tell us what the actual spec is in its entirity and maybe we can help more? Either way using file() or file_get_contents() will be better for getting your file number rather than fopen

cheers,
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Sir P is offline
Reply With Quote
View Public Profile Visit Sir P's homepage!
 
Old 10-14-2008, 08:01 AM Re: fopen help!
Super Talker

Posts: 129
Trades: 1
I do apologize. It was quite late last night when I wrote that. I'll try to clear things up.

Ok, I think if I try to explain everything, it will get too complicated.

I just want to know which function would be appropriate for the following scenario.

On 1st October there was a file called number.txt and in it there was the number 10. Another file called index.php needs that number (10). But if I used the "fread" function, index.php would open number.txt every time the script was executed.

index.php ONLY needs the number from 1st October. On the 2nd October, number.txt may change to 13. So as I mentioned, using fread would get the CURRENT value of number.txt, when index.php needs it from the 1st.

So is file_get_contents suitable to get the number from number.txt and save it in index.php? Then if tomorrow, number.txt changes, index.php won't care as it already has (10) saved in it.

I'm sorry for not being to explain it, but it's a little difficult.
__________________

Please login or register to view this content. Registration is FREE
yourmediaking is offline
Reply With Quote
View Public Profile
 
Old 10-14-2008, 08:25 AM Re: fopen help!
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
OMG

You try to solve the problem which does not exists. Tell us what you WANT to do, not HOW YOU want it to be done. I am 99% sure that you need a database and a single script.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-14-2008, 01:17 PM Re: fopen help!
Super Talker

Posts: 129
Trades: 1
Quote:
Originally Posted by mtishetsky View Post
OMG

You try to solve the problem which does not exists. Tell us what you WANT to do, not HOW YOU want it to be done. I am 99% sure that you need a database and a single script.
Ok, I have a form that creates a new folder and puts a file called index.php in it. So when the form is submitted, a new folder with a homepage for that folder is created.

Now, this index.php is copied from the templates folder. This page is 100% reliant on an id from a database. So all I need to do is write the id at the top of index.php so the rest of the page is taken from the database accurately. It's sort of how wordpress uses ?p=3 to display different posts. But I don't want to use query strings.

So back to the point, I need to put an id at the top of index.php. Rather than writing it manually, the original form will look into the database and get the id. But that form can't write the id itself to the index.php as you can't stop and start with fwrite. So instead, the form will open up a simple txt file and write the id into it.

Now, there is a txt file that contains an id in it. index.php needs an id. What is the best way for index.php to get the id from the txt file?
Bear in mind, there is only one txt file and every time the form is submitted, the id in the txt file will be replaced. So the txt file will keep changing, so fread won't work because fread opens the txt file every time, so it will just get the current id (not what I want).

Any ideas?
__________________

Please login or register to view this content. Registration is FREE
yourmediaking is offline
Reply With Quote
View Public Profile
 
Old 10-15-2008, 03:40 AM Re: fopen help!
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
You definitely need mod_rewrite.

You have e.g. /cats/index.php /dogs/index.php and /rats/index.php. All these index.php are quite identical, because you said you copy them from the template.

What you should do:
1. Setup the mod_rewrite to rewrite requests like /<dirname>/ to /index.php?dir=<dirname>
2. Design your index.php so that it processes $_GET['dir']
3. Store pairs of dir (string) and id (number) in database since you already use it.

If you are still wiling to get inside the house through the sewage instead of the front door you may replace direct copying of index.php from template with reading it into variable, replacing certain substring with required numeric ID and writing it to the target file.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to fopen help!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25998 seconds with 12 queries