【发布时间】:2016-09-05 11:56:27
【问题描述】:
我有一个名为upload_file.php 的文件,它将图像文件上传到我的网络服务器。我遇到的问题是我想在上传过程中用下划线替换文件名中的所有空格。我知道这可以使用 str_replace() 来完成,但我不知道在我的代码中应该在哪里使用 str_replace()。任何帮助将不胜感激。
这是我的 upload_file.php 文件中包含的完整代码:
<?php
require_once '../../config.php';
include '../secure.php';
if ($login_status != 1) exit();
if (isset($_GET['done'])) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
if ($_POST['type'] == 'wallpaper') {
$directory = "../../files/";
}
else {
$directory = "../../files/images/";
}
$filename = basename($_FILES['file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (file_exists($directory . $_FILES["file"]["name"])) {
echo htmlspecialchars($_FILES["file"]["name"]) . " already exists. ";
}
elseif (((strpos($ext, "php") !== false) || $ext == 'aspx' || $ext == 'py' || $ext == 'htaccess') && !isset($allow_php_uploads)) {
echo 'Uploading PHP files disabled (<a target="_blank" href=""></a>)';
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $directory . $_FILES["file"]["name"]);
header("Location: upload-complete.php?type=".htmlspecialchars($_POST['type'])."&newfile=". htmlspecialchars($_FILES["file"]["name"])."&id=".intval($_POST['id'])."");
}
}
} else { ?>
<head>
<style type="text/css">
body {
margin:0px;
padding:0px;
}
</style>
</head>
<form action="upload_file.php?done=1" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file" style="max-width:230px;" />
<input name="id" type="hidden" value="<?php echo intval($_GET['id']);?>" />
<input name="type" type="hidden" value="<?php echo htmlspecialchars($_GET['type']);?>" />
<input type="submit" name="submit" value="Upload" />
</form>
<?php } ?>
【问题讨论】:
标签: php