Jump to content

Recommended Posts

Hello, im learning php programming, and i make this script for fopen

<?php 

print"<form method='post' action=$PHP_SELF>";
print"<input name='filee' type='file' id='txtarchivo4'>";
print"<input type='submit' name='abrir' value=\"Leer Archivo\">";
print"<input type='submit' name='limpiar' value=\"Limpiar Datos\">";
print"</form>";

if(isset($_POST['abrir'])) {
	//Cargamos el contenido obtenido de una variable llamada fichero
	$fichero = $_POST['filee'];
	//Abrimos el fichero en modo lectura
	$descriptorfichero = fopen("$fichero","rb");
	//Hasta que no lleguemos al final del fichero
		while(!feof($descriptorfichero)) {
			//Capturamos 4096 caracteres dentro de la linea,
			//o menos si hay un retorno de carro antes
			$buffer = @fgets($descriptorfichero,4096);
			//Soltamos el texto, añadiendo<BR> detras
			echo $buffer."<br>";
		}
} else {
	$buffer = NULL;

}

?>

 

And this code works just fine in any windows machine installed with apache, xampp, or others, but in my home i have debian linux and when i try to run that script in my localhost i get errors like this

Warning: fopen(ml) [function.fopen]: failed to open stream: No such file or directory in /var/www/lynda/fopen.php on line 13

Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15


Warning: feof(): supplied argument is not a valid stream resource in /var/www/lynda/fopen.php on line 15

 

 

any ideas why this happens?

thanks in advance!

 

you are trying to open a file that doesn't exist in the directory. are you sure your file paths are set correctly? also I believe that the file structure in unix based systems are different than windows in that they use a backslash instead of a forward slash, but that could be entirely wrong, and I could have just made that up

you are trying to open a file that doesn't exist in the directory. are you sure your file paths are set correctly? also I believe that the file structure in unix based systems are different than windows in that they use a backslash instead of a forward slash, but that could be entirely wrong, and I could have just made that up

 

Yes, the path for the file is right!, cause i select the file with the form above, and then i press the button read the file, but i get those errors, yes, windows uses paths like  c:\\windows\system and unix or linux /var/www/web/file.txt but, i dont type manually the path of the file, i find it with the button on the form.

you are using a file type of form, so you need to use the $_FILE superglobal, as opposed to the post superglobal.

 

to get the original file name of the file you choose in that form, you use

$_FILES['filee']['name']

 

so try changing your script to

<?php 

print"<form method='post' action=$PHP_SELF>";
print"<input name='filee' type='file' id='txtarchivo4'>";
print"<input type='submit' name='abrir' value=\"Leer Archivo\">";
print"<input type='submit' name='limpiar' value=\"Limpiar Datos\">";
print"</form>";

   if(isset($_POST['abrir'])) {
      //Cargamos el contenido obtenido de una variable llamada fichero
      $fichero = $_FILES['filee']['name'];
      //Abrimos el fichero en modo lectura
      $descriptorfichero = fopen("$fichero","rb");
      //Hasta que no lleguemos al final del fichero
         while(!feof($descriptorfichero)) {
            //Capturamos 4096 caracteres dentro de la linea,
            //o menos si hay un retorno de carro antes
            $buffer = @fgets($descriptorfichero,4096);
            //Soltamos el texto, añadiendo<BR> detras
            echo $buffer."<br>";
         }
   } else {
      $buffer = NULL;
      
   }

?>

 

if that doesn't work, output the $fichero variable, and see what it holds in it

 

you are using a file type of form, so you need to use the $_FILE superglobal, as opposed to the post superglobal.

 

to get the original file name of the file you choose in that form, you use

$_FILES['filee']['name']

 

so try changing your script to

<?php 

print"<form method='post' action=$PHP_SELF>";
print"<input name='filee' type='file' id='txtarchivo4'>";
print"<input type='submit' name='abrir' value=\"Leer Archivo\">";
print"<input type='submit' name='limpiar' value=\"Limpiar Datos\">";
print"</form>";

   if(isset($_POST['abrir'])) {
      //Cargamos el contenido obtenido de una variable llamada fichero
      $fichero = $_FILES['filee']['name'];
      //Abrimos el fichero en modo lectura
      $descriptorfichero = fopen("$fichero","rb");
      //Hasta que no lleguemos al final del fichero
         while(!feof($descriptorfichero)) {
            //Capturamos 4096 caracteres dentro de la linea,
            //o menos si hay un retorno de carro antes
            $buffer = @fgets($descriptorfichero,4096);
            //Soltamos el texto, añadiendo<BR> detras
            echo $buffer."<br>";
         }
   } else {
      $buffer = NULL;
      
   }

?>

 

if that doesn't work, output the $fichero variable, and see what it holds in it

 

 

If i use $_files['filee'] ['name'] i get the same errors but now for windows and linux, with the other method i get the error only in linux!  :(

Oh sorry, i misread your script for a minute. Yeah the FILES super global won't work.

 

make sure your script is in the same directory as the file you are trying to access. the post value for your filee object is going to just be the name of the file, and not any paths leading to it.

 

where is the file you are trying to access, and where is the script itself

Oh sorry, i misread your script for a minute. Yeah the FILES super global won't work.

 

make sure your script is in the same directory as the file you are trying to access. the post value for your filee object is going to just be the name of the file, and not any paths leading to it.

 

where is the file you are trying to access, and where is the script itself

In that way works fine! thanks! but why in windows take the full paths even if the file is in another folder? that cannot be handle in the code inside linux?

thanks.

As far as that goes, I am not sure. I am running a local php server on a windows box, and for my script it returns the filename without any paths. Are you sure you just didnt have the file in question on your windows machine in the same directory as the script itself

As far as that goes, I am not sure. I am running a local php server on a windows box, and for my script it returns the filename without any paths. Are you sure you just didnt have the file in question on your windows machine in the same directory as the script itself

Yes, im sure, for example in my windows i have mi php files inside the xaamp folder, and i can read with that script a .txt file on the desktop, and it reads it fine! but in mi debian when i open a .txt file out of the working folder i get those errors above...  weird, so when i have my files in a real webserver the script should works? because i dont want to store the files in the same folder of the php scripts, i want to have a folder like /txt and open the files from there...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.