Hi
Is it possible to copy files directory from my pc to server??
| SitePoint Sponsor |

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.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

I know this program i asked if this can done using php,I want to exclude files![]()

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?

You want to copy files from your PC to a remote server using PHP?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

Hi,
I hope I'm understanding you correctly.
You can use use FTP Function to copy a file from local to remote.
For example:
See here: http://stackoverflow.com/questions/9...-server-in-phpPHP Code:<?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);
?>
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

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:
It's explained a bit better here: http://codestips.com/php-read-directory-into-array/PHP Code:$directory="exampledir";
$dirhandler = opendir($directory);
$numFiles=0;
while ($file = readdir($dirhandler)) {
if ($file != '.' && $file != '..'){
$numFiles++;
$files[$nofiles]=$file;
}
}
closedir($dirhandler);
Hope that helps.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

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?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

yes![]()

I'm away from the PC now, so if no one else replies in the meantime I'll post some code later.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

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 Code:<?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>";
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

no ,can't connect to remote server

How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

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

Bizarre.
Can you connect to the remote server using an FTP program?
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

yes

Sounds like a firewall / server configuration issue.
Try putting this line directly after the login:
So it looks like this:PHP Code:ftp_pasv($connect, true);
PHP Code:$connect = ftp_connect('www.url.com');
$login = ftp_login($connect, 'user', 'password');
ftp_pasv($connect, true);
if($login){
...
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog

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

Hi,
Glad you got the first problem solved
The script was just designed to make one directory on the remote server and upload a list files to it.
Sub directories would require recursion, which would get tricky.
Can you try this script with just one folder on your local PC which contains only files.
Before running it, please also, make sure that there is no folder of the same name on the remote server (i.e. if your local folder's name is "test", make sure that there is no "/test" on remote).
Let me know how you get on.
How well do you know your JavaScript from your jQuery?
Check out SitePoint's latest JavaScript challenge
My blog
Bookmarks