View Full Version : Resolved FTP file creation
AndyA
11-26-2008, 02:44 PM
Hi, I have recently started to use php scripts, and so far I have a basic (however working) login and registration system, which adds records to a MySql database. I am now trying to expand on this, and add some php code to my registration page which creates a file on my ftp account when a user submits their details. In other words, I would like a new "profile page" to be created when a valid new member registers to my site.
So far, I have the following, but it does not work. Any suggestions?
<?php
function ftp_copy($source_file, $destination_file)
{
$ftp_server = $ftpserver;
$ftp_user = $ftpuser;
$ftp_password = $ftppass;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
if((!$conn_id) || (!$login_result))
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
}
$fh = fopen("/public_html/profiles/$userUsername","w");
if($fh==false){
ftp_close($conn_id);
die("unable to create file");
}
ftp_close($conn_id); ?>
Also on this script are the registration form and an "include" to my variables details, e.g. $ftp_user.
Thanks in advance for any help. :)
BabblingIdjit
11-26-2008, 04:07 PM
Judging from the code, these variables:
$ftpserver;
$ftpuser;
$ftppass;
are defined outside of the function scope. As such, they are not visible to the function. You will need to:
1. Pass the variables to the function (recommended)
2. Declare them as "global" inside the function (not recommended)
AndyA
11-26-2008, 05:47 PM
Thanks very much for the early reply. How would I go about passing the variables to the function?
Also, the values of each variable are in another php script which I have "included" in this registration script. Why wouldn't that work?
Thanks again. :)
BabblingIdjit
11-26-2008, 06:06 PM
It's a question of scope (more info (http://www.php.net/variables.scope)). Variables used in functions are local to the function unless they are passed to the function (or declared as "global").
To pass the variables:
function ftp_copy($source_file, $destination_file, $ftp_server, $ftp_user, $ftp_password)
{
// function code
}
AndyA
11-26-2008, 06:36 PM
Thanks again John. :)
The registration form and content appears fine now (without errors); however the file still isn't created. :confused:
I am not sure, but is the specified directory valid? (As is given in my opening code):
("/public_html/profiles/$userUsername","w");
Thanks. :)
BabblingIdjit
11-26-2008, 06:41 PM
Probably not. The directory is probably something more like /home/user_name_here/public_html/profiles/
* replace user_name_here with your hosting account username
(Of course, your host could use a different directory structure, but this is common)
Make sure that this folder exists and is writeable.
AndyA
11-26-2008, 06:58 PM
Hmm... the destination is writeable, but the file still isn't being created. :confused:
I've tried all sorts of directory combinations - I guess I'll keep trying different ones for now. :)
Thanks for all of your help John - you've got my main problems working great! :D
BabblingIdjit
11-26-2008, 07:04 PM
Assuming your hosting package include CPanel, you can use File Manager to open the folder - File Manager will display the full path to the folder. I'm sure Plesk and other control panels include a similar feature.
If that doesn't help, post the current version of your code.
AndyA
11-26-2008, 07:20 PM
You're right. I am using File Manager, and the destination is shown in the form:
<mywebsite.co.uk>/public_html/profiles
So I tried this as the destination for fopen (as well as without the <>), but the file still wasn't created.
I want the file created to be the username of the new member - which is why I put the end of the directory as $userUsername. I just thought that I might need to add the extension (i.e. .html). How would I correctly do this (if this works).
(My current version of php is 5.2.6)
BabblingIdjit
11-27-2008, 03:56 AM
Most likely, you'll still need "/home/" before the rest of the path. If you can't figure it out, your host should be able to help.
(My current version of php is 5.2.6)
What I meant was, post the current code that you've written.
AndyA
11-27-2008, 03:59 PM
I've figured it out, and it's working now! :D
I used a completely different code:
$ftp_server = $ftpserver;
$ftp_user = $ftpuser;
$ftp_password = $ftppass;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
if((!$conn_id) || (!$login_result))
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
}
@ftp_mkdir($conn_id, '/public_html/profiles/" . $userUsername . "');
$temp = tmpfile();
//Upload the temporary file to server
@ftp_fput($conn_id, '/public_html/profiles/" . $userUsername/$userUsername . ".html', $temp, FTP_BINARY);
ftp_close($conn_id);
This all works fine now and the files and directories are created on submit.
Thanks very much for all of your help John. :)
BabblingIdjit
11-27-2008, 05:25 PM
you're welcome. glad you got it working
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.