0

I created a simple code to open txt file and put values in mysql database. When I upload the file I have error massage:

Warning: move_uploaded_file(files/szoki.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\betolto\index.php on line 60

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpF08E.tmp' to 'files/szoki.txt' in C:\xampp\htdocs\betolto\index.php on line 60

Warning: fopen(files/szoki.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\betolto\index.php on line 61 Unable to open file!

I'm using XAMPP to run this code

What is the problem?

my code is:

<?php 

if(isset($_POST['read'])){

    $link=mysqli_connect('localhost','root','','szokiadatbazis') or die('could not connect to database'.mysql_error());
    $terminated=$_POST['deli'];
    $file_type=$_FILES['file1']['type'];
    $allow_type=array('text/plain');
    $fieldall="";
    if(in_array($file_type,$allow_type)){

      move_uploaded_file($_FILES['file1']['tmp_name'],"files/".$_FILES['file1']['name']);
      $file=fopen("files/".$_FILES['file1']['name'],"r") or die ("Unable to open file!");
      $tru="truncate table upload_test";
      mysqli_query($link,$tru) or die(mysql_error());
      while(!feof($file)){
      $line = fgets($file);

      $values=str_replace($terminated,"','",$line);
      $sql="insert into upload_test values('$values')";
      mysqli_query($link,$sql);
    }
    fclose($file);//close the file after read
    unlink("files/".$_FILES['file1']['name']);

    }else{
        echo "Please select only text file(.txt file is recomended)!";  

    }
}
?>
j08691
  • 204,283
  • 31
  • 260
  • 272
S. Patrik
  • 49
  • 1
  • 8

2 Answers2

0

you have to specify correctly where to put uploaded file.

"files/".$_FILES['file1']['name']

probably is not right location. you can choose relative or absolute path to save your file. i would suggest you use absolute path and use something like this:

$filesPath = dirname(__DIR__);

To find where it points to, echo $filesPath and adjust it to point to correct folder, where you want files to be saved..

for example: if you run your script from c:\xampp\www\project\script.php

$filesPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "tmp";

would point to your c:\xampp\tmp

But better check correct locations by printing the variable!

Jimmmy
  • 579
  • 12
  • 26
  • There is a troubleshooting checklist for this problem here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 15 '16 at 21:34
0

open a cmd and type

mkdir C:\xampp\htdocs\betolto\files attrib -r -a -s -h C:\xampp\htdocs\betolto\files\

if on unix , open a terminal and type:

mkdir 0755 path_to_http_dir/betolto/files

as natural for move_uploaded_file will pick up $_FILES['file1']['tmp_name'] from your php's tmp dir

As you unlink the file at the end, we can simplify the process by using sys_get_temp_dir

but I forgot that tmp_name already contain the full path.

so this line sys_get_temp_dir() . '/' will not be necessary

This way

<?php

    if(isset($_POST['read'])){

        $link=mysqli_connect('localhost','root','','szokiadatbazis') or die('could not connect to database'.mysql_error());
        $terminated=$_POST['deli'];
        $file_type=$_FILES['file1']['type'];
        $allow_type=array('text/plain');
        $fieldall="";
        if(in_array($file_type,$allow_type)){

          $file = fopen($_FILES['file1']['tmp_name'],"r") or die ("Unable to open file!");

          $tru = "truncate table upload_test";
          mysqli_query($link,$tru) or die(mysql_error());

          while(!feof($file)){

             $line = fgets($file);
             $values=str_replace($terminated,"','",$line);
             $sql="insert into upload_test values('$values')";
             mysqli_query($link,$sql);
         }

         fclose($file);//close the file after read

         } else {

            echo "Please select only text file(.txt file is recomended)!";
        }
    }
?>
Quijote Shin
  • 501
  • 4
  • 11