grissom Posted February 8, 2009 Share Posted February 8, 2009 Hi I'm running XAMPP on my laptop, and running PHP code from my hard drive. What I'd like to do is automatically copy all the contents in a subfolder on an internet server onto a folder on my hard drive the equivalent of : copy "http://www.mywebsite.com/images/*.*" "my_c_drive_imgs/" (in pseudocode) I've tried all kinds of things including (1) something based on opendir/readdir (2) shell exec commands, (3) things involving 'for each' and 'copy' and (4) things involving "header('content-type') None of them have worked. AAArgh !! Please can someone PLEASE help. I bet it's dead simple when you know how, but it's driving me mad. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/ Share on other sites More sharing options...
.josh Posted February 8, 2009 Share Posted February 8, 2009 ftp Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757650 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 Okayyyyy ... So, after a bit of Googling, I imagine the code could look something like this ... <?php $conn = ftp_connect("ftp.my_website.com") or die("Could not connect"); ftp_login($conn, $my_username, $my_password); // some bit in the middle to get into the right subfolder // some bit in the middle to copy every file across ftp_close($conn); ?> The $64,000 question (not literally, I'm not that rich) - what's the // some bit in the middle ?? Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757669 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 // signifies comments. It is not executed it is used to describe code or in debugging by commenting out possible problematic code. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757676 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 Yes, I DO know that. It was me that typed the comments in. Now - please - what proper code goes in the place of my comments. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757679 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 http://us.php.net/manual/en/ref.ftp.php It all depends on what you want to do. All the information you need is provided there. I would expect, you will want to ftp_rawlist then loop through that and use ftp_fget or ftp_get and save the files you want via that way. Give it a try and see what you can come up with. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757693 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 SO NEAR AND YET SO FAR Thanks premiso, I'm sure I'm on the right track now !! I've managed to get the contents of the remote folder and hold the filenames in an array. When I run my code I get the following error message (one for each file in the subfolder) : Warning: ftp_get(localhost/xampp/xyz/thumbnails/mypicture.jpg) [function.ftp-get]: failed to open stream: No such file or directory in C:\xampp\htdocs\xyz\sync.php The PHP file in which I am running the code is calles "sync.php" (as you can see from the error message) and it is stored on my C drive in subfolder C:\xampp\htdocs\xyz\ in this folder is a further subfolder called "thumbnails" in which I want the downloaded image to go. I suspect all my proplems are around the right setting of the variable $destination. I've tried all sorts !! What should it be ?? !!! BIG THANKS FOR ANY HELP $conn_id = ftp_connect('my-website-name.com'); // open the connection $login_result = ftp_login($conn_id, $my_userid, $my_ftp_password); // login in the appropriate way // get the contents of the appropriate subfolder $list_of_files = ftp_nlist($conn_id, '/public_html/xyz/thumbnails/'); for ($jj = 0; $jj <= count($list_of_files); $jj++) { // list_of_files is an array of all the filenames if (!(($list_of_files[$jj] == '.') || ($list_of_files[$jj] == '..'))) { // exclude these two $destination = 'localhost/xampp/xyz/thumbnails/'.$list_of_files[$jj]; $ftp_success = ftp_get($conn_id, $destination, $list_of_files[$jj], FTP_BINARY); } } ftp_close($conn_id); // close the connection Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757733 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 The second param is supposed to be a stream. For example: $destination = fopen('localhost/xampp/xyz/thumbnails/'.$list_of_files[$jj], 'w'); Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757735 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 Getting closer !!! I amended the code and now I get a new error message : Warning: ftp_get() expects parameter 2 to be string, resource given in C:\xampp\htdocs\xyz\sync.php on line 74 (line 74 as you may have guessed is the line with the ftp_get() statement on it) One final push and we'll get there ! many thanks for all your help so far ! $conn_id = ftp_connect('my-website-name.com'); // open the connection $login_result = ftp_login($conn_id, $userid, $password); // login in the appropriate way // get the contents of the appropriate subfolder $list_of_files = ftp_nlist($conn_id, '/public_html/xyz/thumbnails/'); for ($jj = 0; $jj <= count($list_of_files); $jj++) { // list_of_files is an array of all the filenames if (!(($list_of_files[$jj] == '.') || ($list_of_files[$jj] == '..'))) { // exclude these two $destination = fopen('thumbnails/'.$list_of_files[$jj], 'w'); // relative path from where this php file is located $ftp_success = ftp_get($conn_id, $destination, $list_of_files[$jj], FTP_BINARY); } } ftp_close($conn_id); // close the connection Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757749 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 Oh wow.... I told you entirely wrong. Not sure why I thought the second param was supposed to be a stream, but it's not. Ignore my post earlier in other words. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757753 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 What a heavy bummer. Now I'm really stuck ! Anybody ... help ... PLEASE !! Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757759 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 Try echoing out all of the variables, and make sure they make sense. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757764 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 Hi corbin Thanks for the help. Now, re echoing the filenames - I've tried that and I do indeed get a list of all the files in the server directory. I did a loop with ... echo list_of_files[$jj].'<BR>' .... and success - at least insofar as it's getting the right filenames from off the server. No path name was in the array varibale, just a straight list of filenames including extensions like : eiffel_tower.jpg empire_state_building.jpg the_sphinx.jpg etc etc. So at least that bit is working ok Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757771 Share on other sites More sharing options...
grissom Posted February 8, 2009 Author Share Posted February 8, 2009 One further piece of info When I run the code, every file in my local "thumbnails" directory which shares the same name as a file on the server is wiped out ! Any file NOT sharing a name with any on the server is left undeleted. What's going on ?? Help !! Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757782 Share on other sites More sharing options...
grissom Posted February 9, 2009 Author Share Posted February 9, 2009 DONE IT !! results to follow in the next post Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757795 Share on other sites More sharing options...
grissom Posted February 9, 2009 Author Share Posted February 9, 2009 OKAY DONE IT. WHOO HOOOOOOO !!! Thanks to all who helped. Now for anybody else in the same boat as me - running xampp and wanting to download the contents of a server directory onto your C drive, don't tear out your hair, here is how to do it. The secret is in the pathnames : $conn_id = ftp_connect('my-website-name.com'); // open the connection $login_result = ftp_login($conn_id, $my_login_id, $my_ftp_password); // login in the appropriate way // get the contents of the appropriate subfolder $list_of_files = ftp_nlist($conn_id, '/public_html/xyz/thumbnails/'); // state the whole path for ($jj = 0; $jj <= count($list_of_files); $jj++) { // list_of_files is an array of all the filenames // exclude the three cases where they do not refer to an actual file if (!(($list_of_files[$jj] == '.') || ($list_of_files[$jj] == '..') || ($list_of_files[$jj] == ''))) { // destination is the RELATIVE path from where *this* php file is located (I'm using xampp so its on the C drive) $destination = 'thumbnails/'.$list_of_files[$jj]; // source parameter in the next line is the FULL ABSOLUTE pathname on the server $ftp_success = ftp_get($conn_id, $destination, '/public_html/xyz/thumbnails/'.$list_of_files[$jj], FTP_BINARY); } } ftp_close($conn_id); Note that because of the use of the parameter FTP_BINARY it only works with the right types of files (eg images). If it's a text file, you will need to change it for FTP_ASCII. Thanks once again to everybody who helped, especially crayon violent, premiso and corbin. Together we made it !! Thanks folks ! Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757802 Share on other sites More sharing options...
corbin Posted February 9, 2009 Share Posted February 9, 2009 Strange that binary transfers don't work on text files. That should work fine. Weird. Quote Link to comment https://forums.phpfreaks.com/topic/144386-solved-copydownload-images-from-folder/#findComment-757822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.