dpcnm2132 2018-07-27 13:56
浏览 78
已采纳

PHP Unlink不会删除Windows机器上的文件

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! :)

  • 写回答

1条回答 默认 最新

  • doulun9534 2018-07-27 14:22
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿