1

I've tried searching for the solution but nothing quite fit the issue.

I have a function which exports data from my SQL Database and writes it into a CSV file. The name of the file includes the current date.

When I use date format Y-m-d then it works but I need the time as well, when I add time it doesn't work and I get an error.

Here is the code

public function exportCSV()
{
    try {
        $this->db_table = $this->get_data_from_query('SELECT * FROM klassenraeume  ORDER BY id asc');

        if($this->db_table->num_rows > 0)
        {
            $folder = "exportiert/";
            $exported_file = $folder."csv_".date("Y-m-d").".csv";
            $file_creater = fopen($exported_file, 'w');

            foreach ($this->db_table as $data)
            {
                fputcsv($file_creater, $data);
            }

            fseek($file_creater, '0');
            echo "<p style='-webkit-text-fill-color: #26f308; font-size: 2em; '>Daten erfolgreich exportiert!</p>";
        }
    }
    catch (Exception $e)
    {
        echo "Error";
    }
}

I've tried this:

$exported_file = $folder."csv_".date("Y-m-d H:i:s").".csv";

and I've tried this way:

public function exportCSV()
{
    try {
        $this->db_table = $this->get_data_from_query('SELECT * FROM klassenraeume  ORDER BY id asc');

        if($this->db_table->num_rows > 0)
        {
            $date = date("Y-m-d H:i:s");
            $folder = "exportiert/";
            $exported_file = $folder."csv_". $date .".csv";
            $file_creater = fopen($exported_file, 'w');

            foreach ($this->db_table as $data)
            {
                fputcsv($file_creater, $data);
            }

            fseek($file_creater, '0');
            echo "<p style='-webkit-text-fill-color: #26f308; font-size: 2em; '>Daten erfolgreich exportiert!</p>";
        }
    }
    catch (Exception $e)
    {
        echo "Error";
    }
}

Nothing has worked so far. I've also tried putting $date into quotations etc.

The error message is this:

Warning: fopen(exportiert/csv_2022-06-23 12:51:28.csv): Failed to open stream: No such file or directory in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php on line 141

Fatal error: Uncaught TypeError: fputcsv(): Argument #1 ($stream) must be of type resource, bool given in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php:144 Stack trace: #0 C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php(144): fputcsv(false, Array) #1 C:\xampp\htdocs\CSV_Upload_Test\projekt\Bestehende_DB_Eintraege_anzeigen.php(75): Daten->exportCSV() #2 {main} thrown in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php on line 144

What am I doing wrong?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Elly_G
  • 13
  • 4

1 Answers1

1

Since you're using Windows, colons (:) are not allowed in a filename. This is a feature of the Windows filesystem.

So you can simply use some other character instead. In a date / time string, it's a commonly accepted convention to use a dot (.) as a separator between the the time components, so that is an approach you could take and which would likely to be understood by most people.

So simply:

$date = date("Y-m-d H.i.s");

See also Make filename with colon (“:”) in Windows and others.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • thank you, I didn't take that into consideration.. ! – Elly_G Jun 23 '22 at 11:09
  • actually *some* windows APIs allows you to use ":" in filenames, and some do not. For example, the [`CreateFile` api](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea) disallows `:` in filenames, while the [`NtCreateFile` api](https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile) allows you to use `:` in filenames. (why? dunno, guess Microsoft developers were drunk or something) - also php's native Windows build use CreateFile, while Cygwin's php use NtCreateFile, so windows-php can not create files with `:` but cygwin-php can – hanshenrik Jun 23 '22 at 14:14