Jump to content

Recommended Posts

Posted

:lol:

Hi.

 

I am trying to upload multiple files via an FTP PHP script using a wildcard like *.file_extension in FTP using mput or it's equivalent.

 

So far with all the PHP methods I have found, they all list PUT, but none mention MPUT.

Here's the script I have (that works for a single file)...

 

Thanks,

Mike

 

<?php

 

// set up basic connection

$ftp_server = "animalrepellentonline.com";

$conn_id = ftp_connect($ftp_server);

$ftp_user_name = "username";

$ftp_user_pass = "******";

$source_directory = "/securityplus/";

$dir = "securityplus/";

$destination_directory = "/web/";

$destination_file = "testdoc.txt";

//$source_file = $dir . "testdoc.txt";

$source_file = $dir . "*.txt";

 

// List the current local directory

echo "<b>Current local directory:</b>" . $dir . "<p>\n";

 

 

 

// 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!" . "<br>\n";

echo "Attempted to connect to $ftp_server <b>User name:</b> $ftp_user_name" . "\n";

exit;

} else {

echo "<b>Connected to:</b> $ftp_server<br><b>User name:</b> $ftp_user_name" . "\n";

echo "<br>";

}

 

// List the current remote directory

echo "<b>Current directory:</b>" . ftp_pwd($conn_id) . "<br>\n";

 

// Change the remote directory

if (ftp_chdir($conn_id, $destination_directory)) {

echo "<b>Current directory is now:</b>" . ftp_pwd($conn_id) . "<br>\n";

} else {

echo "Couldn't change directory\n";

}

 

// Send

 

// upload single file

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

 

check upload status

if (!$upload) {

echo "FTP upload has failed";

} else {

echo "<b>Uploaded:</b> $source_file <b>to</b> $ftp_server <b>as</b> $destination_directory$destination_file";

}

 

// close the FTP stream

ftp_close($conn_id);

 

?>

Posted

I don't think you can use wildcards in there. You'll need to cycle through all the files in the directory, filtering out the ones you don't want and sending the ones you want, one by one.

Posted

Sure, it's quite simple (the PHP manual has a few examples on how to get a directory listing, too).

 

This code snippet will do what you want (I haven't tested it, though!)

>// set up basic connection
// ......
$dh  = opendir("securityplus");
while (false !== ($source_file = readdir($dh)))
{
  // 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 "<b>Uploaded:</b> $source_file <b>to</b> $ftp_server <b>as</b>$destination_directory$destination_file";
}

 

One thing I noticed is the directory path you're trying to open: /securityplus/

This won't work because that path is relative to the server root. If you want to use absolute paths, you should allways append your home directory path first (for example, /home/yourusername/securityplus), otherwise the opendir() call will most probably fail due to lack of permissions to open that directory - but even if it didn't fail, that wouldn't be the directory you were looking for.

 

Let me know if this helps :(

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...