Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I ran into this problem in an application I am creating to compare files. I am new to this and, after seeing several websites specialized in it and changing the code several times, I cannot get it to show me the two files to be able to follow the programming. I detail how I have defined everything:

tratar_csv.php
PHP
<pre><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Descarga ficheros CSV</title>
</head>

<body>
<div class="container">
  <DIV id="PANEL_0" class="panel panel-primary text-justify">
  <DIV class="panel-heading">
    <H3 class="panel-title">Fichero a trabajar</H3>
  </DIV>
  <DIV class="panel-body">
    <FORM action="ficheros_permitidos.php" method="POST" enctype="multipart/form-data"> 
     
      <DIV class="form-group">
        <label for="origen" role="button">Fichero CSV:</label>
        <INPUT id="origen" type="file" name="origen" class="form_control" />
      </DIV>
      <input type="hidden" name="MAX_FILE_SIZE" value="50000000">	
		<input type="submit" name="continuar" value="Fichero de origen">
		<input type="submit" name="reset" value="Reiniciar">
	  
    </FORM>
  </DIV>
  </DIV>
</div>

</body>
</html>
</doctype>


ficheros_permitidos.php
PHP
<pre><?php
if (isset($_POST['continuar'])) {
	$nombreFichero = $_FILES['origen']['name'];
//	print_r($_FILES);
    $tipoFichero=pathinfo($nombreFichero,PATHINFO_EXTENSION);	
		switch($tipoFichero)	{
			case 'csv':
				include_once('procesa.php');
				break;
			case 'xls':
				include_once('procesa.php');
				break;
			case 'txt':
				include_once('procesa.php');
				break;
			default:
				include_once('error.php');
				break;
		}
}
if (isset($_POST['reset']))	{
		header('location:tratar_csv.php');
}

?>
<script type="text/javascript">
function Redirect()
{
window.location="tratar_csv.php";
}
setTimeout('Redirect()', 30000);
</script>


procesa.php
PHP
<pre><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ficheros a evaluar</title>
</head>

<body>
<div class="container">
  <DIV id="PANEL_0" class="panel panel-primary text-justify">
  <DIV class="panel-heading">
    <H3 class="panel-title">Múltiples ficheros a evaluar</H3>
  </DIV>
  <DIV class="panel-body">
	<FORM action="ficheros_permitidos_multiples.php" method="POST" enctype="multipart/form-data">
     
      <DIV class="form-group">
        <label for="archivos_multiples" role="button">Archivo a comparar:</label>
		<INPUT id="archivos_multiples" type="file" name="archivo" class="form-control" />
        
	</DIV>
	
		<input type="submit" name="comparar" value="Comparar ficheros">
		<input type="submit" name="reset" value="Reiniciar">
		  
	
    </FORM>
  </DIV>
  </DIV>
</div>

</body>
</html>
</doctype>


ficheros_permitidos_multiples.php
PHP
<?php
if (isset($_POST['comparar'])) {	
		$nombreArchivo = $_FILES['archivo']['name'];
//	print_r($_FILES);
		$tipoArchivo=pathinfo($nombreArchivo,PATHINFO_EXTENSION);
		switch($tipoArchivo)	{
			case 'csv':
				include('buscar_y_comparar.php');
				break;
			case 'xls':
				include('buscar_y_comparar.php');
				break;
			case 'txt':
				include('buscar_y_comparar.php');
				break;
			default:
				include('error.php');
				break;
		}
}
	if (isset($_POST['reset']))	{
		header('location:procesa.php');
	}

?>
<script type="text/javascript">
function Redirect()
{
window.location="procesa.php";
}
setTimeout('Redirect()', 30000);
</script>


buscar_y_comparar.php
PHP
<?php

//if(isset($_GET[$tipoArchivo])) 	{
//	$fichero=($_FILES['origen']);
//	$nombreFichero=array();
	$nombreFichero=($_FILES["origen"]["name"]);
	$nombreArchivo=($_FILES["archivo"]["name"]);
	
	echo("Empezar la búsqueda y comparación entre archivos");
	echo("<br>");
	
	echo($nombreFichero);
	echo("<br>");
	echo($nombreArchivo);
//}
//else {
//	$fichero='';
//}

?>


The result I have achieved from all this is the following:

Notice: Undefined index: origen in C:\xampp\htdocs\buscar_y_comparar.php on line 6

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\buscar_y_comparar.php on line 6
Empezar la búsqueda y comparación entre archivos

Sanitario.csv


Thanks in advance for your help

What I have tried:

PHP
if(isset($_GET[$tipoArchivo])) 	{
	$fichero=($_FILES['origen']);
	$nombreFichero=array();
	$nombreFichero=($_FILES["origen"]["name"]);
	$nombreArchivo=($_FILES["archivo"]["name"]);
Posted
Updated 14-Jul-20 2:36am
v2
Comments
Andre Oosthuizen 14-Jul-20 2:57am    
Which line is giving the error, what variable shows as undefined?

You are trying to access an index on a variable that is null, meaning it has no members. All you have to do is debug it and find out why, we can't run it for you.
 
Share this answer
 
You are using a FILE array called 'origen' but it has not been declared anywhere in your code. You need to declare it first and add values to it before you can make a call to its array values -

PHP
$_FILES['origen'] = array();

$_FILES['origen']['name'] = $_POST['name']; //as an example if value was posted, you can use $_GET or $_REQUEST as well depending on your code...

//Now you can make a call to the array...
$nombreFichero=($_FILES["origen"]["name"]);


EDIT: Correcting OP's code...
1) You have a form that posts 'origen' as a value -

PHP
//At the top of your php (ficheros_permitidos.php) page...
if (session_status() == PHP_SESSION_NONE) {
	session_start();
}

if (isset($_POST['continuar'])) {
	$nombreFichero = $_POST['origen'];
   $_SESSION['origen_name'] = nombreFichero;
}


Now you can the value in
buscar_y_comparar.php


PHP
//At the top of your php (buscar_y_comparar.php) page...
if (session_status() == PHP_SESSION_NONE) {
	session_start();
}

<pre>$nombreFichero = $_SESSION['origen_name'];
	
	echo("Empezar la búsqueda y comparación entre archivos");
	echo("<br>");
	
	echo($nombreFichero);
 
Share this answer
 
v2
Comments
Member 14888131 14-Jul-20 9:09am    
and where I must declare it? I tried adding a php in the form and doesn't go. Then, I tried before and after the isset in ficheros_permitidos.php (both options) and it's the same, Undefined index
Andre Oosthuizen 14-Jul-20 9:43am    
I am busy updating my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900