Creating a file in the Drupal file system

This example shows how to create a file with a location within the Drupal file system.

if (!isset($node -> uc_product_image["und"][0])) {

    $filepath = "public://temp.jpg";
    $data = file_get_contents($row["frontcover"]);

    if (!empty($data)) {
        file_put_contents($filepath, $data);
        // echo "{$filepath}, ";
        $file = (object) array(
            'uid' => 2,
            'uri' => $filepath,
            'filemime' => file_get_mimetype($filepath),

            'status' => 1,

        );
        $file = file_copy($file, "public://productimages/{$id}.jpg");
        $node -> uc_product_image["und"][0] = (array)$file;
        unlink($filepath);
    }

}

Done

Leave a comment

Your email address will not be published. Required fields are marked *