如何使用 php 复制图像名称
How to copy an image's name using php
有一个上传脚本,我可以在其中删除图像 我必须在 form.I 的输入框中手动写入其名称 从我服务器的文件夹中直接读取图像的名称。我想通过单击图像或单击要删除的每个图像的名称来复制要删除的图像的名称。不好解释,我给大家看代码:
EDIT2: 工作正常:
<?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
标签放在 a
标签内,并使用 link 到删除文件的脚本,例如:
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
}
您需要像这样将您的 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
您现在可以删除用于输入姓名并以这种方式处理删除的代码
有一个上传脚本,我可以在其中删除图像 我必须在 form.I 的输入框中手动写入其名称 从我服务器的文件夹中直接读取图像的名称。我想通过单击图像或单击要删除的每个图像的名称来复制要删除的图像的名称。不好解释,我给大家看代码:
EDIT2: 工作正常:
<?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
标签放在 a
标签内,并使用 link 到删除文件的脚本,例如:
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
}
您需要像这样将您的 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
您现在可以删除用于输入姓名并以这种方式处理删除的代码