Jump to content

[SOLVED] copy/download images from folder


grissom

Recommended Posts

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.

Link to comment
Share on other sites

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 ??

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 !!

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.