从远程服务器和localhost上的PHP中的目录中删除图像文件

发布于 2025-02-12 20:40:50 字数 1942 浏览 1 评论 0原文

我试图弄清楚如何使用 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.php

File Delete Successfully

jquery-3.6.0.min.js:5177 XHR finished loading: POST
"http://localhost/user/blog/admin/editor-delete.php".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2025-02-19 20:40:50

我无法完全测试此问题,但是我认为您可以更改editor-dlete.php这样的脚本。从问题中引用的路径来判断,您需要通过将遍历文件结构横穿文件结构的uploads/img-uploads so code> chdir>来构建绝对路径。 GetCWD是两个有用的功能。代码中的注释显示了我的意思。

显示文件成功删除的代码片段是基于一个变量,该变量始终是正确的(有效),而不是来自实际Unlink操作的更有意义的结果,因此应该使用返回的值来分叉您的程序逻辑。

<?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';
    }
?>

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';
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文