【问题标题】:PHP Unlink doesn't delete the file on Windows machinePHP Unlink 不会删除 Windows 机器上的文件
【发布时间】:2018-07-27 13:56:31
【问题描述】:

当我使用 Mac 和 MAMP 时,该命令运行良好,但在 Windows 10 和 XAMPP 上移动源代码后,现在以下脚本不会删除文件,而只会将文件的副本移动到文件夹中 (exported_files)并重命名新文件。

<?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! ";
}

?>

有人可以给我一个建议吗?

提前非常感谢! :)

【问题讨论】:

  • php.net/manual/en/function.error-reporting.php - error_reporting(0); 没有帮助。设置为捕捉和显示。
  • 您可能希望丢失引号,因为在使用变量时它不是必需的。除此之外,单引号内的变量被处理为文字字符串:unlink($filename.$estensione);。您可能还想尝试您可能希望丢失引号,因为在使用变量时它不是必需的。除此之外,单引号内的变量被处理为文字字符串:unlink(realpath($filename.$estensione));
  • @icecub 已经为你准备好了我认为...双引号将处理变量,单引号将其视为字符串,其值为 $filename.$estensione
  • 不知何故,我在那里的编辑出了点问题,哈哈。但是,如果它仍然不起作用,请尝试unlink(realpath($filename.$estensione));
  • @icecub 我已经使用 realpath 解决了这个问题。好建议!!!为你点赞!!我认为这个问题已关闭(如何标记问题..已关闭?抱歉,我是新人^^)

标签: php windows xampp unlink


【解决方案1】:

第一个问题:不要在变量周围使用单引号,因为它会将变量作为文字字符串而不是返回它的值。例如:

<?php

$test = "hello";

echo '$test'; // This will print $test
echo "$test"; // This will print hello

?>

unlink(); 中,使用变量时根本不必使用引号。

有时系统无法找到您尝试使用unlink() 删除的文件的位置。在这些情况下,您可以使用realpath();,因为它会返回指向您要删除的文件的绝对路径:

unlink(realpath($filename.$estensione));

【讨论】:

    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 2011-02-10
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多