I want my PHP script to write a file to my directory. What this would do is write a file called hello.php, hello.txt, or hello.html and put it in a specific directory.
The reason is so that when a user fills out their name and age on a form, it would generate a .html file that would be a basic outline, including theirname and age from the form data.
Take a look at file_put_contents(). http://php.net/manual/en/function.file-put-contents.php
file_put_contents('hello.txt', 'Your name is user1544586.');
However, is there a reason you want to generate static HTML files? It is generally better to store data in a database, and then generate pages on the fly with PHP.
Related
I don't want to read file using tmp_name. Is there any other possible methods to read file from a folder with its original name that I stored in databases?
Also, I am trying to access contents of that file in second form.
All else being equal: No.
A file was uploaded and it was saved to a temporary place with a generated filename and your PHP program was told what filename the client said it should have. The two filenames are not the same.
You can't access it using its original file name because that isn't the file name it has on the computer running the PHP.
The script handling the upload could use move_uploaded_file to move the file to somewhere else and change its name so it is the same as the one the client told you it originally had. Then you would be able to use that name. You would, of course, have to be careful to avoid collisions. Two different files with the same name could be uploaded.
I have a system set up where, when the user registers, it creates a custom directory for them and then inside that directory it creates a file called index.html. I would like to write an entire HTML page of 100+ lines to that file. I was using fopen() fwrite($filepath, 'content');, but there are escape characters like '' and "" that mess up the PHP function.
Is there a better way to write large content to these files, or should I just have the file saved somewhere on my webserver and then just transfer it to each new registered user?
If you already have the file on disk, and it will not change for each user, then you should copy the file.
It is a waste of resources to read and then write the file with PHP..
PHP Manual - copy
Have a look at htmlentities($var);
http://php.net/manual/en/function.htmlentities.php
Well that means that where you say 'content' you have a hard coded html file in your script itself. It would be easier to just save a standard html file somewhere on the webserver and to something like this:
file_put_contents('/path/to/user/file', file_get_contents('/path/to/standard/index.html'));
Easier to maintain (no html in your code, but just in a testable html file) and no worries about escaping characters. Easy does it :)
Edit
Someone smarter than me proposed to just copy the file. Which is ofcourse a lot more sensible to do than reading and writing contents...
I am working on an application, where the user has to upload files in a cart.e.g. the user upload the file "A" and now doing different work. After some time he again upload another file, file "B". How I can manage the file path or store the file path, as if I use move_uploaded_file() function, then it can overwrite the other user's file with same file name.
Thanks.
When I've had this issue, I have used a timestamp added to the filename. Usually I want to cleanse the filename anyway, so I
replace characters I don't like
remove the file extension and check it looks OK (e.g. pdf not exe)
add a timestamp to the filename
put the extension back on
Obviously, this isn't suitable in every instance, but it might give you some ideas.
Create folder run time against session id, that way only current session user files goes to the folder.
temp_uploads/ (main uploads folder)
temp_uploads/_jhk43543h5h435k3453 (session id folder for user 1)
temp_uploads/_jhk43543h5h435k34tr (session id folder for user 2)
temp_uploads/_jhk43543h5h43trtrtg (session id folder for user 3)
you just need store session id for each user, which you maybe you are already doing.
happy Coding :)
You use php's time function to generate a timestamp that you append to the filename so that they can be different. Then you can use a column in the db to store the file paths. You could store all the file paths in the same column but separate each one with ; or any other character. To get the separate paths, you can use php's explode function.
my requirement is this :
" When users uploaded one file say "sample.tex" then i need to find the same name PDF file in that directory once he upload the "sample.tex". so file name should be "sample.pdf". we have one form that contain two input file fields.. check the image for reference.
http://img40.imageshack.us/i/proofbb.png/
once user upload the first file and click the "Show Author Email(s)" then i need to find another file "sample.pdf" in the same path and put in below file field. Is that possible in PHP or JQUERY or watever... not only PHP Even Java is also fine. Please help me to find the solution.
Regards
Dipen
You cannot do that with plain JavaScript. You can't even obtain the path information from your "first file"; all the browser will tell you is the plain file name (that is, the file name without any path information). You also cannot force a "file" input field to be set to a value.
You might be able to do this by creating a signed Java applet, but that's a whole different enchilada and you'd pretty much have to make the whole form be a Java thing.
(There's nothing you can do from PHP, as all the server will get is the plain filename and no path information at all.)
Let the user do it. Use uploadify with multiple simultaneous uploads enabled. So simple.
I have created PDF file by using php script and FPDF lib. It works fine, i have created two files named index.php and create_result.php in the file create_result.php i have written the php script for pdf file. what i want is that if i changed any program in this file there is no change in pdf file. my confusion is how to change the pdf file ,
ex:(result.pdf).
In the result.pdf file i have made a registraion form , if i want to change any thing in this result.pdf in the sense what should i do for a change?
Two things:
Does the Server have the permission to overwrite the PDF
Do you write Data from you login-Script into the PDF? If so, you have serious problems with multithreading, becaus one user can change the PDF for another user. You have to write a temporal PDF-File per user, with a unique filename.