I added an Image Field to my Node content type. Now, when I upload an image "image1.jpg" that is attached to a node mysite.com/note/123, I want the uploaded image's path to be mysite.com/node/123/image1.jpg. How do I achieve this? I've tried to change the subfolder of the image with a token. I used the token [current-page:path], but that doesn't return the node where I'm attaching to, but the last page that was visited. (Which might be the node edit form or something completely different with ajax, say)
Seems you are looking for a File (Field) Paths module:
The File (Field) Paths module extends the default functionality of Drupal's core Upload module, the FileField module and many other File Upload modules by adding the ability to use node tokens in destination paths and filenames.
In simple terms, File (Field) Paths allows you to automatically sort and rename your uploaded files using token based replacement patterns to maintain a nice clean filesystem.
Be aware that it's not yet stable. Test intensively before using in production environment. On the other hand, I don't know anything better and I know for a fact it may work all right.
If you wanted a programmatic solution, you could use the setting array when creating image field instance:
'settings' => array( 'file_directory' => 'YOUR_DIRECTORY_HERE', )
// Create the instance. $instance = array( 'field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'article', 'label' => 'Article Image', 'description' => 'An image for the article.', 'required' => TRUE, 'settings' => array( 'file_extensions' => 'png jpg jpeg', 'file_directory' => 'myModule/news', //your wanted attribute 'max_filesize' => '20 MB', 'alt_field' => 1, 'title_field' => 1, 'max_resolution' => '300x200', 'min_resolution' => '200x125', 'default_image' => 0, ),