Hi,

I want to download an image with my module. The code works as a extern php-skript, but not inside my module. No errors, nothing in apache's error.log.

Thats my_module.module:

// is called by cron by a node-address

function xml_import_menu() {
  $items = array();
	
  $items['xml_import'] = array(
    'title' => 'XML Import',
    // register the path - the path is OK
    'page callback' => 'xml_import',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}


function xml_import() {
	
	$imageName = "my_image.jpg";
        $imagePath = "http://test.com/test.jpg";

        // gets "bool(false)"
	var_dump(file_put_contents($imageName, file_get_contents($imagePath)));	
	
}

Comments

nevets’s picture

Some thoughts

Have you checked that file_get_contents($imagePath) works?

Using just my_image.jpg does not indicate where the file should be created and may be the problem.

jepster_’s picture

var_dump(file_get_contents($news_items['image_front']['0']));

brings

bool(false)

What reason could it be, that I cannot download a picture inside my drupal-module, but in a extern skript on the same webserver?

ales.blaznik’s picture

Try to specify absolute path to the file (module folders are not writable by default).

$imageName = file_directory_path() . '/myFolder/' . 'image.jpg';

jepster_’s picture

OK, I've fixed it. There were 2 bugs.

1., the download-path in the drupal file system was not defined
2., there were xml tags in der download-url. I've havent seen them in my dump, because firefox was displaying them only in the source.

thats it:

        $imageUrl = strip_tags($url);
	file_put_contents('/home/me/webs/dev-website/sites/default/files/'.$imageName, file_get_contents($imageUrl));