Search code examples
javascriptphpunlink

I'm using (unlink($_GET['videofile'])) and it works fine on dev server but in prod won't work


Hi I'm loading a table with files on a server folder, each row has a "Delete" link that when clicked, it should unlink/remove the file from the server. The code works perfectly fine on my dev server, however, when in prod it doesn't work. I have checked all the code and there doesn't seem to be any difference on the code. I'm not sure if I'm missing some permission related thing on my prod server, but when I click on "delete" the files are not deleted at all. It runs the function below as if it is doing it but the file is still there.

The addfile.php contains:

if (unlink($_GET['videofile'])) {};

The javascript function

function deleteVideo(file_path)
    {  
        var r = confirm("Are you sure you want to delete this Video?");
        var j = document.getElementById('vid').value;
        if(r == true)

        {
            $.ajax({
              url: 'addfile.php',
              data: {'videofile' :  file_path },
              success: function (response) {
                 alert('Your file has been removed');
                 showVideos(j);

              },
              error: function () {
                 alert('There was an error removing the file, please try again');
              }
            });
        }
    }   

The "delete" link looks like:

 deleteVideo("videopath")

Solution

  • In your production server, check if your httpd server has permissions to write in video folder.

    Take care of allowing unlink a file thought its path, it may be very dangerous. Instead, register your video path on a database, and then passes to function only the ID of video. Also, make sure your user is deleting only the videos he can delete, according to your business rules.