我正在尝试为图像文件名添加编辑选项,并删除图像的选项.所有图像都在一个文件夹中,我根本没有使用过数据库.我能够显示图像文件名以及用于编辑和删除每个图像名称旁边的按钮.
但我不确定如何前进.我知道PHP有一个unlink()和rename()功能.但我不确定如何动态包含这些功能.
<?php
echo "<div class='container'>";
if(isset($_POST["submit"])) {
$files = glob("images/Kwebsite/". $_POST['path']."/*.*");
}
echo "<div class='col l5'>";
echo "<h3>Image Filename Actions</h3>";
echo "</div>";
for ($i=0; $i<count($files); $i++){
$num = $files[$i];
$filenamewithextension = basename($num);
$fileextenstion = pathinfo($filenamewithextension, PATHINFO_EXTENSION);
$filename = basename($num,$fileextenstion);
echo '<div class="imagename">';
echo $filename;
echo '<button type="submit" class="btn-flat" value="Delete" onClick="Delete()">Delete</button>';
if(isset($_POST["submit"])) {
$imgfile = glob("images/Kanishk website/". $_POST['path']."/*.*");
foreach $imgfile as $img {
unlink($img)
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
您必须将文件路径传递给unlink().如果要删除多个文件,则必须多次调用unlink.如果将文件路径推送到数组,则可以遍历路径.
$paths['/files/file1.jpg','/files/file2.jpg']
foreach $paths as $file {
unlink($file)
}
Run Code Online (Sandbox Code Playgroud)
如果要重命名文件,还必须提供完整路径,否则您将在其他位置创建新文件:
rename ("/files/file1.jpg", "/files/file_1.jpg");
Run Code Online (Sandbox Code Playgroud)
该信息必须由用户提供,至少在大多数情况下是这样.我建议将路径和文件名存储在数据库中.
首先,这是一件危险的事情,你应该避免这样做。想象一下,如果有人提供像这样的升级点组合,会发生什么../../;该脚本将列出 2 级以上的所有文件。为了防止这种行为,我们需要检查path输入字符串中是否有斜杠和反斜杠glob,如果发现某些斜杠,则不执行该函数。这是验证查询的正则表达式path:
// Check for backslash, level up dir .. and wildcards
$isValidPath = !preg_match('#([\\\.\?\*])#', $path);
Run Code Online (Sandbox Code Playgroud)
我根据你的代码做了一个非常简单的例子。所有命令都通过将表单数据发布到服务器(而不是 AJAX)来传递给 PHP。当用户单击“删除”按钮时,会出现一条确认消息,如果用户单击“确定”,则表单会发布删除按钮数据,即“它”name="delete"和“ value="path/filename.ext". 我们将$_POST['delete'] == "path/filename.ext"在 PHP 中获得价值。如果我们在 POST 数据中检测到delete,则调用unlink并删除该文件。对于重命名功能,我们使用相同的方法,但这次使用了 javascript 提示符,它会提示并要求用户输入新的文件名。如果新文件名与原始文件名不同,则会使用新文件名更新隐藏字段,并将表单字段发布到我们的 PHP 服务器脚本;POST 数据将具有函数rename和$_POST['rename'] == "path/oldfilename.ext"的这些值$_POST['renameto'] == "newfilename.ext"。然后我们只需在脚本开头调用重命名函数即可。
提示:使用 PHP函数在 HTML 中print_r打印数组,以调试每个页面刷新时的发布数据:$_POST<pre></pre>
<pre><?php print_r($_POST) ?></pre>
Run Code Online (Sandbox Code Playgroud)
最终工作脚本
<pre><?php print_r($_POST) ?></pre>
<?php
$hasPath = isset($_POST['path']);
$basePath = __DIR__."/images/Kwebsite/";
$path = '';
$files = array();
if(isset($_POST['rename']) && isset($_POST['renameto'])) {
$fileToRename = $basePath.$_POST['rename'];
$renameTo = dirname($fileToRename).'/'. $_POST['renameto'];
rename($fileToRename, $renameTo);
}
if(isset($_POST['delete'])) {
$fileToDelete = $basePath.$_POST['delete'];
unlink($fileToDelete);
}
if($hasPath) {
// Check for backslash, level up dir .. and wildcards
$path = $_POST['path'];
$isValidPath = !preg_match('#([\\\.\?\*])#', $path);
if($isValidPath) {
$searchPath = $basePath.$path.'/*.*';
$files = glob($searchPath);
}
}
?>
<form method="post">
<label>Search directory: </label>
<input type="text" name="path" value="<?php echo $path ?>"/>
<button type="submit">Search</button>
<hr>
<table border="1">
<thead>
<tr>
<th>Image Filename</th><th>Actions</th>
</tr>
</thead>
<tbody>
<?php if($hasPath && $isValidPath && count($files) > 0): ?>
<?php foreach($files as $file):
$filenameWithExtension = basename($file);
$fileExtenstion = pathinfo($filenameWithExtension, PATHINFO_EXTENSION);
$filename = pathinfo($filenameWithExtension, PATHINFO_FILENAME);
?>
<tr>
<td><?php echo $filename ?></td>
<td>
<button type="submit" name="delete" value="<?php echo "{$path}/{$filenameWithExtension}" ?>" onClick="Delete(event)">Delete</button>
<button type="submit" name="rename" value="<?php echo "{$path}/{$filenameWithExtension}" ?>" onClick="Rename(event)">Rename</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<input type="hidden" id="renameto" name="renameto"/>
</form>
<script type="text/javascript">
function Delete(e) {
var event = e || window.event,
filename = event.target.value.substr(event.target.value.lastIndexOf('/') + 1);
if(!confirm("Are you sure you want to delete this file '"+filename+"'?"))
event.preventDefault();
}
function Rename(e) {
var event = e || window.event,
oldFilename = event.target.value.substr(event.target.value.lastIndexOf('/') + 1),
newFilename = prompt("Enter a new filename", oldFilename);
if(newFilename != null) {
if(newFilename == oldFilename) {
alert("You must give a different filename");
event.preventDefault();
} else {
document.getElementById('renameto').value = newFilename;
}
} else {
event.preventDefault();
}
}
</script>
Run Code Online (Sandbox Code Playgroud)