所以我有这个HTML文件上传表单,后端如下:
if(isset($_POST['two'])){
$target_dir = "pageimg/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 0;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
$uploadOk = 0;
} else {
$uploadOk = 1;
}
// Check if file already exists
if (file_exists($target_file)) {
$uploadOk = 2;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
$uploadOk = 3;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 4;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 5) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
这样可以正常工作并上传文件,但是,如果我上传FB Logo.png这样的文件,它会按照空格上传。我想编辑此脚本以关闭任何空格或重命名图像?我环顾四周,发现如何在当前脚本中实现任何脚本没有运气?这一切看起来有点像“这是代码”自己解决,我没有运气。我试过这个......
str_replace(" ", "_", $_FILES['image']['name']);
但不太确定我是否正确添加了它?图像无法在测试时上传。
另外,我怎么会回想起图像名称?我会用:
$_FILES['name'];
只是为了回忆图像名称?