Copy files

Hi

Is it possible to copy files directory from my pc to server??

Hi there,

It might be an obvious question, but would anything speak against using a FTP program?
Filezilla is one I can recommend and it is well documented.
Once you have connected to your server via FTP, it is just a matter of dragging the appropriate files and/or directories from one window to the next.

I know this program i asked if this can done using php,I want to exclude files :frowning:

You want to copy files from your PC to a remote server using PHP?

I want to upload directory files from local PC to Server and I want to exclude some files

Is this possible Using php?

if not how can I do it?

Hi,

I hope I’m understanding you correctly.
You can use use FTP Function to copy a file from local to remote.
For example:

<?php
$connect = ftp_connect('111.111.111');  //connect to server

$login = ftp_login($connect, 'ftp_username', 'ftp_password');

if($login)
{
    if(ftp_put($connect, '/home/user/john/test.txt','test.txt', FTP_BINARY))
    {
        echo "Success";
    }
}

ftp_close($connect);
?>

See here: http://stackoverflow.com/questions/9020622/copy-file-to-remote-windows-server-in-php

this will upload one file,i want to upload directory

This is true.
You can however, use readdir to read a list of all files in a directory into an array (minus the ones you don’t want).
Then you could modify the code I just posted to loop over the array and upload them one by one.

Here’s an example:

$directory="exampledir";
$dirhandler = opendir($directory);
$numFiles=0;
while ($file = readdir($dirhandler)) {
  if ($file != '.' && $file != '..'){
    $numFiles++;
    $files[$nofiles]=$file;                
  }   
}
closedir($dirhandler);

It’s explained a bit better here: http://codestips.com/php-read-directory-into-array/

Hope that helps.

sorry,code at server , how to define directory at local pc?
can you give example to write directory?

Hi there,
I have an awful feeling that we are talking at cross purposes.
Are you asking me to provide an example of how to upload a directory (including the files contained within it), from your local PC to a remote server?

yes :confused:

I’m away from the PC now, so if no one else replies in the meantime I’ll post some code later.

Hey,

I’m back now.
Here’s the code.

All this will do is read the files from a specified directory into an array, then it will connect to a remote server, make a directory with the same name as the one it read from and finally transfer all of the files from the local directory to the remote one.

However, the script is very brittle and implements minimal error checking. It is more so as to show you how such a thing would be done.

Hope this helps.

<?php 
$dir = "temp";
$path = "C:/xampp/htdocs/";
$dirhandler = opendir($path.$dir);
$numFiles=0;
while ($file = readdir($dirhandler)) {
  if (!is_dir($file)){
    $numFiles++;
    $files[$numFiles]=$file;                
  }   
}
closedir($dirhandler);
 
echo "<p><strong>Local path:</strong> $path$dir</p>";
echo "<p>Found following files:<br>==============<br>";
foreach($files as $file) {
  echo "$file<br>";
}
echo "</p>";

function customError($errno, $errstr)  {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Ending Script";
  die();
}
	
set_error_handler("customError");

//Assumes dir doesn't exist remotely
$connect = ftp_connect('www.url.com');
$login = ftp_login($connect, 'user', 'password');  
if($login){  
  ftp_mkdir($connect, $dir);
  ftp_chdir($connect, $dir);
  foreach($files as $file) {
    ftp_put($connect, $file, $file, FTP_BINARY);
  }
}  
ftp_close($connect);  
echo "<p>Transfer complete!</p>";

no ,can’t connect to remote server

Oh dear!

Have you filled these two lines out with your details?

$connect = ftp_connect('www.url.com');
$login = ftp_login($connect, 'user', 'password');

yes but can’t
Error: [2] ftp_connect():

Bizarre.
Can you connect to the remote server using an FTP program?

yes

Sounds like a firewall / server configuration issue.
Try putting this line directly after the login:

ftp_pasv($connect, true);

So it looks like this:

$connect = ftp_connect('www.url.com');
$login = ftp_login($connect, 'user', 'password');
ftp_pasv($connect, true);
if($login){
...

thanks ,connected true ,it’s upload parent directory,but can’t upload sub folders and files
Error: [2] ftp_mkdir(): Create directory operation failed.