PDA

Click to See Complete Forum and Search --> : create a file


TheGoldenShogun
Aug 19th, 2002, 11:16 AM
if a file doesnt exists, how do I create on in php? simple one but I never really worked with files before.

Gimlin
Aug 19th, 2002, 11:21 AM
well if your writing to a file, and it does not exist it will create it.

TheGoldenShogun
Aug 19th, 2002, 11:46 AM
ok, this goes with my other question. I'm creating a file upload for where someone submits their zip file and I upload it to the file server and I want to add some kind of bar so they know how much is uploaded (I know thats going to be a pain). But for right now I'm trying to just throw text into a file on the ftp server... here's what my code looks like..

<?php
$file = fopen ("ftp://username:password@xxx.xxx.xxx.xxx/uploads/text", "w");
if (!$file) {
echo "<p>Unable to open remote file for writing.\n";
exit;
}
/* Write the data here. */
fputs ($file, "This is my test" . "\n");
echo("File Created?");
fclose ($file);
?>

now, when are no files in that directory it does nothing. When I create a text document with the same name text.txt it creates the text file with no extension. when the text file with no extension exists, it gives me an error that it cant over write existing file...

:mad:

Gimlin
Aug 19th, 2002, 12:19 PM
You have to give the file you are writing to a name, even if it does not exist yet

$file = fopen ("ftp://usernameassword@xxx.xxx.xxx.xxx/uploads/text/sometextfile.txt", "w");

TheGoldenShogun
Aug 19th, 2002, 12:49 PM
cool, I got that working. Now its typing a line of information into the file. But the new problem is that its looking to write to the file, the information from the remote user file and I keep getting this...

Warning: file("C:\Programming\test.txt") - No such file or directory in /home/sites/home/web/member/uploadthefile.php on line 16

Any ideas?

Gimlin
Aug 19th, 2002, 12:58 PM
with fopen use 'a' instead of 'w'

TheGoldenShogun
Aug 20th, 2002, 07:11 AM
alright, this thing has me tapping out... I get to a point where it works, then it doesnt work. here's my code.

<?
echo($thisfile); //just to see the tmp filename
$fd = fopen ($thisfile, "r"); //$thisfile is from a file input type on the previous page
while (!feof ($fd)) {
$buffer = $buffer.fgets($fd, 4096);
}
fclose ($fd);

mail("thegoldenshogun@aol.com", "Subject", $buffer, "FROM:nobody@nobody.net");
//mail myself the $buffer string to make sure its got stuff in it (which is always does)

$XXfileX = fopen ("ftp://username:password@xxx.xxx.xxx.xxx/uploads/test.txtxt", "a+");
fputs($XXfileX, $buffer."\n");
fclose($XXfileX);
?>

The thing is, it works sometimes and not others. Like if I post my file and refresh this php page around 3 times, it'll show up on the ftpserver. But not the first or second time. There seems to be no consistancy to when it goes through and I cant figure it out. Can anybody help?


:mad: :eek: :mad: :confused: :mad:

scoutt
Aug 20th, 2002, 09:17 AM
If filename begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and a pointer to the requested file is returned. If the server does not support passive mode ftp, this will fail. You can open files for either reading or writing via ftp (but not both simultaneously).
are you sure it supports passive mode?