Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Upload Form Help


PHP Upload Form Help

Reply
  #1 (permalink)  
Old 12-12-03, 01:01 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Question PHP Upload Form Help

In the past i have created quite a few PHP based FTP programs, whereby the user would get the file from their computer by browsing, and upload it to the server. My friend asked me to create a program whereby they would enter text into an input box/text area, and then upload, and it would create a file on their server, just like the FTP program, but without having the file on their computer. Their host just supports PHP, not MySQL, so i was wondering if it's possible (although i pressume it is, seeing that my FTP program was entirely based on PHP, without MySQL), to do this? If so, could someone give me a little help on it please, thanks
Reply With Quote
  #2 (permalink)  
Old 12-12-03, 01:19 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

So basically your friend wants to create a file and its contents on-the-fly via textarea/box, correct?

If so, you can use fopen($filename, 'w').

I believe fopen() with access mode 'w', 'w+', 'a', or 'a+' will create the file with file name of $filename if it does not already exist. You can first create it and then do whatever you want with the file and user input.

Hope this helps.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 12-12-03, 04:57 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
I'm using the following code at the moment, but it's not displaying the file at all, but i know the path is correct. The textarea box is just blank, and i have CHMODed the file.php to 666. Any help would be very useful...

PHP Code:

<?php

$file 
"/home/sites/site10/web/file.php";
$fs fopen$file"a+" ) or die("error when opening the file");

while (!
feof($fs)) {
   
$contents .= fgets($fs1024);
}

fclose($fs);
?>

<html>
<head>
<title></title>
</head>
<body>
<form action="save.php" method="post">
<input type=hidden name="file" value="<?php echo $file?>">
<textarea name="contents"><?php echo htmlspecialchars($contents); ?></textarea>
<input type="submit" value="Submit">
</form>
</body>
</html>
The save.php file is:

PHP Code:

<?php

$fs 
fopen$_POST["file"], "a+" ) or die("error when opening the file");
fwrite($fs$_POST["contents"]);
fclose();
?>
Reply With Quote
  #4 (permalink)  
Old 12-12-03, 06:02 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there.

I just literally copy & pasted your code except for the first line where you assign the path to $file and ran the scripts on my server. Other than getting fclose() wrong param error (you've forgotten $fs for fclose() in save.php), it seems to work just fine for me.

Are you getting any error message? Have you tried outputing into non-textarea? Did you check if your input has been saved onto the file that you specified properly?

Sorry I can't say further than this given the situation...
__________________
Blavv =|
Reply With Quote
  #5 (permalink)  
Old 12-12-03, 06:32 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Well i've got it to semi-work by changing the a+ in save.php to w and by changing the top to:

PHP Code:

$file "http://www.mysite.com/file.php"
I'm just wondering if that's safe, and will it work, becuase when i click submit it says:

Quote:
Warning: fopen("","w") - Inappropriate ioctl for device in /home/sites/site10/web/save.php on line 2
error when opening the file
Could you possibly change the code for that to make it work, by changing save.php, or does the main file has to be changed? What needs to be altered? Thanks

Last edited by tsb; 12-12-03 at 07:14 AM.
Reply With Quote
  #6 (permalink)  
Old 12-12-03, 10:14 AM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Gah, i'm just really confused, any help is very needed...
I know something must be wrong with the save.php file, so if anyone could please show me what i'd be grateful...
Reply With Quote
  #7 (permalink)  
Old 12-12-03, 03:40 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there, my again =)

Just to let you know, edit mode of "w" is write-only and the moment you open the file with this param, all the file contents that was there will be truncated. Is that what you want?

Re remote file fopen, read this excellent doc from PHPBuilder: http://www.phpbuilder.com/manual2/ma...mote-files.php

Code:
Warning: fopen("","w") - Inappropriate ioctl for device in /home/sites/site10/web/save.php on line 2
error when opening the file
It looks like there's no filename param passed to fopen().

Good luck!
__________________
Blavv =|
Reply With Quote
  #8 (permalink)  
Old 12-12-03, 06:18 PM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Hi

With w will it basically overwrite the file then? That's what i want, and what someone told me it would do...

All i've got at the moment in save.php is:

PHP Code:

<?php

$fs 
fopen$_POST["file"], "w" ) or die("error when opening the file");
fwrite($fs$_POST["contents"]);
fclose();
?>
Can you pssibly just change what parts need to be fixed, and explain why it still comes up with the same old error, thanks.
Reply With Quote
  #9 (permalink)  
Old 12-12-03, 08:55 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, it's me again.

Quote:
Originally Posted by tsb
Hi

With w will it basically overwrite the file then? That's what i want, and what someone told me it would do...

All i've got at the moment in save.php is:

PHP Code:

<?php

$fs 
fopen$_POST["file"], "w" ) or die("error when opening the file");
fwrite($fs$_POST["contents"]);
fclose();
?>
Can you pssibly just change what parts need to be fixed, and explain why it still comes up with the same old error, thanks.
Two things:

1) rewrite "fclose()" as "fclose($fs)" - you need a parameter (file handler) to close.
2) If you open the file with edit mode "w" in the first file (not save.php), try using "r" instead.

Other than this, it works fine on my server locally or remotely.

For debug reason you might want to add echo $_POST["contents"]; before you close the file in save.php. See if what you wrote was really written down.

Hope this helps.
__________________
Blavv =|
Reply With Quote
  #10 (permalink)  
Old 12-12-03, 10:15 PM
tsb tsb is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
I did that, and the save.php is now:

PHP Code:

<?php

$fs 
fopen$_POST["file"], "w" ) or die("error when opening the file");
fwrite($fs$_POST["contents"]);
echo 
$_POST["contents"];
fclose($fs);
?>
But it still comes up with this error:

Code:
Warning: fopen("","w") - Inappropriate ioctl for device in /home/sites/site10/web/save.php on line 2
error when opening the file
It says fopen("","w"), so is it not getting the file as such that i'm trying to edit? The code above is the entire save.php file, so am i overlooking something? Thanks
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP script that uploads and emails the form. ./n00b3 PHP 5 11-24-03 03:16 PM
PHP Triad/Upload form eddyvlad PHP 6 10-06-03 11:17 PM
php upload center DingDong PHP 2 07-20-03 04:16 AM
php Mirror upload and download software TAK PHP 1 06-25-03 10:31 AM
PHP help ! Submit the form kevin PHP 1 06-24-03 08:09 AM


All times are GMT -5. The time now is 07:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.