文件上传无法正常工作

时间:2014-05-27 15:01:17

标签: php file upload filenames

编辑:我应该如何实现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";}

1 个答案:

答案 0 :(得分:1)

有几种方法可以实现这一目标。

您可以使用PHP的trim()功能。

$nameafb1=trim($_FILES["afb1"]["name"]);

这将从开头和/或结尾“修剪”不需要的空格。

或PHP的str_replace()函数,它能够替换找到的所有空格:

$string = " hello world ";
$string = str_replace(' ', '', $string);
 echo $string;

所以在你的情况下会是:

$nameafb1=$_FILES["afb1"]["name"];

$nameafb1 = str_replace(' ', '', $nameafb1);

您还可以使用下划线替换空格:

$nameafb1=$_FILES["afb1"]["name"];

$nameafb1 = str_replace(' ', '_', $nameafb1);

另一种选择是使用preg_replace()功能:

$nameafb1 = preg_replace('/\s+/', '_', $nameafb1);

将空格替换为下划线。您可以删除'_'中的下划线,如果不需要,可将其更改为''