XF 2.1 file_exists issue

grantus

Active member
I have this code:

Code:
$dataDir = \XF::app()->config('externalDataPath');
$fileDirectory = $dataDir. '://ill/upload/';
$filename = $signupUsername.'_'.$signupFilename.'.mp3';

if (file_exists($fileDirectory.$filename)) {
 \XF\Util\File::deleteFromAbstractedPath($fileDirectory.$filename); 
}

but for some reason file_exists isn't working. Deleting the file is fine, and I've checked all the paths, so I'm assuming there's a problem with file_exists checking inside the data folder? Any ideas?
 
Solution
You also shouldn't need to check if the file exists first. If it doesn't, it fails silently.
PHP:
$abstractedPath = "data://ill/upload/{$signupUsername}/{$signupFilename}.mp3";
\XF\Util\File::deleteFromAbstractedPath($abstractedPath);

If you do need to check for file existence, use the mount manager:
PHP:
\XF::fs()->has($abstractedPath)
I just realized that file_exists can't look at data::// so this works instead:

Code:
$dataDir = \XF::app()->config('externalDataPath');
$fileDirectory = $dataDir. '://ill/upload/';
$filename = $signupUsername.'_'.$signupFilename.'.mp3';

if (file_exists('data/ill/upload/'.$filename)) {
 \XF\Util\File::deleteFromAbstractedPath($fileDirectory.$filename);
}

The $fileDirectory is what's needed for deleteFromAbstractedPath to delete the file.
 
You also shouldn't need to check if the file exists first. If it doesn't, it fails silently.
PHP:
$abstractedPath = "data://ill/upload/{$signupUsername}/{$signupFilename}.mp3";
\XF\Util\File::deleteFromAbstractedPath($abstractedPath);

If you do need to check for file existence, use the mount manager:
PHP:
\XF::fs()->has($abstractedPath)
 
Solution
You also shouldn't need to check if the file exists first. If it doesn't, it fails silently.
PHP:
$abstractedPath = "data://ill/upload/{$signupUsername}/{$signupFilename}.mp3";
\XF\Util\File::deleteFromAbstractedPath($abstractedPath);

If you do need to check for file existence, use the mount manager:
PHP:
\XF::fs()->has($abstractedPath)
Interesting. I'll try it out this way. I didn't know that check if it exists is not required, good to know. Thanks!
 
Top Bottom