Treehouse: Grow your CSS skills. Land your dream job.

PHP unlink() issue

  • # July 18, 2011 at 7:38 am

    I cannot seem to get this to work. I have tried multiple things, and all of them give me the same PHP error, that there is no such file or directory in the site. This isn’t true, because if I FTP to the site, I can SEE that these files are there… Anyway, here is what I’m doing:


    I understand that unlink does NOT take a URL, and I store some url info in the DB, so I strip that out. So I am basically just using a straight path to the file, since I’m in the root directory of the site when doing this. So that line above would actually run as something like this:


    Any ideas on this? I’ve also tried doing it this way:

    unlink($_SERVER . str_replace(“”,””,$films));

    Again, same result – nothing I’ve done will work, and it can never ‘find’ the file, even though it’s there. Please, please help if you can. Thanks.

    # July 18, 2011 at 8:08 am

    You’re stripping out too much from the URL. With the string replace you’re doing you end up with a document relative URL, whereas you should be running a site relative URL.


    # July 18, 2011 at 8:13 am

    @TT_Mark, it doesn’t make a difference – I get the same error again :( Any other ideas?

    # July 18, 2011 at 8:17 pm

    Make sure you get the right path to the file.
    It’s a better practice to always use ABSOLUTE path.

    Try to print the path and check if it does exist:

    $file_path=$_SERVER . str_replace("","",$films);

    echo $file_path;
    echo '

    BTW, it’ll be helpful if you can show your PHP error.

    # July 18, 2011 at 11:23 pm

    @ddliu – I don’t think you read my original post – I DID try using the $_SERVER, but with no success. I have tried multiple things – the file DOES exist (it’s being used in multiple places on the site), but I want the user to have the option to delete it. The error I get says:

    Warning: unlink(/home/syxbit/public_html/ Flops.png
    ) [function.unlink]: No such file or directory in /home/syxbit/public_html/ on line 27

    The output of:

    echo $_SERVER . str_replace("","",$films);

    is exactly the path in the unlink.

    Any other ideas?

    # July 19, 2011 at 1:21 am

    The only thing I can guess is the file path issue.

    I see there is a new line at the end of the path:

    Warning: unlink(/home/syxbit/public_html/ Flops.png

    Is it exact what it is? Maybe you can try to “trim()” it to see the result.

    Also something you may have a try:
    1. Try a path without space.
    2. Change file permission to 777 and try it again.

    # July 19, 2011 at 8:44 am

    @ddliu – I can’t believe I didn’t recognize that… it looks like it was JUST as simple as an extra space at the end of my file name… So the file name was something like this:

    ‘Flip Flops.png ‘

    Once I did the trim(), it fixed it. Thank you!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.