Skip to content
Advertisement

How to write file to desktop in laravel?

I am trying to write a backup for my database,

I want If a button is clicked the application will export the database to the desktop.

The app exports the file on my database directory if I use laravel database directory path

but if I change the path to desktop location nothing appears on the desktop.

        $get_all_table_query = "SHOW TABLES";
    $result = DB::select(DB::raw($get_all_table_query));

    $tables = [
        'users',
        'migrations'
    ];

    $structure = '';
    $data = '';
    foreach ($tables as $table) {
        $show_table_query = "SHOW CREATE TABLE " . $table . "";

        $show_table_result = DB::select(DB::raw($show_table_query));

        foreach ($show_table_result as $show_table_row) {
            $show_table_row = (array)$show_table_row;
            $structure .= "nn" . $show_table_row["Create Table"] . ";nn";
        }
        $select_query = "SELECT * FROM " . $table;
        $records = DB::select(DB::raw($select_query));

        foreach ($records as $record) {
            $record = (array)$record;
            $table_column_array = array_keys($record);
            foreach ($table_column_array as $key => $name) {
                $table_column_array[$key] = '`' . $table_column_array[$key] . '`';
            }

            $table_value_array = array_values($record);
            $data .= "nINSERT INTO $table (";

            $data .= "" . implode(", ", $table_column_array) . ") VALUES n";

            foreach($table_value_array as $key => $record_column)
                $table_value_array[$key] = addslashes($record_column);

            $data .= "('" . wordwrap(implode("','", $table_value_array),400,"n",TRUE) . "');n";
        }
    }
    $file_name = dirname("C:UsersADC-2Desktop") .'/database_backup_on_' . date('y_m_d') . '.sql';
    $file_handle = fopen($file_name, 'w + ');

    $output = $structure . $data;
    fwrite($file_handle, $output);
    fclose($file_handle);
    echo "DB backup ready";

It will display DB backup ready but nothing really shows up on the desktop.

Advertisement

Answer

dirname()

Use

"C:\Users\ADC-2\Desktop"

instead of

"C:UsersADC-2Desktop"
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement