Page 1 of 1

[SOLVED] Moving an file from one folder to another using PHP

Posted: Fri Jan 07, 2005 6:27 pm
by Mr Tech
Hey there,

I am wanting to move a file in one folder on my website to another folder on my website using PHP and FTP.

Here is my code:

Code: Select all

<?php
$ftp_server = "";
$ftp_user_name = ""; 
$ftp_user_pass = "";
$destination_file = "/public_html/ftptest/file.html";
$source_file = "/public_html/test.html";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name";
   }

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
   }

// close the FTP stream
ftp_close($conn_id);

?>
It says it connects to the server ok but I always get the FTP upload has failed! error and this error:

Warning: ftp_put(): open_basedir restriction in effect. File(/public_html/test.html) is not within the allowed path(s): (/home/webmaste/:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/webmaste/public_html/ftptest/test.php on line 24

I dont get the above error when I remove the slash (/) infront of the source and destination file but I still get the failed error.

Is there something wrong with my code? I copied it directly from the PHP website...

Maybe my source and destination files are setup wrong.

Thanks

ben

Posted: Fri Jan 07, 2005 6:39 pm
by feyd
try:

/home/webmaste/public_html/test.html

/public_html isn't a valid path.

Posted: Fri Jan 07, 2005 7:15 pm
by shiznatix
use ftp_pwd to view what folder ur starting in then use ftp_chdir to move to the folder u need to be in.

Posted: Fri Jan 07, 2005 10:04 pm
by Mr Tech
W00t it worked!

yes your suggestion worked feyd, thanks mate!

$destination_file = "/public_html/ftptest/test.html";
$source_file = "/home/webmaste/public_html/test.html";


Cheers

Ben