我正在使用此代码上传PHP图像
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
我想要这个:如果我上传名为&#34; New York.jpg&#34;的图片,名称将更改为&#34; New-York.jpg&#34;
答案 0 :(得分:0)
,在这里:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
添加:
$target_file = str_replace(' ', '-', $target_file);
答案 1 :(得分:0)
替换
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
这2行,
$target_filename = str_replace(' ', '_', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $target_filename;