Quick/simple Question Bout Php - hopefully i'll get a quick answer...

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Quick/simple Question Bout Php - hopefully i'll get a quick answer...

ChronicLoser
Alright, i just need to know how I would create a new text file with php. When the php program is activated for whatever reason, it will create a new text file of a different name in the same folder. Not sure if i'm making this clear enough...I want a code in php that will create a new seperate text file in the same folder the original code is hosted in.

And don't give me something that requires a database...i'm pretty sure there is a simple function that will allow me to do this. I just don't know what it is... =/

Reply

miCRoSCoPiC^eaRthLinG
    [/tab]Here's the solution. Since you want DIFFERENT file to be created when the script is executed, we have to depend on some very random non-repeating figure to generate the filenames so that they aren't repeated and there' no chance of name collission and files being overwritten. We can use the microtime() function in php which gives us time resolution finer than a second.

[tab]microtime() returns a string that contains the microseconds part of the elapsed time since the epoch, a space and seconds since the epoch. For example, a return value of 0.12345678 1234567890 would mean that 1234567890.12345678 seconds have elapsed since the epoch. Here a string is returned, because the double type doesn't have enough capacity to hold the entire value and yet maintain the microsecond precision that is required.

CODE

list ( $microsecs, $secs ) = explode ( ' ', microtime () );



    [/tab]This will call the microtime() routine and fetch the seconds elapsed since the epoch. The explode function simply splits the returned value into microsecs and seconds based on the space (' ') in between and stores them into the variables $microsecs & $secs with help of the list function (which can assign individual values to the variables provided as its parameter from an array). Once we have the values, we can proceed to defining a unique filename. Lets say, we take the name "Report and append the microseconds part to it - that'd give us a fairly unique filename everytime you run the script. The chances of hitting upon the same filename is about 1/99999999 = 0.00000001 i.e., everytime you access microtime(), you have a chance of 1 in 10 million to arrive upon the same filename - I think you can afford to be pretty happy with the results. Right, lets forge our file name and store it in another variable called $filename.

CODE

$filename = 'Report' . $microsecs . '.txt';



[tab]This just appends your microseconds and ".txt" to the string "Report" giving you a filename like, Report12345678.txt. Next we go about writing that file out...

CODE

$fh = fopen ( $filename, 'w' ) or die ( "Cannot open $filename for writing. "  . $php_errormsg );
.....
// Code to write data to file
.....
fclose ( $fh );



    [/tab]That's it - that'll write out whatever you want into this random file and close it - and since we didn't specify any directory for the file to be created in, it happens in the same directory that the script is run from...Now if we put all this code together:

CODE

.....
// Other parts of your script
.....
.......
list ( $microsecs, $secs ) = explode ( ' ', microtime () );
$filename = 'Report' . $microsecs . '.txt';
$fh = fopen ( $filename, 'w' ) or die ( "Cannot open $filename for writing. " . $php_errormsg );
.....
// Code to write data to file
.....
fclose ( $fh );



[tab]This should do - I've checked the code and it worked fine on my system. Try it and let me know if it helped..

All the best smile.gif

 

 

 


Reply

ChronicLoser
heh, i was actually looking for a shorter and more "straight-forward" answer. But thanks for the microtime part, but it will be kinda unneeded. I have other plans for the title =P

CODE
$filename = $title . '.txt';
$fh = fopen ( $filename, 'w' ) or die ( "Cannot open $filename for writing. " );

So, you're saying if I had something like this, it will create a new file in the same directory?

Alright, I looked over what you gave me and this isn't exactly what I want. Say I have an empty folder with nothing but index.php. In the index.php, it will create a brand new text file. All I want to know is that one line that will create that $title.txt or $title.dat or $title.php. Perhaps I am using your code wrong, but it seems to be that what you gave me simply opens an already made file and write into it.

But thanks anyways, microscopic^earthling! It looks like you put a lot of time going through all that stuff for me...now I kinda feel bad =P



- EDIT -
Shoot, nevermind. By setting it to 'w' it's supposed to write a new file on its own when the server can't find it. I've got the problem fixed. Thanks microscopic^earthling!

Reply

miCRoSCoPiC^eaRthLinG
    [/tab]Naah no probs - I was learning how to use microtime myself - you always learn much better if you try explaining what you've picked up so far - that way you rethink the whole topic from a different perspective and enables you to capture all the nuances attached to it - which would elude you under normal circumstances.

QUOTE
So, you're saying if I had something like this, it will create a new file in the same directory?


[tab]It should, because microtime always returns a different value (far far different from the one when you run it the first time, and so on, every subsequent time) - so if you append that string of numbers to the name of your file (as I'd done - Report12345....txt) you'd get completely random numbered files everytime you run the script. I just tested out this one and it worked absolutely fine for me...I put the whole thing into an index.php and everytime I loaded it a new file (like Report12345.txt, Report67123.txt etc... ) got created in my dir...

    Glad to know you got your prob solved - but keep this routine in mind though (the microtime part especially)... It comes to real good use, when you want to generate completely random html files that act as email activation links - as most of the sites do to verify your email address' authenticity. I'm try to implement that on my site now. Will post the code once I'm successful smile.gif


Reply

vujsa
Hey M^E,
Nice suggestion for creating unique file names, I used to use a sepperate count file that I incremented after each new file name. PHP's uniqid() function does the same thing that you described above, but with fewer steps.

uniqid() is based on microseconds as well but returns an alphnumeric value.
CODE
$filename = uniqid("Report");
$filename .=".txt";

would return something like this:
Report1d2rt35d78h14.txt

Happy coding, mellow.gif
vujsa

Reply

miCRoSCoPiC^eaRthLinG
QUOTE(vujsa @ Feb 24 2005, 07:18 PM)


uniqid() is based on microseconds as well but returns an alphnumeric value.
CODE
$filename = uniqid("Report");
$filename .=".txt";

would return something like this:
Report1d2rt35d78h14.txt

Happy coding,  mellow.gif
vujsa
*



Thanks for the tip - I didn't know that one. I was going for a much longer process of my own..lol.. achieved kind of the same effect though.. but this is much simpler and as they say, "short & sweet" wink.gif

Reply

ChronicLoser
lol, now there's a function of php I didn't know! lol, i guess you learn something new everyday =P thanks, both of you, for the info. I'm sure it'll go to good use ^_^


[edit] oops, sorry bout that...i edited my post ^_^

Reply

vujsa
Wow, I figured that that would be a fairly common function. Glad I could have pass along the knowledge.

The code generated is actually two hex strings and a digit. (13 places)

Guess that's why we're here! laugh.gif

vujsa

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : bout php ll


    Looking for quick, simple, question, bout, php, ill, quick, answer






*SIMILAR VIDEOS*
Searching Video's for quick, simple, question, bout, php, ill, quick, answer
advertisement




Quick/simple Question Bout Php - hopefully i'll get a quick answer...



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE