fopen throwing warning


#1

Hi guys. I’m pretty new to php so any help would be appreciated.

I am working on a file upload system on a website which allows the users to upload an excel file with the data then being stored in a phpmyadmin database.

I have been stuck on this error for a few days and I am starting to lose hope.

[php]
if ( isset($_FILES["orderImp"])) 
{
         $fileName = pathinfo($_FILES['orderImp']['name'], PATHINFO_FILENAME);
         $now = date("d_m_y");
         $fileName = $fileName.".xlsx" ;
             //if file already exists
            // echo $fileName;
         if (file_exists("../Upload/" . $fileName)) 
         {
             unlink("../Upload/".$fileName);
         }
        $storagename = $fileName;
        move_uploaded_file($_FILES["orderImp"]["tmp_name"],  $_SERVER["DOCUMENT_ROOT"] .'/OnlineOrder/Upload/' . $storagename);
	
        if ( $file = fopen( $_SERVER["DOCUMENT_ROOT"] .'/OnlineOrder/Upload/' . $storagename , "r" ) or die("Can't open file !") )
        {

            $firstline = fgets ($file );
            //Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
            $num = strlen($firstline) - strlen(str_replace(";", "", $firstline));

            //save the different fields of the firstline in an array called fields
            $fields = array();
            $fields = explode( ";", $firstline, ($num+1) );

            $line = array();
            $i = 0;
            $dsatz=array();

            while ( $line[$i] = fgets ($file) ) 
            {
                $dsatz[$i] = array();
                $dsatz[$i] = explode( ";", $line[$i], ($num+1) );

                $i++;
            }
            $lNum = 1;
            
            foreach ($dsatz as $key => $number) 
            {
                $code = "";
                $qty= 0;
                $inst = "";
                foreach ($number as $k => $content) 
                {
                    if(trim($fields[$k]) == "Numero")
                    {   
                        $custOrderNo = $content;
                    }
                }
            }
        }
    
}
[/php]

This code gives me the following warning

Warning: fopen(D:/XAMPP/htdocs/OnlineOrder/Upload/file1.xlsx): failed to open stream: No such file or directory in D:\XAMPP\htdocs\OnlineOrder\XLSXReader-master\sample.php on line 27
Can’t open file !

Line 27 is

if ( $file = fopen( $_SERVER["DOCUMENT_ROOT"] .'/OnlineOrder/Upload/' . $storagename , "r" ) or die("Can't open file !") )

I hope the format is right as this is my first post. Any help/leads/pointers would be greatly appreciated. Thanks in advance.


#2

The upload is failing, probably due to the size of the file being uploaded. The move_uploaded_file() is then failing, because there is no uploaded file to move, and is returning a false value.

Your code must ALWAYS detect and handle errors.

For the file upload, you need to test if the upload worked (an error code of zero) before using any of the uploaded file information and running the rest of the code that’s dependent on having a successfully uploaded file, output an appropriate error message telling the visitor what if anything they did that they can correct that cause the code to fail (like an oversize file, unacceptable file type/extension, …), and output a general failure message for the things the visitor cannot correct (coding or server problems) and log the actual cause of these problems so that you have information to find and fix coding/server side problems.

For the move_uploaded_file statement, this usually fails due to a wrong path or permissions, which the visitor cannot do anything about, and for which you should just output a general failure message. Once you correctly detect that the file uploaded without any errors and not run the dependent code when it fails, this statement will then only then fail due to a wrong path or a permission problem.


#3

Just tested it with a smaller file and it worked fine. I can’t believe it was such a silly thing.
Thanks for your help phdr :slight_smile: