drakeman Posted September 18, 2009 Share Posted September 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/ Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920770 Share on other sites More sharing options...
drakeman Posted September 18, 2009 Author Share Posted September 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920772 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920776 Share on other sites More sharing options...
drakeman Posted September 18, 2009 Author Share Posted September 18, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920804 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920810 Share on other sites More sharing options...
drakeman Posted September 18, 2009 Author Share Posted September 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920814 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920818 Share on other sites More sharing options...
drakeman Posted September 18, 2009 Author Share Posted September 18, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/174719-weird-error-in-a-simple-fopen-read-script/#findComment-920858 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.