如何复制图像';s的名称


How to copy an image's name using php

有一个上传脚本,我可以在其中删除一个图像,我必须手动将其名称写在表单的输入框中。我直接从服务器中的文件夹中读取图像的名称。我想通过单击图像或要删除的每个图像的名称来复制要删除的图像的名称。解释起来并不容易,我会给你看代码:

第2版:其工作原理:

    <?php


if ( $_SERVER["REQUEST_METHOD"] == 'GET' ) {
    if (isset($_GET['name']) && $_GET['name'] != '') {
        $img_file = $_GET['name'];
    if($img_file){
        unlink("img/$img_file");
    header('Location: index.php');
}
}
}
?>
<?php
if(isset($_POST['upload_img'])){
$file_name = $_FILES['image']['name'];
$file_type = $_FILES['image']['type'];      
$file_size = $_FILES['image']['size'];
$file_tmp_name  = $_FILES['image']['tmp_name'];
if($file_name){
            move_uploaded_file($file_tmp_name,"img/$file_name");
            }
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label>upload image </label><br>
<input type="file" name="image"><br>
<input type="submit" value="Upload Image" name="upload_img">
</form>

<?php
$folder = "img/";
if(is_dir($folder)){
        if($handle = opendir($folder)){
            while(($file=readdir($handle)) != false){
            if($file==='.' || $file==='..') continue;
echo '<a href="index.php?name='.$file.'">';
echo '<img src="img/'.$file.'" width="150" height="150" alt="">';
echo '</a>';
 }
 closedir($handle);
} 
}
echo '<br>'.$file_names;
?>

</body>

您需要将img标记包装在锚标记中,如

假设您当前的脚本名为mypics.php,则可以执行此操作。

echo '<a href="mypics.php?name="' . $file . '">';
echo '<img src="img/'.$file.'" width="150" height="150" alt="">';
echo '</a>';

现在,在当前脚本的顶部添加

<?php
// picture clicks will be returned as GET requests
if ( $_SERVER["REQUEST_METHOD"] == 'GET' ) {
    if (isset($_GET['name']) && $_GET['name'] != '') {
        // do any sanity checks that you are only 
        // deleting a sensible file name etc
        // delete the file
}

// your existing script follows, which should re-paint the page
// without the deleted image

现在,您可以删除用于输入名称并以这种方式处理删除的代码

您可以将img标记放入a标记中,并带有一个指向删除文件的脚本的链接,类似于:

echo <<< EOF
<a href="deleteFile.php?fileToDelete=$file"> <img src="img/$file" width="150" height="150" alt=""> </a>
EOF;

然后创建一个名为deleteFile.php 的文件

if(isset($_GET['fileToDelete')){
//NOTE: Make SURE you filter and restrict the content of $_GET['fileToDelete'), or may get all the files removed...
//remove the file here
}