Php Script Ftp Equivalent Of Mput

<img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/huh.gif" style="vertical-align:middle" emoid=":huh:" border="0" alt="huh.gif" /> <br />Hi. <br /><br />I am<i> trying </i>to upload multiple files via an FTP PHP script using a wildcard like *.file_extension in FTP using mput or it's equivalent. <br /><br />So far with all the PHP methods I have found, they all list PUT, but none mention MPUT.<br />Here's the script I have (that works for a single file)...<br /><br />Thanks,<br />Mike<br /><br /><?php<br /><br />// set up basic connection<br />$ftp_server = "animalrepellentonline.com";<br />$conn_id = ftp_connect($ftp_server);<br />$ftp_user_name = "username";<br />$ftp_user_pass = "******"; <br />$source_directory = "/securityplus/";<br />$dir = "securityplus/";<br />$destination_directory = "/web/";<br />$destination_file = "testdoc.txt";<br />//$source_file = $dir . "testdoc.txt"; <br />$source_file = $dir . "*.txt"; <br /><br />// List the current local directory<br />echo "<b>Current local directory:</b>" . $dir . "<p>\n";<br /><br /><br /><br />// login with username and password<br />$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);<br /><br />// check connection<br />if ((!$conn_id) || (!$login_result)) { <br /> echo "FTP connection has failed!" . "<br>\n";<br /> echo "Attempted to connect to $ftp_server <b>User name:</b> $ftp_user_name" . "\n";<br /> exit; <br /> } else {<br /> echo "<b>Connected to:</b> $ftp_server<br><b>User name:</b> $ftp_user_name" . "\n";<br /> echo "<br>";<br /> }<br /> <br />// List the current remote directory<br />echo "<b>Current directory:</b>" . ftp_pwd($conn_id) . "<br>\n";<br /><br />// Change the remote directory<br />if (ftp_chdir($conn_id, $destination_directory)) {<br /> echo "<b>Current directory is now:</b>" . ftp_pwd($conn_id) . "<br>\n";<br />} else {<br /> echo "Couldn't change directory\n";<br />}<br /><br />// Send<br /><br />// upload single file<br />//$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); <br /><br />check upload status<br />if (!$upload) { <br /> echo "FTP upload has failed";<br /> } else {<br /> echo "<b>Uploaded:</b> $source_file <b>to</b> $ftp_server <b>as</b> $destination_directory$destination_file";<br />}<br /> <br />// close the FTP stream <br />ftp_close($conn_id); <br /><br />?><!--content-->
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.<!--content-->
Thanks! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> <br /><br />I am relativley new (several years doing asp but starting to convert to php) -- do you know of any examples of this in action?<br /> Thanks!<br />-Mike<!--content-->
Sure, it's quite simple (the PHP manual has a few examples on how to get a directory listing, too).<br /><br />This code snippet will do what you want (I haven't tested it, though!)<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// set up basic connection<br />// ......<br />$dh  = opendir("securityplus");<br />while (false !== ($source_file = readdir($dh)))<br />{<br />   // Upload the file<br />   $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);<br /><br />   //check upload status<br />   if (!$upload)<br />      echo "FTP upload has failed";<br />   else<br />      echo "<b>Uploaded:</b> $source_file <b>to</b> $ftp_server <b>as</b>$destination_directory$destination_file";<br />}<!--c2--></div><!--ec2--><br /><br />One thing I noticed is the directory path you're trying to open: /securityplus/<br />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.<br /><br />Let me know if this helps <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
 
Back
Top