Jump to content

[resolved]Function won't extract files.


fert

Recommended Posts

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: password
Author: Person
Created: Sat, 09 Dec 2006 12:24:35 -0800

'-------------------------------[file]--------------------------------
'-----------------------------[file metadata]------------------------
Filename: file.txt
Size: 12
Date added: Sat, 09 Dec 2006 12:24:35 -0800
File 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?
Link to comment
https://forums.phpfreaks.com/topic/30075-resolvedfunction-wont-extract-files/
Share on other sites

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 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 lines
and 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]
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.