从远程服务器和localhost上的PHP中的目录中删除图像文件
我试图弄清楚如何使用 summernote 来自远程服务器和localhost的目录文件夹。
因此,图像成功地从SummerNote编辑器上传,并通过目录路径找到:
C:/xampp/htdocs/user/blog/uploads/img-uploads/154_20220702.png
和c:\ xampp \ htdocs \ user \ user \ blog \ admin \ admin \ editor \ editor \ editor \ editor-dlete.php
代码>代码:
<?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';
}
?>
或
$src = $_POST['src'];
$dir = "../uploads/img-uploads/";
$file_name = str_replace($dir, '', $src);
unlink($file_name);
if ($file_name) {
echo 'File Delete Successfully';
}
警告:unlink():http不允许在 C:\ Xampp \ htdocs \ user \ blog \ admin \ editor \ editor-deiter.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正确的路径必须为c:/xampp/htdocs/blog/uploads/img-uploads/img-uploads/154_20220702.png
,但是使用Unlink($ _ server ['document_root']。'/'。 $ file_name);
该路径包含额外的http:// localhost/user/
in c:/xampp/htdocs/http:// http:// localhost/user/blog/uploads/uploads/uploads/http:/ img-uploads/154_20220702.png
警告: unlink(c:/xampp/htdocshttp://localhost/user/blog/uploads/img-uploads/154_20220702.png): 没有这样的文件或目录 C:\ Xampp \ htdocs \ user \ blog \ admin \ editor \ editor-deiter.php
文件删除成功
jquery-3.6.0.min.js:5177 xhr完成加载:邮局 “ http://localhost/user/blog/admin/editor-delete.php”。
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.phpFile Delete Successfully
jquery-3.6.0.min.js:5177 XHR finished loading: POST
"http://localhost/user/blog/admin/editor-delete.php".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法完全测试此问题,但是我认为您可以更改
editor-dlete.php
这样的脚本。从问题中引用的路径来判断,您需要通过将遍历文件结构横穿文件结构的uploads/img-uploads
socode> chdir
和>来构建绝对路径。 GetCWD
是两个有用的功能。代码中的注释显示了我的意思。显示
文件成功删除
的代码片段是基于一个变量,该变量始终是正确的(有效),而不是来自实际Unlink
操作的更有意义的结果,因此应该使用返回的值来分叉您的程序逻辑。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 intouploads/img-uploads
sochdir
andgetcwd
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 actualunlink
operation so you should use that returned value to fork your program logic.