when I was using a Mac and MAMP the command worked fine but after moving the source code on Windows 10 and XAMPP now the following script doesn't delete the file but only move a copy of the file inside a folder (exported_files) and renaming the new file.
<?php
session_start();
error_reporting(0);
if (isset($_POST['ok']) && ($_POST['ok'] == 1))
{
//$arrayRicevuto = implode(" ",$arrayRicevuto);
if (!isset($_SESSION['countRows']) && empty($_SESSION['countRows']))
{
$_SESSION['countRows'] = $_POST['countRows'];
}
$data = date("dmY");
$filename = "EstrazioneParziale_del_" . $data;
$estensione = ".csv";
$fileOpen = fopen($filename.$estensione, "a") or die("Impossibile aprire il file");
foreach ($_POST['arrayFiltrato'] as $fields) {
fputcsv($fileOpen, $fields);
}
fclose($fileOpen);
$_SESSION['countRows']--;
if ($_SESSION['countRows'] == 0)
{
$finalData = date("h-i-s");
$directory="exported_files/";
copy($filename.$estensione, $directory.$_SESSION['email']."_".$filename.$finalData.$estensione);
unlink('$filename.$estensione');
unset($_SESSION['countRows']);
echo $directory.$_SESSION['email']."_".$filename.$finalData.$estensione;
}
} else {
echo "Errore! ";
}
?>
Can somebody give me a suggestion?
Many thanks in advance! :)
First problem: Don't use single quotes around variables as it will take the variable as a literal string instead of returning you its value. For example:
<?php
$test = "hello";
echo '$test'; // This will print $test
echo "$test"; // This will print hello
?>
In unlink();, you don't have to use quotes at all when using a variable.
Sometimes the system is unable to find the location of the file you're trying to delete with unlink()
. In those cases, you can use realpath(); as it will return the absolute path towards the file you're trying to delete:
unlink(realpath($filename.$estensione));