上传文件并使用file_get_contents php读取

时间:2015-12-03 21:55:38

标签: php mysql xml simplexml

我正在尝试使用file_get_contents上传文件并读取它,目前我可以读取文件并获取数据,但只有项目文件夹中的文件,有人可以告诉我,如何使用post方法或成才?

这是我的代码:

   <?php

$RE_fecha='.*?((?:2|1)\d{3}(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))';





$xmlCont=file_get_contents('archivo');
//$xmlCont=file_get_contents($_FILES);

$RE_receptor='<.*?Receptor.*?rfc="(.*?)"';
$RE_nombre='<.*?Receptor.*?nombre="(.*?)"';
$RE_calle='<.*?Receptor.*?calle="(.*?)"';
$RE_cp='<.*?Receptor.*?codigoPostal="(.*?)"';
$RE_colonia='<.*?Receptor.*?colonia="(.*?)"';
$RE_estado='<.*?Receptor.*?estado="(.*?)"';
$RE_municipio='<.*?Receptor.*?municipio="(.*?)"';
$RE_noExterior='<.*?Receptor.*?noExterior="(.*?)"';
$RE_noInterior='<.*?Receptor.*?noInterior="(.*?)"';
$RE_pais='<.*?Receptor.*?pais="(.*?)"';



//Extraer fecha del xml
preg_match_all("/".$RE_fecha."/is",$xmlCont, $matches);
$fechaxmlunix=strtotime($matches[1][0]);
$fechaxmlorig=$matches[1][0];
unset($matches);

//Extraer rfc del receptor
preg_match_all('/'.$RE_receptor.'/is',$xmlCont, $matches);
$rfc=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer rfc del receptor
preg_match_all('/'.$RE_nombre.'/is',$xmlCont, $matches);
$nombre_rs=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer rfc del receptor
preg_match_all('/'.$RE_calle.'/is',$xmlCont, $matches);
$calle=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer código postal del receptor
preg_match_all('/'.$RE_cp.'/is',$xmlCont, $matches);
$cp=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer colonia del receptor
preg_match_all('/'.$RE_colonia.'/is',$xmlCont, $matches);
$colonia=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer estado del receptor
preg_match_all('/'.$RE_estado.'/is',$xmlCont, $matches);
$estado=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer municipio del receptor
preg_match_all('/'.$RE_municipio.'/is',$xmlCont, $matches);
$municipio=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer no. exterior del receptor
preg_match_all('/'.$RE_noExterior.'/is',$xmlCont, $matches);
$no_exterior=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer no. interior del receptor
preg_match_all('/'.$RE_noInterior.'/is',$xmlCont, $matches);
$no_interior=$matches[1][0]; // RFC del receptor
unset($matches);

//Extraer pais del receptor
preg_match_all('/'.$RE_pais.'/is',$xmlCont, $matches);
$pais=$matches[1][0]; // RFC del receptor
unset($matches);

echo "Fecha es: ".$fechaxmlorig."<br />";
echo "Fecha UNIX es: ".$fechaxmlunix."<br />";
echo "RFC receptor es: ".$rfc."<br />";
echo "Nombre: ".$nombre_rs."<br />";
echo "Calle: ".$calle."<br />";
echo "No. Exterior: ".$no_exterior."<br />";
echo "No. Interior: ".$no_interior."<br />";
echo "Colonia: ".$colonia."<br />";
echo "Municipio: ".$municipio."<br />";
echo "Estado: ".$estado."<br />";
echo "País: ".$pais."<br />";
echo "Código Postal: ".$cp."<br />";


// open MySQL connection
$connection = mysqli_connect("localhost", "root", "", "rbp") or die ("ERROR: Cannot connect");

//$sql = "INSERT INTO facturas_xml (rfc, nombre_rs, calle) VALUES ('$rfc', '$desxml', '$calle')";
$sql = "INSERT INTO facturas_xml (rfc, nombre_rs, calle, no_exterior, no_interior, colonia, municipio, estado, pais, cp, nombre) VALUES ('$rfc', '$nombre_rs', '$calle', '$no_exterior', '$no_interior', '$colonia', '$municipio', '$estado', '$pais', '$cp', '$nombre_rs')";
mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");

// close connection
mysqli_close($connection);

require 'view/carga.view.php';

?>

<html>

<header>
	<meta charset="utf-8" />
</header>
<body>
<h2>Carga de archivos </h2>
<?php if(strlen($msj)>0):?>
<div><?php echo $msj; ?></div>
<?php endif;?>
<form action="obtener.php" method="post" 
enctype="multipart/form-data">
	<input type="file" name="archivo" id="archivo"></input>
    <input type="submit" name="subir" value="Subir archivo"></input>
</form>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您想要阅读当前上传文件的内容。如果您只想阅读上传文件的内容,只需使用

即可
<?php
  $contents = file_get_contents($_FILES['file_name']['tmp_name'];
 //$contents contains the content of the currently uploaded file.
?>