简体   繁体   中英

Why am I getting a 'Unable to locate stream wrapper' message when using unlink() in php?

I am trying to delete a file from the server using the unlink( ) function with php.

Here is my code:

$photo_to_delete = 'http://www.example.com/images/uploads/properties/197/IMG_0002.jpg';
unlink($photo_to_delete);

Here is the error message I am getting:

Warning: unlink() [function.unlink]: Unable to locate stream wrapper in /nfs/c09/h01/mnt/137110/domains/www.example.com/html/listers/edit_photos.php on line 43

Line 43 is where I have the unlink() function is.

Why am I getting a 'Unable to locate stream wrapper' message when using unlink() in php?

You cannot delete something over the HTTP protocol, period.
See the table at http://php.net/manual/en/wrappers.http.php .

To clarify, the HTTP protocol only does requests . You may well send a request that basically contains "please delete file x", but there needs to be a server/program/script behind the target URL that actually fulfills this request. You cannot simply ask random URLs to delete themselves.

I'm pretty sure you can't delete a file remotely like that. Try using a relative path. Or even an absolute path (minus the http://domain.com part).

In other words:

unlink('/images/uploads/properties/197/IMG_0002.jpg'); // relative path
unlink('/var/www/html/images/uploads/properties/197/IMG_0002.jpg'); // absolute

Or better yet, if you're going to use an absolute path, try prefixing it with $_SERVER['DOCUMENT_ROOT'] .

unlink($_SERVER['DOCUMENT_ROOT'] . '/images/uploads/properties/197/IMG_0002.jpg');

Okay, unlink() does in fact interface with PHPs stream wrapper API, but most wrappers don't implement the method.

There is a DELETE method for HTTP. But it's unlikely your server has that implemented. (Though a simple Script DELETE /delete.php could be set up. Not very advisable sans authorization, obviously!)
And then you would need curl or the http extension (or even fsockopen ) to actually send a proper request for this.

If you want to delete files on your server (the same where the php script runs) use relative or absolute path as mmmshuddup said. If the file is located somewhere else you should use some kind of network protocol for file management such as FTP... If this is your use case you can take a look here: http://php.net/manual/en/function.ftp-delete.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM