简体   繁体   中英

PHP file_exists and unlink doesn't work (“No such file or directory”)

$path = "F:/www/__DADOS__/__SESSAO/__9987f2bfdfb80bce8fd72402887bb2c50a433ae0__E6nDSMnD7TCY5#47$BNCx0e#47$r#47$ByZJvcyMIazXSYWBWBXN5lgdZOd3Ps#47$ROrVSPl7QVQaCqfa2WezCauk#47$LVFyhgw==.meudominio.com.sessid"

file_put_contents($path, $texto);

unlink($path);

In the above code I get the warning No such file or directory in unlink($ path); and the file is not deleted, I also tried using file_exists to check the file before, and the return is: false .

The file_put_contents creates the file correctly (checked), even with the correct content. I can access the file through file_get_contents , but in time to delete or verify the existence get failure. Anyone have a clue?

The total size of $path is 241 .

The problem seems more delicate. The file name is formed by an base64_encode , the filename changes often, and I have about 10 files per execution. I can delete some, others not, issuing the warning described above. Can it be any character that is not to unlink() does not accept? I have some escape reserved characters such as /\\?%*:|"<> . http://en.wikipedia.org/wiki/Filename

I tried using hash (sha1, sha256 and sha512) instead of base64_encode, but the error persists.

Since you are using / inside of " " so / char is not a normal character and it's usually used to escape special characters ( wen used inside of double quotation marks ), but if you want to ignore any / inside of your string value definition just use // so the first forward slash will escape the second one and PHP will treat them as one slash but don't forget to escape dollar sign ( $ ) as well and all special chars or change your quotations from " to ' then you don't have to change anything inside your string value:

<?php
$path = 'F:/www/__DADOS__/__SESSAO/__9987f2bfdfb80bce8fd72402887bb2c50a433ae0__E6nDSMnD7TCY5#47$BNCx0e#47$r#47$ByZJvcyMIazXSYWBWBXN5lgdZOd3Ps#47$ROrVSPl7QVQaCqfa2WezCauk#47$LVFyhgw==.meudominio.com.sessid';
//OR
$path = "F://www//__DADOS__//__SESSAO//__9987f2bfdfb80bce8fd72402887bb2c50a433ae0__E6nDSMnD7TCY5#47/$BNCx0e#47/$r#47/$ByZJvcyMIazXSYWBWBXN5lgdZOd3Ps#47/$ROrVSPl7QVQaCqfa2WezCauk#47/$LVFyhgw==.meudominio.com.sessid";

echo file_exists($path);//will return 1
//unlink($path);
?>

尝试

$path ="F:/www/__DADOS__/__SESSAO/__9987f2bfdfb80bce8fd72402887bb2c50a433ae0__E6nDSMnD7TCY5#47$BNCx0e#47$r#47$ByZJvcyMIazXSYWBWBXN5lgdZOd3Ps#47$ROrVSPl7QVQaCqfa2WezCauk#47$LVFyhgw==.meudominio.com.sessid"

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