Jump to content
xisto Community
Sign in to follow this  
ChronicLoser

Quick/simple Question Bout Php hopefully ill get a quick answer...

Recommended Posts

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... =/

Share this post


Link to post
Share on other sites

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.

 

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.

 

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

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.

 

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

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...

 

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

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:

 

.....// 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 );

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 :P

Share this post


Link to post
Share on other sites

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

$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!

Share this post


Link to post
Share on other sites

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.

 

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

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 :P

Share this post


Link to post
Share on other sites

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.

$filename = uniqid("Report");$filename .=".txt";
would return something like this:
Report1d2rt35d78h14.txt

Happy coding, :)
vujsa

Share this post


Link to post
Share on other sites

uniqid() is based on microseconds as well but returns an alphnumeric value.

$filename = uniqid("Report");$filename .=".txt";
would return something like this:

Report1d2rt35d78h14.txt

 

Happy coding,  :)

vujsa

<{POST_SNAPBACK}>


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" :)

Share this post


Link to post
Share on other sites

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 ^_^

Share this post


Link to post
Share on other sites

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! :) vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.