fert Posted December 9, 2006 Share Posted December 9, 2006 I have a function that is suppose to extract files that have been put in one file that has a syntax like this:[quote]'-----------------------------[metadata]-----------------------------Password: passwordAuthor: PersonCreated: Sat, 09 Dec 2006 12:24:35 -0800'-------------------------------[file]--------------------------------'-----------------------------[file metadata]------------------------Filename: file.txtSize: 12Date added: Sat, 09 Dec 2006 12:24:35 -0800File type: txt'-----------------------------[contents]-----------------------------hello 5645 2[/quote]To do this i created this function:[code]function extract_mff_files($filename,&$global,&$local){ $metadata; $file=@fopen($filename,"r") or die("Cannot open file"); $contents=@fread($file,filesize($filename)) or die("Cannot read file"); @fclose($file); $files=explode("\n'-------------------------------[file]--------------------------------\n",$contents); $files[0]=str_replace("'-----------------------------[metadata]-----------------------------\n","",$files[0]); $global_metadata=explode("\n",$contents[0]); $num=count($contents); for($count=1;$count<$num;$count++) { $temp=explode("'-----------------------------[contents]-----------------------------\n",$files[$count]); $temp[0]=str_replace("'-----------------------------[file metadata]------------------------\n","",$temp[0]); $metadata=explode("\n",$temp[0]); $metadata=array("filename"=>$metadata[1],"size"=>$metadata[2],"added"=>$metadata[3],"type"=>$metadata[4]); $tem=explode("\\",$metadata['filename']); $last=count($tem); $file=@fopen($tem[$last-1],"w") or die("Cannot create file"); @fwrite($file,$temp[1]) or die("Cannot write to file"); @fclose($file); } $global=$global_metadata; $local=$metadata;}[/code]to test this function i made this code[code]<?php require("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\includes\\includes.php"); extract_mff_files("test.mff",$t,$r); print_r($t); print_r($r);?>[/code]when i run the code i get this:[quote]Array ( [0] => ' )[/quote]and in the Apache error log there's this:[quote][Sat Dec 09 15:42:26 2006] [error] [client 127.0.0.1] PHP Notice: Undefined variable: metadata in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\includes\\includes.php on line 109[/quote]Can anybody offer me some help as to why this function doesn't work? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 10, 2006 Share Posted December 10, 2006 Set "$metadata" equal to something....Change:[code]function extract_mff_files($filename,&$global,&$local){ $metadata;[/code]to[code]function extract_mff_files($filename,&$global,&$local){ $metadata = "";[/code]Or $metadata = array(); Quote Link to comment Share on other sites More sharing options...
fert Posted December 10, 2006 Author Share Posted December 10, 2006 now it gives me[quote]Array ( [0] => ' ) Array ( )[/quote]and it still won't extract the files. Quote Link to comment Share on other sites More sharing options...
fert Posted December 10, 2006 Author Share Posted December 10, 2006 *bump Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 Does this work on any form of *nix based host? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 A couple issues.First off, on windows a newline is \n\r or \r\n, forgot the order, so your explodes are not working properly. Remove the \n and trim the results.Secondly, the line:[code=php:0]$global_metadata=explode("\n",$contents[0]);[/code]is exploding on $contents[0], which is just a single character. So you're not going to get anything useful out of it. Perhaps you meant $files[0]?I'll post more after I play with it some more. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 $num=count($contents);should most certainly be$num=count($files); Quote Link to comment Share on other sites More sharing options...
fert Posted December 10, 2006 Author Share Posted December 10, 2006 [quote author=roopurt18 link=topic=118017.msg482013#msg482013 date=1165736853]First off, on windows a newline is \n\r or \r\n, forgot the order, so your explodes are not working properly. Remove the \n and trim the results.[/quote]The way i've formatted the file is with '\n' between linesand when i change the code as you advised i get[quote]Array ( [0] => Password: password [1] => Author: person [2] => Created: Sat, 09 Dec 2006 12:24:35 -0800 [3] => ) Array ( [filename] => Size: 19 [size] => Date added: Sat, 09 Dec 2006 12:24:35 -0800 [added] => File type: xls [type] => )[/quote] Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 10, 2006 Share Posted December 10, 2006 All I can tell you is when I used the line:[code] $files=explode("\n'-------------------------------[file]--------------------------------\n",$contents);[/code]it didn't explode properly. Removing the \n from the first parameter to explode did explode properly.Take a look at the note in the manual for fopen:http://us3.php.net/fopen Quote Link to comment Share on other sites More sharing options...
fert Posted December 10, 2006 Author Share Posted December 10, 2006 Thanks for all your help i finally fixed the function. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.