Need somone to make a PHP script to transfer XML to drupal database.

The xml structure is following


<image>
<filename>spaladinum1.jpg</filename>
<caption>SPALADIUM ARENA 2008</caption>
</image>

<image>
<filename>somthin2.jpg</filename>
<caption>SOMETHING 2008</caption>
</image>

so you need to read filename and caption from XML and put it into "files" table in DB and also caption into description in "field_picture_data" row which is in "content_field_pictures" table.

1. File upload is not needed, only setting path in DB like "sites/default/files/".$filenameFromXml
2. All the other fields that are not in xml, will have fix value like "filemime - image/jpeg" and can be filled automaticly. FID in "files" needs to be incremented and "field_pic_fid" and "delta" in "content_filed_pictures", in a way that XML is sorted.

Send your estimate in $$ for this. Thanx

Comments

jh3’s picture

$sourcedir is the path to your .xml files
$filename is a direct path to the current .xml file being worked on

error_reporting(E_ALL);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$sourcedir = "./path/to/your/xml";
$mimetype  = "image/jpeg";

if ($handle = opendir($sourcedir)) {
// Set index to 0, used to populate array from directory list
$index = 0;
// Create empty array to store filenames in
$filearray = array();
	while (false !== ($f = readdir($handle))) {		
		if (($f != "." && $f != "..") && (substr($f,strlen($f)-4,4) == ".xml")) {
			if (!is_dir("$f")) {
				// Assigns each file to a position in the array
				$filearray[$index] = $f;
				// Increments the index variable by 1
				$index = $index + 1;
			}
		}
	}
	// Closes the file handler
	closedir($handle);
	while(current($filearray)) {
		$filename = $sourcedir.current($filearray);
		$xml = simplexml_load_file($filename) or die ("Unable to load XML file!");
		next($filearray);
		
		$file = new stdClass();		
		foreach ($xml->image as $img) {
			foreach ($img->filename as $fn) {
				$file->filename = $fn;
				$file->filepath = $sourcedir.$fn;
				// $file->filesize = filesize($sourcedir.$fn);
			}
		}
		$file->filemime = $mimetype;		
		$file->uid = 1;
		$file->status = FILE_STATUS_PERMANENT;
		$file->timestamp = time();
		drupal_write_record('files', $file);
		$file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));
		echo $filename . ' has been added to the files table.<br>';
		
		unset($filename);
		unset($xml);
		unset($img);
	}		
}

Here's something you can fool around with. Free of charge :P

Marko B’s picture

Thanx i realized that i need also filesize to put it into drupal table and i dont have it so i will have to manually enter data for either filesizes or put images by hand (upload) them. thanx on help and all who applied for job and sorry for cancelling it but there is no point in doing this conversion if it wont work.

ecstasy2’s picture

You can maybe Upload the files via ftp on your server, then you couold use the snipset gived by ejh3 to import your file.
I think this is easier than adding your file manually. But this is not a point if you do not have more than 50 files.

Marko B’s picture

thanx on ideas, i have 300 and more files. already did it manually, hope i could use script some other time :-)