Delete files posted by upload script within PHP

Ok so I used Valum's Upload script to make a custom file uploader feature within my buddypress profiles. Success there, but the failure comes when I appended a "delete" link to the list item that is returned. That link has an onclick event that uses WP ajax to then delete the file from the location it was just uploaded to.

The Ajax callback function is as follows:

/*
 * Delete Files
 */
add_action("wp_ajax_delete_file", "delete_file");
add_action("wp_ajax_nopriv_delete_file", "delete_file");
function delete_file(){
    ob_clean();
    $file = $_POST['file'];
    if(unlink($file)){
        $response = json_encode( array(
	    'success' => true
	)
    );
    } else {
        $writeable = (is_writable($file))? "YES" : "NO";
    	$exist = (file_exists($file))? "YES" : "NO";
	    $response = json_encode( array(
    	        'error'		=> FALSE,
    		'file'		=> $file,
    		'writable'	=> $writeable,
    		'exist'		=> $exist
		)
	);
    }
    // response output
    header( "Content-Type: application/json" );
    echo $response;
    // IMPORTANT: don't forget to "exit"
    exit;
}

The function fires and everything works as expected except the file does not delete and I now have the following message in my console which doesn't make sense because as you'll see it says the file doesn't exist but the file path is included and you can link to it and see that it exist.

The Response:

error: false
exist: "NO"
file: "http://kept-private.com/wp-content/uploads/user_uploads/divtruth/videos/close-up_01.jpg"
writable: "NO"

I checked the permissions through my FTP client (Filezilla) and I have 777 on all directories within user_uploads. Any guidance here would be awesome!

  • aecnu
    • WP Unicorn

    Greetings Nick,

    Thank you for this great question and a significant item to bring up.

    I admit I have no idea what need to be done to repair this custom coding.

    Therefore I will see if I can get a lead developer in here with his invaluable coding insight for his advice/advise for us.

    Though this may take a bit longer then a normal ticket, I will try to get him in here asap.

    Cheers, Joe

  • aecnu
    • WP Unicorn

    Greetings Nick,

    Thank you Nick it is greatly appreciated.

    Well it appears that Aaron is out until Friday, so i am going to try to get you handled though please bare with me I am not a coder by any means.

    I believe the problem is you are trying to address the file as a URL somewhere instead of the required path for manipulating files on a server, which cannot be done using a URL.

    Note: it returned a URL instead of a path.

    file: "http://kept-private.com/wp-content/uploads/user_uploads/divtruth/videos/close-up_01.jpg"

    Rather then something like: /root.dir/path/to/wp-content/uploads/lala/blah.jpg

    We are using the same AJAX uploads script in Activity Plus plugin, and we are doing some dance around the issue, so that code might be helpful for you to check out.

    But with my limited knowledge of coding but great knowledge of servers, I see it as an issue being URL = browser and Path = server

    Please let me know if this helps you out.

    As I mentioned I am certainly not a coder but wanted to try to give you a hand before Aaron Friday if possible.

    Please advise.

    Cheers, Joe

  • Div Truth
    • Design Lord, Child of Thor

    Hey Joe,

    Thanks for the advice. I passed this variable as the path:

    'upload_apath' => $_SERVER['DOCUMENT_ROOT'].'wp-content/uploads/user_uploads',

    And I was able to get this in the response from the console to show the path that is being used now:

    error: false
    exist: "NO"
    file: "/home/company_name/web/kept-private.com/webwp-content/uploads/user_uploads/divtruth/videos/close-up_01.jpg"
    writable: "NO"

    Unfortunately it still comes back unable to delete the file. The problem at the moment I believe is because it fails the logic: if(file_exists($file))

    I'll see what else I can figure out and report back if I find anything good, thanks for the help.

  • Div Truth
    • Design Lord, Child of Thor

    So ya I'm an idiot and your advice was great. I read my post again after I edited it and I realized the file path was missing a "/" after web and before wp-content. Fixed that and the file deleted perfectly. Awesome thanks a lot!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.