FTP File Harvest Failed: Unsupported File(s)?

darkmetroid - April 21, 2009 - 16:34
Project:Media Mover
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:arthurf
Status:needs review
Description

Well, I keep getting this error for some reason. I ran the FTP test and it hosted the test.png just fine and then deletes it. I then ran the actual config in Media Mover, it says 0 on the harvesting part, and then it shows this error in my logs.

FTP file harvest failed: unsupported file extension files/media_mover/mm_ftp/_0

I have a ton of files in my ftp, some something should show up on my list of Harvested files. Very odd, any ideas what might be wrong?

Thanks.

#1

arthurf - April 21, 2009 - 16:39

Do the file extensions on the files that you're trying to harvest from the FTP server match the extensions that you've allowed? The error message seems to indicate that it is a problem with your local directory, but the error message is from line 104 in media_mover_ftp_harvest.inc:

media_mover_api_file_validate_extensions($local_file, $configuration['mm_ftp_allowed_files'])

Note that the list of file extensions changed from comma separated to space separated (to be consistent with drupal) awhile back.

#2

darkmetroid - April 21, 2009 - 17:01

Yeah, my list goes:

jpg jpeg gif png txt html doc xls pdf ppt pps ico avi mpeg mpg mov rm flv wmv 3gp mp4 dir dcr ogg php

I have all sorts of jpgs, pngs and flv files in my folder. I changed the location of where it harvests my files to my home dir (just left it as a '.') to bypass any weird permission problems. Put a few images and flv's in the root of my server, and still nothing.

I have a fresh installation of Drupal 6.10 and Media Mover also.

I noticed whenever I had it trying to harvest files from a folder I made called, 'files', it gives me these 3 errors:


    * warning: ftp_fput() [function.ftp-fput]: Rename/move failure: No such file or directory in /home/harrahch/public_html/hc/sites/all/modules/media_mover/media_mover_ftp/mm_ftp_storage.inc on line 49.

    * FTP file harvest failed: unsupported file extension files/media_mover/mm_ftp/_0

    * There was a problem downloading the file from the server. You may need to check your permissions.

Been messing around with my permissions on that folder, have it set to all 7's along with that error that I am having now when I try to harvest files from my root folder.

Is there any other place Media Mover pules file permissions from that I might have to change?

EDIT: I tried it on our Cirtex host just now, if I use the home root location and run a test, it does the same thing, no errors posted after the host, but if I check the logs it tells me that, " FTP file harvest failed: unsupported file extension files/media_mover/mm_ftp/_0".

#3

arthurf - April 22, 2009 - 21:48
Component:User interface» Code
Assigned to:Anonymous» arthurf
Status:active» needs work

* warning: ftp_fput() [function.ftp-fput]: Rename/move failure: No such file or directory in /home/harrahch/public_html/hc/sites/all/modules/media_mover/media_mover_ftp/mm_ftp_storage.inc on line 49.

FYI you're using FTP storage, but that's not important.

Can you add drupal_set_message($file_name); on line 47 in mm_ftp_storage.inc? I'm guessing that for some reason, the $file_name is not being set.

#4

tachikoma5 - June 19, 2009 - 23:51

I appear to have the same issue with the ftp harvest, but it might be a one-off so thought some more info might help. Encountered the issue on media_mover-6.x-1.0-beta2,
then switched over to media_mover-6.x-1.x-dev with the same FTP file harvest doing nothing.

Tried to resolve or workaround this yesterday and today without success.

First tried to modified media_mover_harvest.inc
in the function media_mover_ftp_create_file
at line 106 added the following changes

  // If this is a folder, do nothing
  // http://gskinner.com/RegExr/
  $pattern_has_ext = "|\S+\.\S+|i";
  // A file without a file extension or a directory that slipped by
  $has_ext_flag = preg_match($pattern_has_ext, $ftp_file['filename']);
  watchdog(MM_FTP_WATCHDOG_TYPE, 'ext is matched, [!fe]', array('!fe' => $has_ext_flag), WATCHDOG_NOTICE);

  if (is_array($ftp_file['dir']) || (!$has_ext_flag)) {
    return;
  }

My change didn't really resolve the issue; the FTP file harvest was still not harvesting any of the files, it just sees the one directory.

Then modified media_mover_ftp.module
in the function media_mover_ftp_get_files(
on line 394 added
watchdog(MM_FTP_WATCHDOG_TYPE, 'Raw filelisting, [!fl]', array('!fl' => $ftp_file_listing), WATCHDOG_NOTICE);
which gives me
Message Raw filelisting, [drw-rw-rw- 1 USER GROUP 1 Jun 19 18:54 Catalog]

Its so odd it looks like ftp_rawlist is only returning a single record. At this point I was double checking the ftp server with FileZilla, and there were jpegs in the directory and its parent.
Tried to dig deeper, but kept getting lost in the code. It been awhile since I did anything complex in PHP, need to setup eclipse again.

Below is all the info that might help, was trying to harvest files from the /Catalog directory on a ftp server.
The following info was grabbed from media_mover-6.x-1.x-dev.

Original Recent log entry

Type MM FTP
Date Friday, June 19, 2009 - 15:33
User AdminJ5
Location http://##.###.#.##/drupal/admin/build/media_mover/config/1/run
Referrer
http://##.###.#.##/drupal/admin/build/media_mover/config/1/view
Message
FTP file harvest: unsupported file extension sites/default/files/media_mover/mm_ftp/Catalog
Severity notice
Hostname ##.###.#.##
Operations


Media Mover Configuration

$configuration = new stdClass;
$configuration->required = array('media_mover_ftp', 'media_mover_api', 'media_mover_api', 'mm_dir');
$configuration->name = 'ftp harvest catalog';
$configuration->cid = '1';
$configuration->description = 'ftp harvest catalog';
$configuration->start_time = '1245450835';
$configuration->last_start_time = '1245450835';
$configuration->status = 'stopped';
$configuration->settings->mma_file_perm = '0';
$configuration->settings->mma_file_mask = '0644';
$configuration->settings->mma_node_edit_item_show = '0';
$configuration->settings->mma_node_item_delete = '1';
$configuration->settings->mma_node_config_rss = '0';
$configuration->settings->mma_cron_notify = '1';
$configuration->settings->mma_cron_notify_email = '';
$configuration->settings->mma_cron_notify_time = '10';
$configuration->settings->mma_process_num = '10';
$configuration->settings->mma_storage_num = '10';
$configuration->settings->mma_complete_num = '10';
$configuration->harvest->module = 'media_mover_ftp';
$configuration->harvest->action = '1';
$configuration->harvest->configuration =  array(
  'mm_ftp_user' => '',
  'mm_ftp_pass' => '',
  'mm_ftp_host' => '',
  'mm_ftp_dir' => '/Catalog',
  'mm_ftp_allowed_files' => 'mov mp4 mp3 avi 3gp 3g2 divx xvid wmv asf mpg mpeg mpeg4 jpeg',
  'mm_ftp_harvest_without_tokens' => '1',
  'mm_ftp_delete_files' => '0',
  'mm_ftp_delete_files_no_token' => '0',
  'mm_ftp_harvest_depth' => '2',
  'mm_ftp_user_from_dir' => '0',
  'mm_ftp_harvest_folders' => '0',
  'mm_ftp_harvest_delete_folders' => '0',
  'mm_ftp_harvest_delete_non_token_folders' => '0',
  'cid' => '1',
  'module' => 'media_mover_ftp',
  'action' => '1',
  'verb' => 'harvest'
);
$configuration->harvest->cid = '1';
$configuration->process->module = 'media_mover_api';
$configuration->process->action = '1';
$configuration->process->configuration =  array(
  'cid' => '1',
  'module' => 'media_mover_api',
  'action' => '1',
  'verb' => 'process'
);
$configuration->process->cid = '1';
$configuration->storage->module = 'media_mover_api';
$configuration->storage->action = '1';
$configuration->storage->configuration =  array(
  'cid' => '1',
  'module' => 'media_mover_api',
  'action' => '1',
  'verb' => 'storage'
);
$configuration->storage->cid = '1';
$configuration->complete->module = 'mm_dir';
$configuration->complete->action = '3';
$configuration->complete->configuration =  array(
  'path' => 'sites/default/files/',
  'cid' => '1',
  'module' => 'mm_dir',
  'action' => '3',
  'verb' => 'complete'
);
$configuration->complete->cid = '1';
$configuration->hierarchy->parent = '0';

#5

arthurf - June 19, 2009 - 23:59

I wonder if the harvest depth is not working properly- can you set it to 0 and see if that solves the issue? Also are you sure that you have files in that directory that match your extension list (I've been bitten by this one more than once)? Finally, is your FTP server windows or a *nix based server? I have not tried the ftp module on a windows server, so unfortunately I can't report much about it.

#6

tachikoma5 - June 20, 2009 - 00:44

With 6.x-1.x-dev I tried the different File harvest depth: for each value without any success.
"All levels" 'mm_ftp_harvest_depth' => '-1',
"1, Top Level,default" 'mm_ftp_harvest_depth' => '1',
"2 levels" 'mm_ftp_harvest_depth' => '2',
"3 Levels" 'mm_ftp_harvest_depth' => '3',

While the 6.x-1.0-beta2 didn' have a depth and had the same issue, when I tried it first yesterday.

'mm_ftp_allowed_files' => 'mov mp4 mp3 avi 3gp 3g2 divx xvid wmv asf mpg mpeg mpeg4 jpeg',
I have a few files in the directory(~12) all jpegs. Example lantern_modern_quarter.jpeg
-rw-rw-rw- 1 USER GROUP 168430 Jun 12 14:53 lantern_modern_quarter.jpeg

I think the FTP server is nix.
SYST Return system type.

Grabbed from FileZilla
Command: SYST
Response: 215 UNIX L8
Command: FEAT
Response: 211-Supported features
Response: SIZE
Response: MDTM
Response: LANG EN*
Response: 211 End

Is there anything I missed?

#7

arthurf - June 20, 2009 - 00:53

Can you run the FTP test (admin/build/media_mover/tests/mm_ftp ) and see if it is able to get a file back and forth? There were some major changes to the FTP module between beta 1 and the most recent dev. I used some contributed code to set up recursive harvesting which maybe the root of the issue, however if beta1 was not working for you, perhaps not.

You may also want to look at this function: media_mover_ftp_harvest() (media_mover_ftp_harvest.inc) and do a print_R($connection); to see if it is finding any files at all. If that is null, try doing a print_r(ftp_raw_list($connection['ftp_connection'])); which should give the list of files in the directory without all the recursive checking and or file filtering.

#8

tachikoma5 - June 20, 2009 - 01:10

print_R($connection);

only gave me

Array ( [ftp_connection] => Resource id #157 [ftp_login] => 1 [ftp_files] => Array ( [0] => Array ( [filename] => Catalog [filesize] => 1 [created] => 1245469260 [filepath] => /Catalog/Catalog [user_name] => Catalog ) ) )

#9

arthurf - June 20, 2009 - 01:14

Can you try print_r(ftp_raw_list($connection['ftp_connection'])); ? What you show above seems to indicate that the files it is handing back is malformed I think- it's setting the path it sees as /Catalog/Catalog which does not seem right to me.

#10

tachikoma5 - June 20, 2009 - 01:18

print_R(ftp_rawlist($connection['ftp_connection'],'/Catalog'));

only gave me, sigh
Array ( [0] => drw-rw-rw- 1 USER GROUP 1 Jun 19 20:55 Catalog )

#11

tachikoma5 - June 20, 2009 - 01:23

double checking from terminal with my 2nd PC
I pulled all the other files out and left only one
Remote system type is UNIX.
ftp> ls
200 PORT command OK.
150 Opening ASCII mode data connection.
drw-rw-rw- 1 USER GROUP 1 Jun 19 20:59 Catalog
ftp> cd Catalog
250 CWD command successful
ftp> ls
200 PORT command OK.
150 Opening ASCII mode data connection.
-rw-rw-rw- 1 USER GROUP 168430 Jun 12 14:53 lantern_modern_quarter.jpeg
226 Transfer complete.

#12

tachikoma5 - June 20, 2009 - 01:27

I am getting dragged to BBQ and a Street Fighter 4 tournament, will be back in a few mins.

#13

arthurf - June 20, 2009 - 01:35

Ok this is strange- the ftp_rawlist is the php ftp client- it should give you the full list of files. So it seems like maybe the path is wrong? Can you change the directory path from /Catalog to just Catalog?

#14

tachikoma5 - June 20, 2009 - 01:50

got some food and a little beer, since I suck at Street Fighter 4 changed
print_R(ftp_rawlist($connection['ftp_connection'],'Catalog'));

is this right, it is still
Array ( [0] => drw-rw-rw- 1 USER GROUP 1 Jun 19 21:27 Catalog )

#15

tachikoma5 - June 20, 2009 - 02:02

Tried another directory which is deep in the ftp server were I had some .jpg files this time
print_R(ftp_rawlist($connection['ftp_connection'],'/X_CAT/_Project_/Electronics/Headphones/X_Catalog'));

Array ( [0] => drw-rw-rw- 1 USER GROUP 1 Jun 19 21:39 X_Catalog )

#16

tachikoma5 - June 20, 2009 - 02:05

I am calling it a night, let me know if there is any info you need.
time for more beer :(

#17

arthurf - June 22, 2009 - 12:33

Something is amiss. The command above is the standard php ftp client- it should work if you have the correct directory. You can experiment with this code:

<?php
  $connection
= ftp_connect('your.host.com');
 
$login = ftp_login($connection, 'your_user', 'your_pass');
 
print_R(ftp_rawlist($connection, 'path', true);
?>

This will recursively list any files in your directory that php FTP can read.

#18

tachikoma5 - June 22, 2009 - 17:47

Today I will contact the guys who setup the FTP Server, and confirm the stats/settings.
Something is amiss, either in the standard php ftp client or in the ftp server itself.

I tried your suggestion, commented out and replaced the code with
...
print_R(ftp_rawlist($connection, '/X_CAT/_Project_/Props/Electronics', true));
it is still returning only a since record set
Array ( [0] => drw-rw-rw- 1 USER GROUP 1 Jun 22 12:45 Electronics )

I can dig deeper with the ftp server and get more details today.

Also before I forget. When I run the FTP Test
* Connected to FTP server
* Copied file to: ########.com/./Catalog/test.png
# FTP file harvest: unsupported file extension sites/default/files/media_mover/mm_ftp/._0
# There was a problem downloading the file from the server. You may need to check your permissions.

Confirmed it was able to put the test.png on the ftp server but that is all MM_FTP can do.

But when I use FileZilla I can add and delete the test.png without any issues.

Is there anything in the php info that would be useful?
PHP Version 5.2.6
FTP support enabled

If I get time, I will setup another ftp server myself, try to get a confirmation if this only the one ftp server, or if php is doing this for all the ftp servers I try to harvest from.

#19

tachikoma5 - June 22, 2009 - 21:38

Quickly setup a MM config and harvested from ftp.swpc.noaa.gov using anonymous user without any problems. Exported and the info is below, I just removed my email info. Still trying to get more info on the setup of the FTP Server, trying to find a non-default setting, that might be a cause. Are there any know issues with php 5 ftp_rawlist?
Tried using the ftp_pasv http://w3schools.com/PHP/func_ftp_pasv.asp, but didn't seem to make any difference.

Any ideas, any info that might help?

$configuration = new stdClass;
$configuration->required = array('media_mover_ftp', 'media_mover_api', 'media_mover_api', 'mm_dir');
$configuration->name = 'swpcnoaagov';
$configuration->cid = '3';
$configuration->description = 'swpcnoaagov';
$configuration->start_time = '1245705385';
$configuration->last_start_time = '1245705304';
$configuration->status = 'stopped';
$configuration->settings->mma_file_perm = '0';
$configuration->settings->mma_file_mask = '0644';
$configuration->settings->mma_node_edit_item_show = '0';
$configuration->settings->mma_node_item_delete = '1';
$configuration->settings->mma_node_config_rss = '0';
$configuration->settings->mma_cron_notify = '1';
$configuration->settings->mma_cron_notify_email = 'myemail@myeyesonly.com';
$configuration->settings->mma_cron_notify_time = '10';
$configuration->settings->mma_process_num = '10';
$configuration->settings->mma_storage_num = '10';
$configuration->settings->mma_complete_num = '10';
$configuration->storage->module = 'media_mover_api';
$configuration->storage->action = '1';
$configuration->storage->configuration =  array(
  'cid' => '3',
  'module' => 'media_mover_api',
  'action' => '1',
  'verb' => 'storage'
);
$configuration->storage->cid = '3';
$configuration->process->module = 'media_mover_api';
$configuration->process->action = '1';
$configuration->process->configuration =  array(
  'cid' => '3',
  'module' => 'media_mover_api',
  'action' => '1',
  'verb' => 'process'
);
$configuration->process->cid = '3';
$configuration->harvest->module = 'media_mover_ftp';
$configuration->harvest->action = '1';
$configuration->harvest->configuration =  array(
  'mm_ftp_user' => 'anonymous',
  'mm_ftp_pass' => 'anonymous',
  'mm_ftp_host' => 'ftp.swpc.noaa.gov',
  'mm_ftp_dir' => '/pub/gallery/HollomanMisc/stamp',
  'mm_ftp_allowed_files' => 'mov mp4 mp3 avi 3gp 3g2 divx xvid wmv asf mpg mpeg mpeg4 jpeg txt gif',
  'mm_ftp_harvest_without_tokens' => '1',
  'mm_ftp_delete_files' => '0',
  'mm_ftp_delete_files_no_token' => '0',
  'mm_ftp_harvest_depth' => '1',
  'mm_ftp_user_from_dir' => '0',
  'mm_ftp_harvest_folders' => '1',
  'mm_ftp_harvest_delete_folders' => '0',
  'mm_ftp_harvest_delete_non_token_folders' => '0',
  'cid' => '3',
  'module' => 'media_mover_ftp',
  'action' => '1',
  'verb' => 'harvest'
);
$configuration->harvest->cid = '3';
$configuration->complete->module = 'mm_dir';
$configuration->complete->action = '3';
$configuration->complete->configuration =  array(
  'path' => 'sites/default/files/media_mover/mm_ftp',
  'cid' => '3',
  'module' => 'mm_dir',
  'action' => '3',
  'verb' => 'complete'
);
$configuration->complete->cid = '3';
$configuration->hierarchy->parent = '0';

#20

tachikoma5 - June 24, 2009 - 00:03

I don't know the why of any this. But here is my discovery from today, and the workaround that seems to work.
In media_mover_ftp.module in the function media_mover_ftp_completed_file_list.
I had to break up the line for
ftp_rawlist($connection, $directory);
which the ftp server will return only the one line for the $directory itself.

When I use

$dir_changed = ftp_chdir($connection, $directory);
ftp_rawlist($connection, '')

The harvest works perfectly fine, and the ftp_rawlist gives me the full list of files.

Here is my version of the function.

function media_mover_ftp_completed_file_list(&$connection, $directory, $strict = FALSE) {
$results = array();

$dir_changed = ftp_chdir($connection, $directory);
  // get 2 listings 1 second appart. If there are no results, exit 
//  if (! $pass1 = ftp_rawlist($connection, $directory) ) {
  if (! $pass1 = ftp_rawlist($connection, '') ) {
  return array();
  }
  // wait for a moment to get the second listing.
  sleep(1);
//  $pass2 = ftp_rawlist($connection, $directory);
  $pass2 = ftp_rawlist($connection, '');
 
  // get a list of only the results in both listings (ie fully uploaded files)
  $results = array_intersect($pass1, $pass2);

  // set results to false if in there is a difference and in strict mode
  if ($strict AND count($result) !== count($pass2) || ! count($result)) {
    $results = FALSE;
  }

  return $results;
}

#21

jasonn1234 - July 9, 2009 - 22:04

FYI - I can confirm that I have the same issue as darkmetroid on both 6.x-1.0-beta2 and 6.x-1.x-dev. My configuration (user/pass details changed) below:

$configuration = new stdClass;
$configuration->required = array('media_mover_ftp', 'media_mover_api', 'mm_dir', 'media_mover_api');
$configuration->name = 'Apartments.com Data Feed';
$configuration->cid = '1';
$configuration->description = 'Apartments.com Data Feed from datatransfer.cj.com';
$configuration->status = 'stopped';
$configuration->harvest->module = 'media_mover_ftp';
$configuration->harvest->action = '1';
$configuration->harvest->configuration = array(
'mm_ftp_user' => 'myuser',
'mm_ftp_pass' => 'mypasswd',
'mm_ftp_host' => 'remote.server.com',
'mm_ftp_dir' => '/1570981/outgoing/productcatalog/30204',
'mm_ftp_allowed_files' => 'mov mp4 mp3 avi 3gp 3g2 divx xvid wmv asf mpg mpeg mpeg4 jpeg txt gz',
'mm_ftp_harvest_without_tokens' => '1',
'mm_ftp_delete_files' => '0',
'mm_ftp_delete_files_no_token' => '0',
'cid' => '1',
'module' => 'media_mover_ftp',
'action' => '1',
'verb' => 'harvest'
);
$configuration->harvest->cid = '1';
$configuration->process->module = 'media_mover_api';
$configuration->process->action = '1';
$configuration->process->configuration = array(
'cid' => '1',
'module' => 'media_mover_api',
'action' => '1',
'verb' => 'process'
);
$configuration->process->cid = '1';
$configuration->storage->module = 'mm_dir';
$configuration->storage->action = '2';
$configuration->storage->configuration = array(
'path' => '/newhousing/drupal/sites/default/files/datafeed',
'cid' => '1',
'module' => 'mm_dir',
'action' => '2',
'verb' => 'storage'
);
$configuration->storage->cid = '1';
$configuration->complete->module = 'media_mover_api';
$configuration->complete->action = '1';
$configuration->complete->configuration = array(
'cid' => '1',
'module' => 'media_mover_api',
'action' => '1',
'verb' => 'complete'
);
$configuration->complete->cid = '1';
$configuration->hierarchy->parent = '0';

#22

jasonn1234 - July 10, 2009 - 00:53
Category:support request» bug report
Status:needs work» needs review

Okay, I did a little digging and found a solution - although I'm not quite sure of the base cause of the problem. See patch.

It appears that the "FTP file harvest failed: unsupported file extension files/media_mover/mm_ftp/_0" error was a result of php's ftp_rawlist() returning a list files in the current working directory rather than those of the argument it's passed. Since the working directory isn't changed, the contents of the home directory are returned. (In my case, the home directory only has other directories in it - thus the unsupported file extension error.)

This problem - ftpraw() returning only working directory contents rather than those of the $directory argument - only happens on some servers and I'm not sure why.

The patch just changes the following in media_mover_ftp.module:

  $connection['ftp_connection'] = ftp_connect($configuration['mm_ftp_host']);
  $connection['ftp_login'] = ftp_login($connection['ftp_connection'], $configuration['mm_ftp_user'], $configuration['mm_ftp_pass']);
  $connection['ftp_files'] = media_mover_ftp_get_files($connection['ftp_connection'], $configuration, $configuration['mm_ftp_dir'], $configuration['mm_ftp_harvest_depth']);

...to the following (inserts a ftp_chdir() to change the working directory to the $configuration['mm_ftp_dir'])

  $connection['ftp_connection'] = ftp_connect($configuration['mm_ftp_host']);
  $connection['ftp_login'] = ftp_login($connection['ftp_connection'], $configuration['mm_ftp_user'], $configuration['mm_ftp_pass']);
  $connection['ftp_pwd'] = ftp_chdir($connection['ftp_connection'], $configuration['mm_ftp_dir']);
  $connection['ftp_files'] = media_mover_ftp_get_files($connection['ftp_connection'], $configuration, $configuration['mm_ftp_dir'], $configuration['mm_ftp_harvest_depth']);

Once this is done the file list from the configured directory is returned instead of the contents of the home directory, and the harvest completes as expected.

AttachmentSize
media_mover_ftp.module.patch 751 bytes

#23

jasonn1234 - July 10, 2009 - 05:04

For reference - I also found the following note which seems to back up this method here:

http://us2.php.net/manual/en/function.ftp-rawlist.php

/*
www.bosssftp.com
07-Nov-2008 10:43
Some FTP servers only allow you to get list of files under current working directory. So if you always get result as empty array (array(0){ }), try changing the cwd befor get the list:
*/

<?php
function ftprawlist($connid, $dir) {
 
ftp_chdir($connid, $dir);
  return
ftp_rawlist($connid, "-a");
}
?>

#24

arthurf - July 10, 2009 - 14:42

So it seems that a refactor is in order to use ftp_chdir based on the $dir argument. I need to take a look and how this will effect the ftp_rawlist wrapper that is being used.

#25

jasonn1234 - July 12, 2009 - 21:10

Right... good call. I moved the ftp_chdir() to the ftp_rawlist wrapper that is being used. How does this look? Patch of 6.x-1.x-dev included.

function media_mover_ftp_completed_file_list(&$connection, $directory, $strict = FALSE) {
$results = array();

  // change working directory to $directory, since some FTP servers only allow you to get list of files under current working directory
  ftp_chdir($connection, $directory);

  // get 2 listings 1 second appart. If there are no results, exit 
  if (! $pass1 = ftp_rawlist($connection, $directory) ) {
  return array();
  }
  // wait for a moment to get the second listing.
  sleep(1);
  $pass2 = ftp_rawlist($connection, $directory);
 
  // get a list of only the results in both listings (ie fully uploaded files)
  $results = array_intersect($pass1, $pass2);

  // set results to false if in there is a difference and in strict mode
  if ($strict AND count($result) !== count($pass2) || ! count($result)) {
    $results = FALSE;
  }

  return $results;
}

AttachmentSize
media_mover_ftp-2.module.patch 641 bytes

#26

arthurf - July 12, 2009 - 23:58

This looks fine to me. The one other consideration is that the current working directory will need to be reset after these operations are done- otherwise assumptions about the ftp stream could be wrong. If $directory in media_mover_ftp_completed_file_list() is not an absolute path on the ftp server, the ftp_chdir is going to start messing things up (I think). So I think we need to store the current working directory before the ftp_chdir happens and then reset it after the files are returned. So long that $directory is at least the full path relative to the user's root, I think this should work.

Any thoughts?

#27

jasonn1234 - July 16, 2009 - 23:13

Sorry for taking so long to respond - I just realized that I don't know how to subscribe to an issue.

Anyways - yes, good point. Allright - added in a var to store the original pwd, then also added a check to see if $directory is an absolute path. Then before the function is returned, there is a check to see if the working dir got changed, in which case it switches back to the original.

Also added a bit of help text to the ftp form stating "...Absolute paths may be more compatible with some ftp servers."

/**
* A wrapper for the ftp_rawlist() php function to only return files which have
* finished uploading.
* It does this by calling ftp_rawlist twice with a 1 second pause, then checking
* the results for inconsitancies (uploading file will have increased in size).
*
* @param resource $connection
* @param string $directory
* @param bool $strict
*  If strict is TRUE the if any files are still uploading then false is returned
* @return array
*/
function media_mover_ftp_completed_file_list(&$connection, $directory, $strict = FALSE) {
  $results = array();

  // store the present working directory before we change it
  // and check that $directory is absolute
  if (($orig_pwd = ftp_pwd($connection)) && (drupal_match_path($directory,'/*'))) {
    // change working directory to $directory, since some FTP servers only allow you to get list of files under present working directory
    $chdir = ($directory != $orig_pwd) ? ftp_chdir($connection, $directory) : FALSE;
  }
 
  // get 2 listings 1 second appart. If there are no results, exit 
  if (! $pass1 = ftp_rawlist($connection, $directory) ) {
    if($chdir){
      // change working directory back to original if changed above
      ftp_chdir($connection, $orig_pwd);
    }
    return array();
  }
 
  // wait for a moment to get the second listing.
  sleep(1);
  $pass2 = ftp_rawlist($connection, $directory);
 
  // get a list of only the results in both listings (ie fully uploaded files)
  $results = array_intersect($pass1, $pass2);

  // set results to false if in there is a difference and in strict mode
  if ($strict AND count($result) !== count($pass2) || ! count($result)) {
    $results = FALSE;
  }

  // change working directory back to original if changed above
  if($chdir){
    ftp_chdir($connection, $orig_pwd);
  }

  return $results;
}

...and

  $form['mm_ftp_conf']['mm_ftp_dir'] = array(
    '#title' => t('FTP directory'),
    '#type' => 'textfield',
    '#description' => t('Enter "." for the homedir. No trailing slash. Absolute paths may be more compatible with some ftp servers.'),
    '#default_value' => $configuration['mm_ftp_dir'] ? $configuration['mm_ftp_dir'] : '.',
  );

AttachmentSize
media_mover_ftp-3.module.patch 2.27 KB

#28

jasonn1234 - July 24, 2009 - 22:27

Bump...

How does the above patch look to you, Arthur? I'm more than happy to refactor / fix anything here to make it commit-able. The only thing here I'm not so sure about upon review is the:

drupal_match_path($directory,'/*')

...which checks to make sure that $directory is absolute before going ahead with the ftp_chdir(). Maybe there's no need for it - and maybe there are cases where we still want the chdir to work whether or not an absolute path was given.

Any thoughts?...

 
 

Drupal is a registered trademark of Dries Buytaert.