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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

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

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try:

/home/webmaste/public_html/test.html

/public_html isn't a valid path.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
Post Reply