would be a way to insert an upload file to the database?

this way these files would be available to all other modules/nodes to use them. Some times it would be better than removing them and they would stop appearing in the log.

thanks.

Comments

Stuart Greenfield’s picture

Can you clarify what you mean here? The files table links files to nodes, so that the files on the system have a node that owns them. If the file appears in the report it means that the connection is broken. If you were to "re-attach" files then you would need to choose the node that you want to link to and you wouldn't really know where to put it!

The best option in this case might be to download the file using FTP, and then edit the relevant node and re-upload the file that way?

If I have missed something then if you can explain a little further how you might see this working then I can understand how this might work.

Hope that's ok!

enboig’s picture

Status: Active » Closed (fixed)

Sorry, I didn't think carefully before writting. I will explain my situation:

I am using IMCE to attach images and files to nodes, but these files aren't really associated to the node, if I remove a node, the file isn't removed. So I was thinking about a way to insert these files to the nodes which are using them. This way it would be easy to remove files.
After reading more and thinking about it I think maybe it would be IMCE who should use uploaded files in the node...

The good things and bad of every alternative are:

* IMCE use attached files:
+ seems easy to code.
+ it could use "inline" or "Path Filter" so if you move your site to another location everything works ok.
- you are limited to upload files, you cannot use FTP to upload big files (now you can do it).
- you cannot share files across nodes

* Audit files attach used files to the nodes using it:
? I don't know if a file can be "belonged" by more than one node.
+ you could upload big files with ftp and they would be used by any node.
+ if a file is used twice or more, you could save space; and if you have to change the uploaded file, you just have to do it once.
- I think it would be hard to code.

mmmm..... these are my thoughts, any idea is wellcome.

acondiff’s picture

I am in the exact same boat as you are in. I am using IMCE, and I want to be able to write uploaded FTP files to the database. Sorry for opening an old thread however if anyone has any suggestions that would be great! Thanks.

acondiff’s picture

So I found a solution. In imce.inc in the filefield_sources module, instead of this:

  foreach ($directory['files'] as $filename => $file) { 
    if (!isset($db_files[$filename])) { 
			unset($directory['files'][$filename]);
			$directory['dirsize'] -= $file['size']; 

    } 
  }
  return $directory;
}

put this:

  foreach ($directory['files'] as $filename => $file) { 
    if (!isset($db_files[$filename])) { 
			//unset($directory['files'][$filename]);
			//$directory['dirsize'] -= $file['size']; 
			$mime_type = file_get_mimetype($filename);
			$sql = "INSERT INTO files(uid, filename, filepath, filemime, filesize, status, timestamp)
			VALUES('1', '" . $filename . "', '" . $sql_dir_name. "/" . $filename . "', '" . $mime_type . "', '" . $file["size"] . "', '1', '" . $file["date"] . "')";
			//echo $sql . "<br><br>";
			db_query($sql);
    } 
  }
  return $directory;
}

So this basically creates a database entry for new files that are not in the database. So now when I click insert it inserts the image, although, whenever I go to save my node I get this error: "Referencing to the file used in the field is not allowed."

Does anyone know what is wrong here?

Thanks.

acondiff’s picture

Ok I found a solution for that as well so it does not throw the error. In the filefield module, I just commented out this block of code and it works like a charm.

if ($references['filefield'] == 0) {
          form_error($element, t('Referencing to the file used in the %field field is not allowed.', array('%field' => $element['#title'])));
}
nuthman’s picture

Using Filefield 6.x-3.7

My version was slightly different. I found and commented out the code block under the filefield module in filefield_widget.inc and it looked like this:

if (field_file_references($file) == 0) {
form_error($element, t('Referencing to the file used in the %field field is not allowed.', array('%field' => $element['#title'])));
}

commenting that out worked for me! Thanks

giginos’s picture

Hi acondiff,
thanks for shareing your code.
I got the stuff working on Drupal 7 while changeing the module-file filefield_sources/sources/inc/imce.inc

Hope this helps someone:


/**
 * Scan directory and return file list, subdirectories, and total size.
 */
function filefield_source_imce_custom_scan($dirname, $imce) {
  // Get a list of files in the database for this directory.
  $scheme = variable_get('filefield_source_imce_scheme', 'public');
  $imce['scheme'] = $scheme;
  $sql_uri_name = $dirname == '.' ? $scheme . '://' : $scheme . '://' . $dirname;

  $result = db_select('file_managed', 'f')
    ->fields('f', array('filename'))
    ->condition('f.uri', $sql_uri_name . '/%', 'LIKE')
    ->condition('f.uri', $sql_uri_name . '/%/%', 'NOT LIKE')
    ->execute();

  $db_files = array();
  foreach ($result as $row) {
    $db_files[$row->filename] = $row->filename;
  }

  // Get the default IMCE directory scan, then filter down to database files.
  $directory = imce_scan_directory($dirname, $imce);
  foreach ($directory['files'] as $filename => $file) {
    if (!isset($db_files[$filename])) {
      #unset($directory['files'][$filename]);
      #$directory['dirsize'] -= $file['size'];
	  
      $mime_type = file_get_mimetype($filename);
	  
	  $id = db_insert('file_managed')
		->fields(array(
			'uid' => 1,
			'filename' => $filename,
			'uri' => $sql_uri_name . "/" . $filename,
			'filemime' => $mime_type,
			'filesize' => $file['size'],
			'status' => 1,
			'timestamp' => $file['date']
			))->execute();
	  if($id > 0) {
		$sql = "INSERT INTO file_usage (fid, module, type, id, count) VALUES(" . $id . ", 'file', 'node', 1, 1)";
		db_query($sql);
	  }
	  
    }
  }

  return $directory;
}

Cheers,
Rafael Kutscha