Question

I am writing some code which allows a user to upload a .zip file. When they upload the file the .zip gets moved to its correct location on the server. 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(); 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

$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. The unlink($file); inside of my PHP file doesn't seem to be executed.

Was it helpful?

Solution

unlink() takes a path, not a URL. You must give it the path on the server, for example unlink('/var/www/mysite/files/upload.zip');

See unlink() documentation

OTHER TIPS

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

glob(), returns an array containing matched files/directories.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top