0

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.

santyas
  • 96
  • 1
  • 2
  • 12

5 Answers5

2

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.

benestar
  • 552
  • 4
  • 12
  • BTW that line of code appears to work as in your original post. I tried it here http://writecodeonline.com/php/ –  Aug 14 '13 at 14:48
  • @JustinWyllie Yes, it wasn't a php error but a javascript error as I posted above. – benestar Aug 14 '13 at 14:56
  • Ah yes. Sorry. He has not put quotes around the parameters. I see –  Aug 14 '13 at 15:53
1

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 .

  • Thank you for your observation, I'm trying to do my best. 1 - The picture looks good, the url is ok. 2 - How I can put one? 3 - It was imported and running js code in the application. But when I want to delete nothing happens. 4 - The parameters are in my question, is a copy of my code, but I have no behavior in the debugger. 5 - I have not used any return in delete.php :/ – santyas Aug 14 '13 at 14:31
1
  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.

  • Ok im testing your solution in the debuger. But i changed this line: `parametros // data:{name_only:name_only,ext:ext},` and works fine. I dont know how, it is the same! – santyas Aug 14 '13 at 14:59
1

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.

Community
  • 1
  • 1
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
  • Thank you again, and whats the differece between `var parametros = {"name_only": name_only, "ext": ext};` AND `data:{name_only:name_only,ext:ext},` ? – santyas Aug 14 '13 at 17:25
  • actually, if you are using arrays i think you need to pass them as json arrays. I have added link to the answer. or may be you just don't need the quotes. I am not sure since I always use the method I suggested. – Optimus Prime Aug 14 '13 at 18:00
0

can you add

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

into deletefile.php

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

Fatihd
  • 635
  • 6
  • 12