如何在上传图片时更改图片名称并用“-”代替 space。
How to change image name and put "-" in place of space, while uploading it.
我正在使用此代码通过 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.";
}
}
?>
我想要这个:如果我上传一张名为 "New York.jpg" 的图片,名称会更改为 "New-York.jpg"
在您的代码中,此处:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
添加:
$target_file = str_replace(' ', '-', $target_file);
替换
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
加上这两行,
$target_filename = str_replace(' ', '_', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $target_filename;
我正在使用此代码通过 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.";
}
}
?>
我想要这个:如果我上传一张名为 "New York.jpg" 的图片,名称会更改为 "New-York.jpg"
在您的代码中,此处:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
添加:
$target_file = str_replace(' ', '-', $target_file);
替换
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
加上这两行,
$target_filename = str_replace(' ', '_', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $target_filename;