Delete onclick file using PHP

I want to delete a file when the user clicks the delete link. But when I go to my page, the file is deleted, and I do not know why:

echo '<h3><a onclick="'.unlink(__FILE__).'">Delete Now!</a></h3>'; 

What am I doing wrong?

+4
source share
6 answers

This code will delete the current file when the user clicks on the link:

 <h3><a href="?delete=1">Delete Now!</a></h3> <?php if(isset($_GET['delete'])) { unlink(__FILE__); } ?> 

If you prefer to use POST instead of the GET method, use this code:

 <form method="post"> <input name="delete" type="submit" value="Delete Now!"> </form> <?php if(isset($_POST['delete'])) { unlink(__FILE__); } ?> 
+7
source

You need to load this action using Javascript. If you use jQuery, you cannot try something like this

Your javascript

 <script type="text/javascript"> $('.delete').live('click',function(){ deleteFile( $(this).attr('id') ); }); function deleteFile(id){ $.ajax({ url: 'deletefile.php?fileid='+id, success: function() { alert('File deleted.'); } }); } </script> 

Your deletefile.php file looks like this.

 <?php $fileid = $_GET['fileid']; //HERE IS THE LOGIC TO FIND THE PATH OF YOUR FILE unlink($file); //You can add more validations or full paths ?> 

And your link should have the following structure

 printf("<a id='%s' class='delete'>Delete</a>",$youridfile); 
+5
source

You do not understand the difference between client and server code. Javascript cannot just call PHP. PHP will start immediately, since the page is built on the server, and not saved for later use.

You will need to make an AJAX request to delete onClick or create a new page, for example / delete / $ ID /, that will be deleted for you, or, as Jocelyn just beat me, make the same page capable of deleting if the GET / POST

Although, it is worth noting that __FILE__ is the file in which this code is located, so it is going to kill itself.

+3
source

Is this php? You cannot run a PHP function from inside javascript. Instead, you need to upload / redirect / send to php file.

 echo '<h3><a href="deleteScript.php" >Delete Now!</a></h3>'; 

edit:

 function table_exists($tablename, $database = false) { if(!$database) { $res = mysql_query("SELECT DATABASE()"); $database = mysql_result($res, 0); } $res = mysql_query(" SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename' "); return mysql_result($res, 0) == 1; } if(table_exists('my_table_name')) { // do something } else { // do something else } 
+3
source

You cannot run the PHP function as an onclick event, javascript. You need to run this function like this:

 <?php if (isset($_GET['delete'])) { unlink($_GET['delete']); } ?> <html> <a href="?delete=/PATH/TO/FILE">Delete Now</a> </html> 
+2
source

Do you need a request to send php processes ... or an ajax / javascript function that runs a php script ...

Here is an example with ajax http://www.website-php.com/de/tutorials/treeview/treeview-04.html

+1
source

Source: https://habr.com/ru/post/1444893/


All Articles