Web Hosting Talk







View Full Version : [php] whats wrong with this code?


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)

pphillips
09-19-2004, 05:35 PM
Add this into your form tag:

enctype="multipart/form-data"

EDIT: Actually you'd need that for uploading a new file through the form. Not sure about creating a new file via a form without uploading anything.

pphillips
09-19-2004, 06:03 PM
Are you trying to upload a filename specified in your form via this line?

$upload = ftp_put($conn_id, $filename, $source_file, FTP_BINARY);

Is $filename suppose to be the same as $file_name = $_POST['file_name']; ?

If so the variable names do not match, but I do not see how you can upload a variable to a file without creating the file first. Look into fopen. Also, not sure what you want to put into the file or where that comes from. Unless the file name in your form is a file that already exists on the server and your just uploading it to a remote site.

kneuf
09-19-2004, 09:40 PM
No, I am not trying to upload a file. I have a 'template' file stored in another directory, that I am copying into a new location (the new file)... I know the variables are a little messed up but they are there. I can't use fopen because it doesn't allow me to chmod the file to what I need (safe_mode on), so I am using FTP to create the new file, and change the permissions. Also, I have done this before using the same concept and it worked flawlessy, however all the files were in the same directory as each other (the 'template' file and the new file), and I don't know why this isn't working.

kneuf
09-28-2004, 03:08 PM
OK, just an update. I got it to work, I re did it all and filled in the full path to the download file and full path to the new file and it works :)