Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2003, 01:37 PM   PM User | #1
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Files and PHP

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
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 03:02 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Searching with your eyes fopen usually works better

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

__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 11-26-2003, 03:17 PM   PM User | #3
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
Well, fopen wouldn't seem to create, but thanks anyways
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 03:28 PM   PM User | #4
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
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 Code:
<?php
$filename 
$_POST['filename'];
$filecontents $_POST['filecontent'];
$open fopen($filename,"x");
$chmod chmod($filename0777);
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
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 03:41 PM   PM User | #5
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 11-26-2003, 03:59 PM   PM User | #6
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
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.
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 04:30 PM   PM User | #7
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Do you have multipart set in your form attribute???

Can you show us the html?
bcarl314 is offline   Reply With Quote
Old 11-26-2003, 04:35 PM   PM User | #8
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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/ )
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 11-26-2003, 04:37 PM   PM User | #9
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
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
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 05:25 PM   PM User | #10
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Quote:
Originally posted by SDP2006
Well, fopen wouldn't seem to create, but thanks anyways
Are you blind or just South Carolinian?

Quote:

'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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 11-26-2003, 07:32 PM   PM User | #11
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Quote:
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

Code:
<form method="POST" enctype="multipart/form-data" action="filecreater.php">
bcarl314 is offline   Reply With Quote
Old 11-26-2003, 07:39 PM   PM User | #12
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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?

Quote:
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.
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 11-26-2003, 10:23 PM   PM User | #13
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
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
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Old 11-26-2003, 10:29 PM   PM User | #14
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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:

PHP Code:
$fp fopen('foo.bar''w');
fwrite($fp"hello foo");
fclose($fp); 
__________________
De gustibus non est disputandum.
mordred is offline   Reply With Quote
Old 11-26-2003, 10:45 PM   PM User | #15
SDP2006
Regular Coder

 
Join Date: May 2003
Location: 34° 54' N 82° 13' W
Posts: 996
Thanks: 0
Thanked 0 Times in 0 Posts
SDP2006 is an unknown quantity at this point
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 Code:
<?php
echo "TEST";
?>
for the file contents. It is adding slashes to the " "'s. How do I stop it from doing that?
__________________
Stevie Peele
Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
#dev - any programming,etc. question
#design - design discussion and critque
#central - general chat
Come join us!
SDP2006 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:32 PM.


Advertisement
Log in to turn off these ads.