{"id":6738,"date":"2014-04-23T01:29:39","date_gmt":"2014-04-23T01:29:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/file-field-not-getting-updated-collection-of-common-programming-errors\/"},"modified":"2014-04-23T01:29:39","modified_gmt":"2014-04-23T01:29:39","slug":"file-field-not-getting-updated-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/file-field-not-getting-updated-collection-of-common-programming-errors\/","title":{"rendered":"File field not getting updated-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5f10300b519b3778f7dedbe93fa685fb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPaniyar<\/p>\n<p>i tried to update file cck field of a content type as below from another node as below.<\/p>\n<pre><code>\n    \n    \n\n\n<\/code><\/pre>\n<p>file gets uploaded but results error in updating file field .<\/p>\n<pre><code>localhost\nError\nThe website encountered an unexpected error. Please try again later.\nError message\n\n    Notice: Undefined index: fid in file_field_presave() (line 219 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc).\n    Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController-&gt;load() (line 173 of C:\\xampp\\htdocs\\drupal\\includes\\entity.inc).\n    Notice: Trying to get property of non-object in file_field_presave() (line 220 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc).\n    Warning: Creating default object from empty value in file_field_presave() (line 221 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc).\n    Notice: Undefined property: stdClass::$uri in file_save() (line 601 of C:\\xampp\\htdocs\\drupal\\includes\\file.inc).\n    Notice: Undefined index: fid in file_field_update() (line 261 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc).\n    Notice: Undefined index: fid in file_field_update() (line 287 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc).\n    Notice: Undefined property: stdClass::$fid in file_usage_add() (line 685 of C:\\xampp\\htdocs\\drupal\\includes\\file.inc).\n    PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fid' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] =&gt; [:db_insert_placeholder_1] =&gt; file [:db_insert_placeholder_2] =&gt; node [:db_insert_placeholder_3] =&gt; 32 [:db_insert_placeholder_4] =&gt; 1 ) in file_usage_add() (line 692 of C:\\xampp\\htdocs\\drupal\\includes\\file.inc).\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3e9dfaa83ce8fab4e2ceffe355c19685?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAgA<\/p>\n<p>I guess you should use this way:<\/p>\n<pre><code>$file = file_save_data($image, 'public:\/\/' . $filename, FILE_EXISTS_RENAME);\n$node-&gt;field_ftest[LANGUAGE_NONE][0]=(array)$file;\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/91KDl.jpg?s=32&amp;g=1\" \/><br \/>\nxurshid29<\/p>\n<p>You should use Form API to create and manage file uploads and their management. You can do something like this instead:<\/p>\n<pre><code>...\n$form['file'] = array(\n    '#title' =&gt; t('File'),\n    '#type' =&gt; 'managed_file',\n    '#description' =&gt; t('File formats: %format. Maximum: %max_size. Resolution %resolution', array('%format' =&gt; 'Jpg, Jpeg, Png, Gif', '%max_size' =&gt; '800 Kb', '%resolution' =&gt; '80x100')),\n    '#upload_location' =&gt; 'public:\/\/',\n);\n...\n<\/code><\/pre>\n<p>and submit function:<\/p>\n<pre><code>...\n$node = node_load(32);\n$file_d = $form_state['values']['file'];\n$file = file_load($file_d);\n$file-&gt;status = FILE_STATUS_PERMANENT;\nfile_save($file);\nfile_usage_add($file, 'node', 'TYPE', $file-&gt;fid);\n$file_info = image_get_info($file-&gt;uri);\n\n$file_arr = array(\n  'fid' =&gt; $file-&gt;fid,\n  'alt' =&gt; '',\n  'title' =&gt; '',\n  'width' =&gt; $file_info['width'],\n  'height' =&gt; $file_info['height'],\n  'uid' =&gt; $file-&gt;uid,\n  'filename' =&gt; $file-&gt;filename,\n  'uri' =&gt; $file-&gt;uri,\n  'filemime' =&gt; $file-&gt;filemime,\n  'filesize' =&gt; $file-&gt;filesize,\n  'status' =&gt; 1,\n  'timestamp' =&gt; $file-&gt;timestamp,\n  'rdf_mapping' =&gt; array(),\n);\n\n$node-&gt;field_ftest[LANGUAGE_NONE][0] = $file_arr;\nnode_save($node);\n...\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Paniyar i tried to update file cck field of a content type as below from another node as below. file gets uploaded but results error in updating file field . localhost Error The website encountered an unexpected error. Please try again later. Error message Notice: Undefined index: fid in file_field_presave() (line 219 of C:\\xampp\\htdocs\\drupal\\modules\\file\\file.field.inc). Warning: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6738","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6738"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6738\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}