【发布时间】:2014-05-27 15:01:16
【问题描述】:
编辑:我应该如何实现 str_replace 功能?它不工作。查看下面的代码
我正在制作一个人们可以上传图片的表单。图像正在调整大小,以便它们适合图像轮播。我不知道它是怎么可能的,但是当图像包含空格或标点符号时,图像将不会被保存。当图像不包含空格时,它可以正常工作。有人可以帮帮我吗?
表格部分
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"
enctype="multipart/form-data" name="form1" style="margin:0 auto; width:70%;
color:black; font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
font-size:16px; text-transform:none; font-weight:normal" >
<label for="afb1" style="color:black;">
afbeelding 1 (max 5Mb)
</label>
<input type="file" accept="image/*" name="afb1" id="afb1" style="float:right">
php部分
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["afb1"]["name"]);
$extension1 = end($temp);
$newfilename1="afbeelding1.".$extension1;
if ((($_FILES["afb1"]["type"] == "image/gif")
|| ($_FILES["afb1"]["type"] == "image/jpeg")
|| ($_FILES["fb1"]["type"] == "image/jpg")
|| ($_FILES["afb1"]["type"] == "image/pjpeg")
|| ($_FILES["afb1"]["type"] == "image/x-png")
|| ($_FILES["afb1"]["type"] == "image/png"))
&& ($_FILES["afb1"]["size"] < 10000000)
&& in_array($extension1, $allowedExts)) {
if ($_FILES["afb1"]["error"] > 0) {
echo "Return Code: " . $_FILES["afb1"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["afb1"]["name"] . "<br>";
echo "Type: " . $_FILES["afb1"]["type"] . "<br>";
echo "Size: " . ($_FILES["afb1"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["afb1"]["tmp_name"] . "<br>";
if (file_exists('../upload/afbeeldingen/' . $newfilename1)) {
echo $_FILES["afb1"]["name"] . " already exists. ";
} else {
$images_orig = ImageCreateFromJPEG($_FILES["afb1"]["tmp_name"]);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
if ($photoY < $photoX*240/400) {
$width=400; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($_FILES["afb1"]["tmp_name"]);
$height=round($width*$size[1]/$size[0]);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,'../upload/afbeeldingen/'.$newfilename1);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
else {
$height=240; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($_FILES["afb1"]["tmp_name"]);
$width=round($height*$size[0]/$size[1]);
$images_fin = ImageCreateTrueColor(400, 240);
ImageCopyResampled($images_fin, $images_orig, (400-$width)/2, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,'../upload/afbeeldingen/'.$newfilename1);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}
}
} else {echo "Invalid file";}
第 2 版:
喜欢这样吗?它不工作
$nameafb1=$_FILES["afb1"]["name"];
$search = array(" ","_","(",")","!","@","#","$","%","^","&","*","-","+","=",";",":",">","'","/","<","?");
$nameafb1new = str_replace($search,"",$nameafb1);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $nameafb1nieuw);
$extension1 = end($temp);
$newfilename1="afbeelding1.".$extension1;
if ((($_FILES["afb1"]["type"] == "image/gif")
|| ($_FILES["afb1"]["type"] == "image/jpeg")
|| ($_FILES["fb1"]["type"] == "image/jpg")
|| ($_FILES["afb1"]["type"] == "image/pjpeg")
|| ($_FILES["afb1"]["type"] == "image/x-png")
|| ($_FILES["afb1"]["type"] == "image/png"))
&& ($_FILES["afb1"]["size"] < 10000000)
&& in_array($extension1, $allowedExts)) {
if ($_FILES["afb1"]["error"] > 0) {
echo "Return Code: " . $_FILES["afb1"]["error"] . "<br>";
} else {
echo "Upload: " . $nameafb1new . "<br>";
echo "Type: " . $_FILES["afb1"]["type"] . "<br>";
echo "Size: " . ($_FILES["afb1"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["afb1"]["tmp_name"] . "<br>";
if (file_exists('../upload/afbeeldingen/' . $newfilename1)) {
echo $_FILES["afb1"]["name"] . " already exists. ";
} else {
$images_orig = ImageCreateFromJPEG($_FILES["afb1"]["tmp_name"]);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
if ($photoY < $photoX*240/400) {
$width=400; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($_FILES["afb1"]["tmp_name"]);
$height=round($width*$size[1]/$size[0]);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,'../upload/afbeeldingen/'.$newfilename1);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
else {
$height=240; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($_FILES["afb1"]["tmp_name"]);
$width=round($height*$size[0]/$size[1]);
$images_fin = ImageCreateTrueColor(400, 240);
ImageCopyResampled($images_fin, $images_orig, (400-$width)/2, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,'../upload/afbeeldingen/'.$newfilename1);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}
} } else {echo "Invalid file";}
【问题讨论】:
-
尝试对文件名使用
trim()函数或str_replace() -
你能告诉我怎么做吗?
-
试试
$nameafb1=trim($_FILES["afb1"]["name"]); -
非常感谢!它现在工作正常!
-
不客气。我已经在下面为您发布了答案。如果您不了解 StackOverflow 系统的工作原理,只需勾选旁边的白色复选标记直到它变为绿色,即可正确关闭问题。