Jump to content

FILE UPLOADER with Find/Replace


kvnmck18

Recommended Posts

[before I start this is in PHP 4]This is a file uploader, it's being used to upload Excel .XML data sheets and turn them into straight XML by removing: "<?mso-application progid=\"Excel.Sheet\"?>"; "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""; "xmlns:o=\"urn:schemas-microsoft-com:office:office\""; "xmlns:x=\"urn:schemas-microsoft-com:office:excel\""; "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\""; "xmlns:html=\"http://www.w3.org/TR/REC-html40\""; "o:"; "x:"; "ss:"; "html:"; "urn:";I just can't seem to be able to remove these and get the file to save to the directory. I've tried a lot of different things to try to get this to work. I can't get file_get_contents() to work (that would have solved my problem, I think). Here is what the code I'm using to remove the code mentioned above:

				  $find[] = "<?mso-application progid=\"Excel.Sheet\"?>";				  $find[] = "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"";				 $find[] = "xmlns:o=\"urn:schemas-microsoft-com:office:office\"";				 $find[] = "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"";				 $find[] = "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"";				 $find[] = "xmlns:html=\"http://www.w3.org/TR/REC-html40\"";				 $find[] = "o:";				 $find[] = "x:";				 $find[] = "ss:";				 $find[] = "html:";				 $find[] = "urn:";											$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$replace[] = "";					$tox2 = str_replace($find, $replace, $thisVAR);

I need to incorporate that above code in the below code. So when I upload a file it reads the content, removes "the above" and then saves it.

$uploaddir = 'App_Data/';							$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);															if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {										$input = '																<file location="' . htmlspecialchars($uploadfile, ENT_QUOTES) . '" 													name="' . htmlspecialchars($_FILES['userfile']['name'], ENT_QUOTES) . '" 													DATEadded="' .date('ymd F Y'). '"/> 																						  ';											$find[] = ".xml";																$replace[] = "";										$tox = str_replace($find, $replace, $input);																					$args = array('input' => $tox);																					$xmlfile = "directory_x.xml";										$xslfile = "update_s.xsl";																					$engine = xslt_create();										$output = xslt_process($engine, $xmlfile, $xslfile, NULL, NULL, $args);																					unlink($xmlfile);																					$handle = fopen($xmlfile, "w+");																					fwrite($handle, $output);										fclose($handle);										xslt_free($engine);																														print $_FILES['userfile']['name']; print "Added Successfully";

Link to comment
Share on other sites

You're not using file_get_contents anywhere, you're not reading the uploaded file at all. You set $input to be a string that is a <file> element, and then you find and replace in that string. So $tox is the file element string with ".xml" removed, and then you use that as an argument to xslt_process.Where are you trying to replace the contents of the file?

Link to comment
Share on other sites

haha, that's because that's the code stripped down. I can send my last attemps; I didn't feel like I needed to attach bad code. The above is two parts from the code.

Link to comment
Share on other sites

I scrapped it all... I don't know when or where I was at the point of this code below:

				else {										if ($_GET['pageName'] == 'upload') {							$uploaddir = 'App_Data/';							$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);															if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {																$file=fopen($uploadfile,"r") or exit("Unable to open file!");								while (!feof($file)) 								  { 								  $thisVAR = fgets($file);								  $find[] = "<?mso-application progid=\"Excel.Sheet\"?>";								  $find[] = "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"";								 $find[] = "xmlns:o=\"urn:schemas-microsoft-com:office:office\"";								 $find[] = "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"";								 $find[] = "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"";								 $find[] = "xmlns:html=\"http://www.w3.org/TR/REC-html40\"";								 $find[] = "o:";								 $find[] = "x:";								 $find[] = "ss:";								 $find[] = "html:";								 $find[] = "urn:";															$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$tox2 = str_replace($find, $replace, $thisVAR);									print $tox2;								  }								fclose($file);										$input = '																<file DATEadded="' .date('ymd F Y'). '"/> 																						  ';											$find[] = ".xml";																$replace[] = "";										$tox = str_replace($find, $replace, $input);																					$args = array('input' => $tox);																					$xmlfile = "directory_x.xml";										$xslfile = "update_s.xsl";																					$engine = xslt_create();										$output = xslt_process($engine, $xmlfile, $xslfile, NULL, NULL, $args);																					unlink($xmlfile);																					$handle = fopen($xmlfile, "w+");																					fwrite($handle, $output);										fclose($handle);										xslt_free($engine);																											} 								else {									print "Error.";									}							}

Doesn't work but you can see the idea.

Link to comment
Share on other sites

Oh boy. Long before, and now too, I feel sick every time I look at a code of yours. No, literally, I felt like I was going to throw up... it's a good thing I haven't had dinner yet.In DOM, it's as easy as:

<?php//Whatever code you use to copy the file to a certain location, having it's name in the $file variable$xml->loadXML($file);//Remove all PIs at the root level$xml->replaceChild($xml->documentElement,$xml);//In the end saving the temporary file with removed PIs from the top$xml->save($file);?>

Link to comment
Share on other sites

That's PHP 5. (Sad to say, I need 4)
Could you, on the very least, use the dom_xml extension? I suppose there is a chance the PHP5 code could be migrated to it, at least to some extent.
Link to comment
Share on other sites

Well, actuallt boen, your code doesn't even apply to this.The problem is not the XML/XSLThe problem is THE FILE BEING UPLOADED (which so happens to be an .xml) having a FIND and REPLACE and then SAVE to a DIRECTORY.

Link to comment
Share on other sites

The problem is THE FILE BEING UPLOADED (which so happens to be an .xml) having a FIND and REPLACE and then SAVE to a DIRECTORY.
Aren't you doing search&replace on the PIs (processing instructions) of the XML file? You know, the ones Excel places before the root element? The above code does just that. It removes them. No XSLT involved, and just 3 lines of code. It's actually also faster then doing an XSLT transformation.All other sorts of search&replace things you may want, DOM (or XSLT) can do them too. And the resulting file is to be saved with DOMDocument::save() or the equivalent DOM_XML dump_file() function in the case of PHP4.If the problem is in getting the actual file (i.e. anything before the search&replace), then yes, I have misunderstood you, sorry.
Link to comment
Share on other sites

By this I mean, I still don't have a way of opening "current"/"newly uploaded" xml.... that's my problem. fgets() only first line, etc etc etc, file_get_contents() not working.

Link to comment
Share on other sites

Exactly what would

<?phpecho file_get_contents($_FILES['userfile']['tmp_name']);?>

show if you used ONLY that as the code for your form processor?The above I believe should directly print the contents of the temporary file without even saving it.All provided your form has

<input type="file" name="userfile"/>

of course.

Link to comment
Share on other sites

I added a patch to the server to make file_get work.Here is what I have so far:

						if ($_GET['pageName'] == 'upload') {							$uploaddir = 'App_Data/';							$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);															$Excelfile = file_get_contents($_FILES['userfile']['tmp_name']);																  $find[] = "<?mso-application progid=\"Excel.Sheet\"?>";								  $find[] = "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"";								 $find[] = "xmlns:o=\"urn:schemas-microsoft-com:office:office\"";								 $find[] = "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"";								 $find[] = "xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"";								 $find[] = "xmlns:html=\"http://www.w3.org/TR/REC-html40\"";								 $find[] = "o:";								 $find[] = "x:";								 $find[] = "ss:";								 $find[] = "html:";								 $find[] = "urn:";															$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$replace[] = "";									$TURNexcelTOxml= str_replace($find, $replace, $Excelfile);																											$XMLfileToUpload = file_put_contents($TURNexcelTOxml);								if (move_uploaded_file($XMLfileToUpload, $uploadfile)) {										$input = '																<file location="' . htmlspecialchars($uploadfile, ENT_QUOTES) . '" 													name="' . htmlspecialchars($_FILES['userfile']['name'], ENT_QUOTES) . '" 													DATEadded="' .date('ymd F Y'). '"/> 																						  ';											$find[] = ".xml";																$replace[] = "";										$tox = str_replace($find, $replace, $input);																					$args = array('input' => $tox);																					$xmlfile = "directory_x.xml";										$xslfile = "update_s.xsl";																					$engine = xslt_create();										$output = xslt_process($engine, $xmlfile, $xslfile, NULL, NULL, $args);																					unlink($xmlfile);																					$handle = fopen($xmlfile, "w+");																					fwrite($handle, $output);										fclose($handle);										xslt_free($engine);																														print $_FILES['userfile']['name']; print "Added Successfully";									} 								else {									print "Error.";									}

It's really close. I'm just missing something between:

		$XMLfileToUpload = file_put_contents($TURNexcelTOxml);								if (move_uploaded_file($XMLfileToUpload, $uploadfile)) {

This is the contents of the Excel, and it has gotten the FIND/REPLACE. I just need it to be "put" back into the file then added...

Link to comment
Share on other sites

How do I fix these lines:

$XMLfileToUpload = file_put_contents($TURNexcelTOxml);								if (move_uploaded_file($XMLfileToUpload, $uploadfile)) {

Link to comment
Share on other sites

Like I said, you're using file_put_contents without a filename. Obviously you can't save the data into a file unless you tell it which file to save the data in. You have the right parameter count for move_uploaded_file, that function expects 2 parameters. But you're sending move_uploaded_file the return value from file_put_contents. If you check the reference for file_put_contents you can see that it returns an integer number corresponding to how many bytes were written to the file. So you're sending move_uploaded_file a number as the source file to move. Obviously it's not going to be able to find a file with that name.Check the reference pages on php.net for the functions you're trying to use. Most of your questions can be answered there.

Link to comment
Share on other sites

Actually, file_put_contents() is not even available in PHP4. Whatever the idea, you should rethink it a bit. I'm not sure what's going on in this mess, so I'm not going to say anything further.

Link to comment
Share on other sites

<?define('FILE_APPEND', 1);function file_put_contents($n, $d, $flag = false) {	$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';	$f = @fopen($n, $mode);	if ($f === false) {		return 0;	} else {		if (is_array($d)) $d = implode($d);		$bytes_written = fwrite($f, $d);		fclose($f);		return $bytes_written;	}}?>

Link to comment
Share on other sites

So removing the line

$XMLfileToUpload = file_put_contents($TURNexcelTOxml);

and putting the above like:

define('FILE_APPEND', 1);function file_put_contents($uploadfile, $TURNexcelTOxml, $flag = false) {	$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';	$f = @fopen($uploadfile, $mode);	if ($f === false) {		return 0;	} else {		if (is_array($TURNexcelTOxml)) $d = implode($TURNexcelTOxml);		$bytes_written = fwrite($f, $TURNexcelTOxml);		fclose($f);		return $bytes_written;	}}

But this doesn't work...

Link to comment
Share on other sites

No, that wouldn't work. You need to add the function definition anywhere in the file. I'm having a hard time getting the point across. Look at what you're doing:file_put_contents($TURNexcelTOxml)You're sending 1 parameter to the file_put_contents function. Look at the definition for file_put_contents:int file_put_contents ( string $filename , mixed $data [, int $flags [, resource $context ]] )Do you see the problem? There are 2 required parameters and 2 optional parameters, and you're only sending 1.

Link to comment
Share on other sites

I have an idea. Try to migrate your applicaiton to PHP5. Whatever incompatabilities are there with other code, let us deal with them. After all, PHP5 is most of the times compatible with PHP4.XSLT is practically one of the few exclusions to that rule, but then again, anything XML-ish is too. But comments on the XSL extension page also show a wrapper for the old functions incase that turns out to be a bummer to rewrite.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...