【问题标题】:File upload not working correctly文件上传无法正常工作
【发布时间】: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 系统的工作原理,只需勾选旁边的白色复选标记直到它变为绿色,即可正确关闭问题。

标签: php file upload filenames


【解决方案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);

这会将空格替换为下划线。你可以删除'_'中的下划线,如果你不想要它们,把它改成''

【讨论】:

  • 谢谢杰。它没有显示 + @JayBlanchard 但非常感谢:) 我猜你已经达到了当天的最高投票数(?)
猜你喜欢
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多