使用PHP脚本上传图像时出现严格标准错误 [英] Strict Standards Error while image upload with PHP script
问题描述
我尝试用php写一个图像上传器.但是当我尝试时,它给出了一个错误.
I try to write an image uploader with php. But it is giving an error when I try.
错误是:
Error is:
严格的标准:仅在第10行的C:\ xx \ xx \ xx \ profile_image_upload_script.php中通过引用传递变量
第10行是: $extension = end(explode(".", $file_name_encrypted));
图片上传脚本
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$file_name = $_FILES["file"]["name"];
echo "File name:".$file_name;
$file_name_encrypted = $file_name."".md5(rand(1, 1000000));
$extension = end(explode(".", $file_name_encrypted));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2097152) // 2 MB
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $file_name_encrypted . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024*1024) . " MB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $file_name_encrypted))
{
echo $file_name_encrypted . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $file_name_encrypted);
echo "Stored in: " . "upload/" . $file_name_encrypted;
}
}
}
else
{
echo "Invalid file";
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
?>
注意:脚本正在从html表单获取文件名,没有问题
Note: script is getting file name from html form, there is no problem
推荐答案
注意$file_name_encrypted
将不包含 real 或 matchable 扩展名,因为您的附加带有md5的文件名:
Note $file_name_encrypted
will not contain a real or matchable extension, because your appending the filename with md5:
$file_name_encrypted = $file_name."".md5(rand(1, 1000000));
例如filename.jpg79054025255fb1a26e4bc422aef54eb4
因此它将永远不会与您的$allowedExts
数组中的任何内容匹配.然后解决该问题:
So it will never match any in your $allowedExts
array. Fix that then:
也更改该行:
$extension = pathinfo($file_name_encrypted, PATHINFO_EXTENSION);
或者爆炸,然后将爆炸传递给end()函数.
Or explode then pass the exploded to the end() function.
$temp = explode(".", $file_name_encrypted);
$extension = end($temp);
这篇关于使用PHP脚本上传图像时出现严格标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!