kneuf
09-18-2004, 06:48 PM
arggg, this has been bugging my for a little while now. I have a script that is supposed to make a new file in a directory using PHP's FTP functions. I can login fine, but when it goes to 'put' the file into its place, it doesn't work. The directory structure looks something like this:
--------------
/home/test/vhosts/test_site.loc/
--------------
I have a 'templates' file (the file that is used when making the new file) which is located at:
--------------
/home/test/vhosts/test_site.loc/inc/templates/default.inc.php
--------------
here is the code:
<?
//$filename = $_GET['file'];
$ftp_server = "192.168.1.102";
$ftp_user = 'username';
$ftp_pass = 'password';
$file_name = $_POST['file_name'];
$file_op = $_GET['file_op'];
$template = $_POST['template'];
$base_dir = "vhosts/test_site.loc/";
$template_dir = "inc/templates/";
$source_file = $file_name;
$filename = $template_dir . $template;
if (!$file_op) {
?>
<form action='?file_op=make_new' method='post'>
<table>
<tr><th>Filename:</th><td><input type='text' name='file_name' value='test.php' class='form_but'></td><td><input type='submit' value='Make' class='form_but'></td></tr>
<tr><th>Template:</th><td colspan='2' align='left'><select name='template' class='form_but'>
<option value='default.inc.php'>Admin PopUp
</select></td></tr>
</table>
</form>
<?
} elseif ($file_op == "make_new") {
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
die("FTP connection has failed !");
}
// try to change the directory to somedir
if (ftp_chdir($conn_id, $base_dir)) {
// upload the file
$upload = ftp_put($conn_id, $filename, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "<p>FTP upload has failed! Could not upload $source_file from $filename</p>";
} else {
echo "<p>Uploaded $source_file to $ftp_server as $filename</p>";
}
// try to chmod $file to 777
$chmod_cmd="CHMOD 0777 ".$filename;
if (ftp_site($conn_id, $chmod_cmd)) {
echo "<p>$file chmoded successfully to 777\n</p>";
} else {
echo "<p>could not chmod $file\n</p>";
}
} else {
echo "Couldn't change directory\n";
}
// close the connection
ftp_close($conn_id);
}
?>
I can login and everything, but I get this message (not very descriptive):
FTP upload has failed! Could not upload test.php from inc/templates/default.inc.php
-------------------
I know the code is a little messy, I am just trying to get it to work right now. Any ideas?
[edit]
and yes, the files(and directories) are chmod'd to 777)
--------------
/home/test/vhosts/test_site.loc/
--------------
I have a 'templates' file (the file that is used when making the new file) which is located at:
--------------
/home/test/vhosts/test_site.loc/inc/templates/default.inc.php
--------------
here is the code:
<?
//$filename = $_GET['file'];
$ftp_server = "192.168.1.102";
$ftp_user = 'username';
$ftp_pass = 'password';
$file_name = $_POST['file_name'];
$file_op = $_GET['file_op'];
$template = $_POST['template'];
$base_dir = "vhosts/test_site.loc/";
$template_dir = "inc/templates/";
$source_file = $file_name;
$filename = $template_dir . $template;
if (!$file_op) {
?>
<form action='?file_op=make_new' method='post'>
<table>
<tr><th>Filename:</th><td><input type='text' name='file_name' value='test.php' class='form_but'></td><td><input type='submit' value='Make' class='form_but'></td></tr>
<tr><th>Template:</th><td colspan='2' align='left'><select name='template' class='form_but'>
<option value='default.inc.php'>Admin PopUp
</select></td></tr>
</table>
</form>
<?
} elseif ($file_op == "make_new") {
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
die("FTP connection has failed !");
}
// try to change the directory to somedir
if (ftp_chdir($conn_id, $base_dir)) {
// upload the file
$upload = ftp_put($conn_id, $filename, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "<p>FTP upload has failed! Could not upload $source_file from $filename</p>";
} else {
echo "<p>Uploaded $source_file to $ftp_server as $filename</p>";
}
// try to chmod $file to 777
$chmod_cmd="CHMOD 0777 ".$filename;
if (ftp_site($conn_id, $chmod_cmd)) {
echo "<p>$file chmoded successfully to 777\n</p>";
} else {
echo "<p>could not chmod $file\n</p>";
}
} else {
echo "Couldn't change directory\n";
}
// close the connection
ftp_close($conn_id);
}
?>
I can login and everything, but I get this message (not very descriptive):
FTP upload has failed! Could not upload test.php from inc/templates/default.inc.php
-------------------
I know the code is a little messy, I am just trying to get it to work right now. Any ideas?
[edit]
and yes, the files(and directories) are chmod'd to 777)
