简体   繁体   English

从目录中删除.zip文件

[英]Deleting a .zip file from a directory

I am writing some code which allows a user to upload a .zip file. 我正在编写一些允许用户上载.zip文件的代码。 When they upload the file the .zip gets moved to its correct location on the server. 当他们上传文件时,.zip文件将移动到服务器上的正确位置。 That all works correctly. 一切正常。

I have created a button that allows a user to delete the file they have uploaded. 我创建了一个按钮,允许用户删除他们已上传的文件。 When the button gets click, I am using ajax to call on a .php file that houses the unlink(); 当按钮被点击时,我正在使用ajax调用包含unlink();的.php文件unlink(); code. 码。

The Ajax Call 阿贾克斯电话

function delete_custom_pack() {

                var phpfilepath = '/path/to/php/file/';

                //console.log( phpfilepath );


                    jQuery.ajax({
                      method: 'get',
                      url: phpfilepath ,
                      success: function() {
                        alert("successfully got file");
                      },
                      error: function() {
                        alert("error getting file");            
                      }

                    });

            }

PHP File PHP文件

<?php

$filename = 'http://www.some-site/wp-content/uploads/custom-folder/custom-pack/test.zip';

unlink($filename);

?>

I receive the alert "successfully got the file", but the file is never unlinked on the server. 我收到“成功获取文件”警报,但是文件从未在服务器上取消链接。 It still resides in the directory. 它仍然驻留在目录中。 The success call is just letting me know it successfully got the php file. 成功调用只是让我知道它成功获取了php文件。 The unlink($file); unlink($file); inside of my PHP file doesn't seem to be executed. 我的PHP文件内部似乎未执行。

unlink() takes a path, not a URL. unlink()采用路径而不是URL。 You must give it the path on the server, for example unlink('/var/www/mysite/files/upload.zip'); 您必须为它提供服务器上的路径,例如unlink('/var/www/mysite/files/upload.zip');

See unlink() documentation 请参阅unlink()文档

  foreach (glob("*.zip") as $filename) {
     if(unlink($filename))
            echo "success" ;
     else
            echo "Failure"
    }

glob(), returns an array containing matched files/directories. glob(),返回包含匹配文件/目录的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM