...

View Full Version : Files and PHP



SDP2006
11-26-2003, 01:37 PM
I was wondering how I might create a file using a PHP form action? I searched PHP.net but I didn't see any function to create a file. Thanks

Spookster
11-26-2003, 03:02 PM
Searching with your eyes fopen usually works better

http://us3.php.net/manual/en/function.fopen.php

:)

SDP2006
11-26-2003, 03:17 PM
Well, fopen wouldn't seem to create, but thanks anyways

SDP2006
11-26-2003, 03:28 PM
Okay, so my meager quest to create a file creation system So I don't have to be at home to add files. My error is this

Warning: fopen("", "x") - No error in c:\ibserver\www\fcreate\filecreater.php on line 4

Warning: fwrite(): supplied argument is not a valid File-Handle resource in c:\ibserver\www\fcreate\filecreater.php on line 7

and then my form action

<?php
$filename = $_POST['filename'];
$filecontents = $_POST['filecontent'];
$open = fopen($filename,"x");
$chmod = chmod($filename, 0777);
if (is_writable($filename)) {
fwrite($filename,$filecontents);
echo "$filename sucessfully created";
}
else {
echo "<B>Error:</b> Cannot write to file";
}
?>


I know there are probably many things wrong. Any help anyone?

Thanks

Nightfire
11-26-2003, 03:41 PM
Warning: fopen("", "x") - No error in c:\ibserver\www\fcreate\filecreater.php on line 4

That says it all.

$filename = $_POST['filename'];

Isn't being set. Read the errors.

SDP2006
11-26-2003, 03:59 PM
Okay, I uploaded it to the web http://www.net-riches.com/includes/fcreate/

Try for yourself, I get different errors now. Still using same code as above.

bcarl314
11-26-2003, 04:30 PM
Do you have multipart set in your form attribute???

Can you show us the html?

Nightfire
11-26-2003, 04:35 PM
Have you chmodded the directory the file's to be created in?

bcarl314, the html is on the page he showed ( http://www.net-riches.com/includes/fcreate/ )

SDP2006
11-26-2003, 04:37 PM
I don't believe I had, but where are the files going to be created in? The folder that the script is in?

Thanks for your continued help

Spookster
11-26-2003, 05:25 PM
Originally posted by SDP2006
Well, fopen wouldn't seem to create, but thanks anyways

Are you blind or just South Carolinian? :D




'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

bcarl314
11-26-2003, 07:32 PM
Originally posted by Nightfire
Have you chmodded the directory the file's to be created in?

bcarl314, the html is on the page he showed ( http://www.net-riches.com/includes/fcreate/ )

D'OH <slaps what="head">

Ok, in that case, change your <form> tag as below



<form method="POST" enctype="multipart/form-data" action="filecreater.php">

mordred
11-26-2003, 07:39 PM
Is PHP 4.3.2 installed on your host? That's needed as a minimum requirement if you want to use the 'x' flags. Why don't you go with the standard 'w' argument?



Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

SDP2006
11-26-2003, 10:23 PM
Yes, I have 4.2.3 on my server. I changed it to "w". Now it is creating the file, but nothing is being written to the file. I still get one error, which I think is the reason the file is not being written -

Warning: fwrite(): supplied argument is not a valid File-Handle resource in /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/fcreate/filecreater.php on line 7

If you wanna test it, http://www.net-riches.com/includes/fcreate/


Thanks again for the continued help :D

mordred
11-26-2003, 10:29 PM
Assuming you use a similar code as shown above, you need to pass the file handle as an argument to frwrite(), not the file name itself. Basically this example illustrates the usage:



$fp = fopen('foo.bar', 'w');
fwrite($fp, "hello foo");
fclose($fp);

SDP2006
11-26-2003, 10:45 PM
Wonderful guys! It creates and writes to the file now. Step 1 complete. Now, when I acess the file I created, example http://www.net-riches.com/includes/fcreate/testone.php i get the errors as you can see. I typed in testone.php for the file name and
<?php
echo "TEST";
?> for the file contents. It is adding slashes to the " "'s. How do I stop it from doing that?

Nightfire
11-26-2003, 11:33 PM
http://www.php.net/stripslashes

mordred
11-26-2003, 11:55 PM
Before you do anything else, secure the form immediately from general access! Right now everybody can create arbitrary PHP files on your server. You can also delete the info.php file I just created... it was just a test, nothing terrible inside it. ;)

SDP2006
11-27-2003, 12:33 AM
Should I .htaccess it?

btw, I changed the path to the file for my safety. so the links in previous replies will be 404's

Stevie



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum