Drupal custom script and problem with file_copy, file_save, file_move and generally file functions.

I have recently been occupied with developing a custom script that uses drupal 7 api in order to programmatically create content nodes.
For all these i have found this very usefull post.

My nodes had to have many attachements so i was really happy that i had this code.

NOTE: i was executing my script by hand from a command line like this:

$php myscript.php

Despite doing everything correctly i couldn’t make file_copy() to succeed.

To make a long story short, the solution to this was to place my custom script in the root directory where my drupal site was running and then run it by hand again.
For security reasons of course i had to change the permissions to my script accordingly so that it couldn’t be executed from the web server but only by command line.

To make a short story long, the reason for the script not working originally had to do with drupal’s public:// stream handler producing relative paths
to the drupal installation directory, thus causing various checks like php’s is_dir(“public://”) functions to fail miserably when run from other place.

That was it.

Cheers!