title: Uploading and saving a file programmatically to Drupal nodes
body: <p>I am trying to create a node based on a custom form submission. Everything works great except for the images that get uploaded.</p>
  
  <p>I can capture them fine and set them in the form object cache. When I pass the data into the function to create the node, I get this error:</p>
  
  <p>"The specified file could not be copied, because no file by that name exists. Please check that you supplied the correct filename."</p>
  
  <p>I also receive the error multiple times, despite only submitting one or two images at a time.</p>
  
  <p>Here is the code I am using. $uploads is passed in and is an array of file objects returned from file_save_upload() in a previous step:</p>
  
  <pre><code>if (isset($uploads)) {
      foreach ($uploads as $upload) {
        if (isset($upload)) {
          $file = new stdClass;
          $file-&gt;uid = 1;
          $file-&gt;uri = $upload-&gt;filepath;
          $file-&gt;filemime = file_get_mimetype($upload-&gt;uri);
          $file-&gt;status = 1;  
  
          $file = file_copy($file, 'public://images');
  
          $node-&gt;field_image[$node-&gt;language][] = (array) $file;
        }
      }
    }
  
    node_save($node);
  </code></pre>
  
  <p>I also tried this:</p>
  
  <pre><code>if (isset($uploads)) {
      foreach ($uploads as $upload) {
          $upload-&gt;status = 1;  
  
          file_save($upload);
  
          $node-&gt;field_image[$node-&gt;language][] = (array) $upload;
        }
      }
    }
  
    node_save($node);
  </code></pre>
  
  <p>The second causes a duplicate key error in MySQL on the URI field. Both of these examples I saw in tutorials, but neither are working?</p>