Question

I am trying to show and delete images that are stored in folders, but I can not run the JS code that removes each.

My code php in the index.php:

// folder with files    
$pathimages = $_SERVER['DOCUMENT_ROOT'];
$pathimages .= "/uploads/files/";

// showing images
foreach(glob($pathimages.'*') as $filename){
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $name_only = basename($filename, ".".$ext);

    echo "<img src=\"../uploads/files/" .$name_only.".".$ext. "\" width=\"30\" />
        <a onclick=\"DeleteImage(".$name_only.",".$ext."); return false;\" href=\"javascript:;\">X</a>
        <br />";
}

(Im sending the file-name and extension in two vars, I dont know why)

When I click on the "X", call the function "DeleteImage", but doesnt work.

JS function from .js file imported:

 function DeleteImage(name_only, ext){
    var parametros = {
        "name_only" : name_only,
        "ext" : ext
    };

    $.ajax({
        url: 'views/delete_image.php',
        type: "POST",
        data: parametros,
        success: function(datos){
            console.log("check");
        }
    });
}

And the delete file php:

$name_only = $_POST['name_only'];
$ext = $_POST['ext'];

$pathimages = $_SERVER['DOCUMENT_ROOT'];
$pathimages .= "/uploads/files/";

unlink($pathimages . $name_only .".". $ext);
echo "Deleted";

I have something similar but with records in the database and works OK, using GET method, deleting, editing, etc. But here the problems started. Thank you very much and I apologize for my english.

Was it helpful?

Solution 2

I think the problem is with the array you are passing,

Try using,

data:{name_only:name_only,ext:ext},

to pass the parameters or you may consider passing them as json arrays.

OTHER TIPS

As it is a javascript error, take a look at this lines of code:

<img src=\"../uploads/files/" .$name_only.".".$ext. "\" width=\"30\" />
        <a onclick=\"DeleteImage('".$name_only."','".$ext."'); return false;\" href=\"javascript:;\">X</a>
        <br />

You have to add the quotation marks in javascript.

I don't think it is really possible to offer an answer. The problem could be anywhere. I would consider: i. Does the image show on the page? ii. Put a debug point in DeleteImage and see what parametros actually is. This will also check that it is called. iii. Is the ajax call even made? Again Firebug or the Chrome developer tools will come in handy here. Look at the Network tab and the XHR option. iv. Ok. So we've got this far; echo the post back from the ajax handler - did you pass the correct parameters there? v. What does unlink return? it returns true or false. If false then likely path or permissions problems.

Basically it seems to me your question is showing a lack of knowledge about how to debug .

  1. Good.

  2. If using Chrome. Press F12. Choose Sources. Click the little arrow at the left. Choose the JS file that contains DeleteImage - or the page if it is inline Javascript. Open the file (in the debugger). Use Watch Expressions over on the right hand side to see what paramatros actually is by clicking the + sign and typing in parametros. Then click the X. The debugger will spring into action and stop at the line where you put the break point. (Now look over at the Watch Expressions on the right hand side and type in the name of the variable you want to watch e.g. parametros).

  3. 'Nothing happens': are you sure? Now you have the Chrome debugger open click the Network tab; and click XHR (which means you only see the Ajax requests). Look at the Headers and Response in the panels on the right. Now you can see what happened: was the ajax call made (I assume you have jQuery loaded before the file which contains DeleteImage?) ? What did it return?

    • -
  4. Now you can see the response from the ajax call modify the delete file to this: $result = unlink($pathimages . $name_only .".". $ext); echo $result. Is it true or false? Do a var_dump() on $_POST i.e. var_dump($_POST) in the delete file and see what you get in the response- did you post what you wanted to?

Another thing you could try of course is calling that php delete script directly in the browser address bar with known parameters - to see if that part is working.

can you add

 echo $pathimages . $name_only .".". $ext;

into deletefile.php

i think $ext is repeated . but i am not sure.

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