I'm trying to figure out, how to delete correctly image file with summernote from directory folder on remote server and localhost.
So, image successfully uploaded from summernote editor and located by directory path:
C:/xampp/htdocs/user/blog/uploads/img-uploads/154_20220702.png
and C:\xampp\htdocs\user\blog\admin\editor-delete.php
code:
<?php
$src = $_POST['src'];
$path = "../uploads/img-uploads/";
$dir = basename($path);
$file_name = str_replace($dir, '', $src);
unlink($file_name);
if ($file_name) {
echo 'File Delete Successfully';
}
?>
or
$src = $_POST['src'];
$dir = "../uploads/img-uploads/";
$file_name = str_replace($dir, '', $src);
unlink($file_name);
if ($file_name) {
echo 'File Delete Successfully';
}
Warning: unlink(): http does not allow unlinking in C:\xampp\htdocs\user\blog\admin\editor-delete.php
$src = $_POST['src'];
$dir = "../uploads/img-uploads/";
$file_name = str_replace($dir, '', $src);
unlink($_SERVER['DOCUMENT_ROOT'] . $file_name);
if ($file_name) {
echo 'File Delete Successfully';
}
Localhost correct path must be C:/xampp/htdocs/blog/uploads/img-uploads/154_20220702.png
, but with unlink($_SERVER['DOCUMENT_ROOT'] .'/'. $file_name);
the path contains extra http://localhost/user/
in C:/xampp/htdocs/http://localhost/user/blog/uploads/img-uploads/154_20220702.png
Warning: unlink(C:/xampp/htdocshttp://localhost/user/blog/uploads/img-uploads/154_20220702.png): No such file or directory in C:\xampp\htdocs\user\blog\admin\editor-delete.php
File Delete Successfully
jquery-3.6.0.min.js:5177 XHR finished loading: POST "http://localhost/user/blog/admin/editor-delete.php".
CodePudding user response:
I cannot completely test this but I think you could change the editor-delete.php
script like this. Judging by the paths cited in the question you need to construct the absolute path by leaving the admin directory traversing the file structure into uploads/img-uploads
so chdir
and getcwd
are two useful functions for this. The notes in the code show what I mean.
The piece of code that displays the File Delete Successfully
is based upon a variable which will always be true (effectively) rather than the more meaningful result from the actual unlink
operation so you should use that returned value to fork your program logic.
<?php
/*
C:\xampp\htdocs\user\blog\admin\editor-delete.php
so:
chdir('../') leads to C:\xampp\htdocs\user\blog\
chdir('../uploads/img-uploads') should lead to C:\xampp\htdocs\user\blog\uploads\img-uploads
*/
if( isset( $_POST['src'] ) ){
# Change working directory for current script operations
# - go up one level out of "admin" and into the uploads folder.
chdir('../uploads/img-uploads');
# default value for use later in output message
$status=false;
# construct the full path to local file using getcwd() to find working directory.
$path=sprintf( '%s/%s', getcwd(), basename( $_POST['src'] ) );
# if the file exists - delete it or warn ( inspect the path! )
if( file_exists( $path ) )$status=@unlink( $path );
else exit( sprintf( 'Unable to find file: "%s"', $path ) );
# prevent cached results tainting file checks
clearstatcache();
echo $status ? 'File deleted successfully' : 'Sorry, that file could not be deleted';
}
?>