HELP Needed With Creating File Using PHP
07-07-2006, 06:16 AM
|
HELP Needed With Creating File Using PHP
|
Posts: 36
Name: Hayden Shaw
|
I am new to using PHP and i am trying to create a script that will create a folder and place a html document in the folder. So far i have done this with the following code
PHP Code:
mkdir ("{$rootpath}/{$username}", 0755); $filename="{$rootpath}/{$username}/index.html"; $file=fopen($filename,"w", 0777); fwrite($file, "Hello\n"); fclose($file);
But the problem i am having is that once the file index.html is created i am unable to delete it from the sever. I have tried deleting it using WS FTP but it just wont delete. Can anyone tell me how to fix this?
I am not wanting to delete the file using PHP i am just wanting to delete the file at a later date using FTP. But every file i have created using the script at the top will not delete using FTP it just comes up with an error saying "550 Could not delete index.html: Permission denied". This only happens with files created with the php code.
|
|
|
|
07-07-2006, 06:45 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 880
Location: Leeds UK
|
1st thing.
Change the $file=fopen($filename,"w", 0777); to $file=fopen($filename,"w", 0755);
777 perms are dangerous VERY dangerous
You need to chown the permisions of the file for its been upload and set as apache's file which means you no longer own it.
chown($path, $user); where $user is your account username.
Ibbo
|
|
|
|
07-07-2006, 07:17 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 251
Location: Belgium, Antwerp, Zoersel
|
Quote:
|
Originally Posted by ibbo
PHP Code:
chown($path, $user);
|
The problem is that you need to run apache as root, which almost never happens and is much more dangerous than chmodding to 777.
PHP Code:
fopen($filename,"w", 0777);
is not correct however. You can't set file permissions using the fopen function, use chmod instead:
PHP Code:
mkdir ("{$rootpath}/{$username}", 0755);
$filename="{$rootpath}/{$username}/index.html";
$file=fopen($filename,"w");
fwrite($file, "Hello\n");
fclose($file);
chmod($filename, 0777);
|
|
|
|
07-07-2006, 07:33 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 36
Name: Hayden Shaw
|
I have tried both of the above methods but neither work. The first one has this error
Warning: chown(): Operation not permitted in /home/username/public_html/multihost/regconfirm.php on line 150
and the second one operates fine but does not fix the problem of the below error in ws ftp
550 Could not delete index.html: Permission denied
Does anyone know how to fix this?
|
|
|
|
07-07-2006, 08:19 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 880
Location: Leeds UK
|
No of course not it does need root perms to run chmod on the server.
If all the folders and files are within a folder itself you can chgrp (folder, apache) before you upload it. Then you will have access to the folder where you can delete stuff etc.
failing that you can call unlink(file) which will use apache's permisions and drop the file for you.
Ibbo
|
|
|
|
07-07-2006, 08:25 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 251
Location: Belgium, Antwerp, Zoersel
|
Yeah, for some reason the chmod command doesn't have any effect on some servers. The only way I know to solve that, is to use ftp ( this class helps alot). You could then write the file to a temporally location (using the script above, and then copy it to it's final location using ftp. Then delete the temp-file and you should be able to delete the file using your ftp-client  .
|
|
|
|
07-07-2006, 09:20 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 251
Location: Belgium, Antwerp, Zoersel
|
Quote:
|
Originally Posted by ibbo
No of course not it does need root perms to run chmod on the server.
If all the folders and files are within a folder itself you can chgrp (folder, apache) before you upload it. Then you will have access to the folder where you can delete stuff etc.
|
You do not need root permissions to use chmod, but you do need that to use chgrp. So that solves nothing.
Quote:
|
failing that you can call unlink(file) which will use apache's permisions and drop the file for you.
|
That's true, but ezhs8554 wanted to delete them using his ftp-client.
|
|
|
|
07-07-2006, 07:56 PM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 36
Name: Hayden Shaw
|
I found this information on php.net. It is pretty much what Orodreth said i should do, but as i am very new to php i am having trouble getting the script to work. It has an error on the first line. To make it work i did add my login info but i am unsure if i did it correctly. Could someone tell me if this script will work and if so give me an example of what i need to change.
Sometimes a web host will open PHP sessions with a user of 'nobody'. Files created by this user may not have the correct permissions to allow management of those files by the actual owner of the site. The following script allows the actual owner to open access to a directory so that 'nobody' can create a file using fopen(). Then using the handle created by 'nobody', the ftp_fput() command saves the file with the correct owner. The file 'nobody' created is discarded.
PHP Code:
$connection = ftp_connect($ftpServer); ftp_login($connection, $ftpUser, $ftpPass); ftp_chdir($connection, $ftpDir); // open the directory so that 'nobody' can create a temporary file ftp_site($cn, "CHMOD 777 $ftpDir"); $new="tempFile"; @unlink($new); // just in case $handle=fopen($new,"x"); chmod($new,0777); fputs($handle,"a bunch of stuff..."); fclose($handle); // have to rewind $handle=fopen("tempFile","r"); ftp_fput($connection, "finalFile", $handle, FTP_ASCII); fclose($handle); unlink($new); // remove open access to the directory ftp_site($connection, "CHMOD 755 $ftpDir") ftp_close($connection);
|
|
|
|
07-07-2006, 08:35 PM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 251
Location: Belgium, Antwerp, Zoersel
|
There are some errors in that code, but I don't see any in the first line. Here's what I would do:
PHP Code:
###########################
# ftp settings #
$ftpServer='ftp.server.com'; //ftp server, without ftp://
$ftpPort=21; //the ftp port (don't change if you don't know what it is)
$ftpUser='username'; //ftp username
$ftpPass='password'; //ftp password
# end ftp settings #
###########################
###########################
# file settings #
$tempfile='tempfile.ext';
$file='definitive_file.ext';
$dir='/path/to/store/the/file/in'; //note, this is the ftp path, not the real path
$file_content='all the files content';
$dirpermissions=0755; //the final permissions of the dir
# end file settings #
###########################
//connect to server
$connection = ftp_connect($ftpServer, $ftpPort);
ftp_login($connection, $ftpUser, $ftpPass);
ftp_chdir($connection, $ftpDir);
//chmod directory so that the server can create a file
ftp_chmod($connection, 0777, $ftpDir);
//make temporary file
$handle=fopen($tempfile, 'w');
fwrite($handle, $file_content);
fclose($handle);
//upload file using ftp
$handle=fopen($tempfile, 'r');
ftp_fput($connection, $file, $handle, FTP_ASCII);
fclose($handle);
//delete temporary file
unlink($new);
//reset directory permissions
ftp_chmod($connection, 0755, $ftpDir);
//close connection
ftp_close($connection);
I didn't test it, but it should do the job.
|
|
|
|
07-08-2006, 07:12 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 36
Name: Hayden Shaw
|
Thanks Orodreth for that script i have it just about working. It is having one error. which is
"Fatal error: Call to undefined function: ftp_chmod()" I have found out that ftp_chmod is only supported in php 5 and my sever runs php 4. To fix this i have tried using the following code
PHP Code:
ftp_site($connection, 'CHMOD 0777'.$ftpDir);
But i get this error Warning: ftp_site(): SITE CHMOD: No file name in /home/username/public_html/multihost/regconfirm.php on line 170
line 170 is the line shown above.
Does anyone know how to fix this?
|
|
|
|
07-09-2006, 03:42 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 251
Location: Belgium, Antwerp, Zoersel
|
Whell, seems like you've entered a rong directory. It most be relative to your ftp-server-root, not the real root.
|
|
|
|
07-09-2006, 05:45 AM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 36
Name: Hayden Shaw
|
The directory i used was
/public_html/multihost/
i have also tried
/username/public_html/multihost/
/home/username/public_html/multihost/
/multihost/
I have got the same error with each one
|
|
|
|
07-09-2006, 05:56 PM
|
Re: HELP Needed With Creating File Using PHP
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
If it's a Unix webserver, it usually follows the path as:
/home/public_html/multihost/
** Although - not always the case.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to HELP Needed With Creating File Using PHP
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
|