使用PHP脚本上传图片时发生严格的标准错误 [英] Strict Standarts Error while image upload with PHP script
问题描述
我尝试用php编写图片上传器。但是,当我尝试时发生错误。
错误是:
$ b
严格标准:只有变量应该通过引用传递给第10行的C:\xx\xx\xx\profile_image_upload_script.php
第10行是: $ extension = end(explode(。,$ file_name_encrypted)); / code>
图片上传脚本
<?php
$ allowedExts = array(gif,jpeg,jpg,png);
$ file_name = $ _FILES [file] [name];
echo文件名:。$ 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] ==图片/ 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
& amp ;& in_array($ extension,$ allowedExts))
{
if($ _FILES [file] [error]> 0)
{
echo返回代码:。 $ _FILES [file] [error]。 <峰; br> 中;
}
else
{
echoUpload:。 $ file_name_encrypted。 <峰; br> 中;
回显类型:。 $ _FILES [file] [type]。 <峰; br> 中;
回声大小:。 ($ _FILES [file] [size] / 1024 * 1024)。 MB< br>;
回显Temp file:。 $ _FILES [file] [tmp_name]。 <峰; br> 中;
if(file_exists(upload /。$ file_name_encrypted))
{
echo $ file_name_encrypted。 已经存在。 ;
}
else
{
move_uploaded_file($ _ FILES [file] [tmp_name],upload /。$ file_name_encrypted);
回声存储于:。 上传/。 $ file_name_encrypted;
}
}
}
else
{
echo文件无效;
回显返回代码:。 $ _FILES [file] [error]。 <峰; br> 中;
}
?>
注意:脚本正在从html表单获取文件名,不存在问题
注意 $ file_name_encrypted
不会包含 real 或 matchable 扩展名,因为您的将文件名追加到md5:
$ file_name_encrypted = $ file_name。 .md5(rand(1,1000000));
例如 也改变这一行: 或爆炸,然后将爆炸传递给end()函数。 I try to write an image uploader with php. But it is giving an error when I try. Error is: Strict Standards: Only variables should be passed by reference in C:\xx\xx\xx\profile_image_upload_script.php on line 10 Line 10 is: image upload script Note:script is getting file name from html form, there is no problem Note e.g So it will never match any in your Change that line too: Or explode then pass the exploded to the end() function.
这篇关于使用PHP脚本上传图片时发生严格的标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! filename.jpg79054025255fb1a26e4bc422aef54eb4 $ c $因此它永远不会匹配你的
$ allowedExts
数组中的任何内容。修正那个:
$ extension = pathinfo($ file_name_encrypted,PATHINFO_EXTENSION);
$ temp = explode(。,$ file_name_encrypted);
$ extension = end($ temp);
$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>";
}
?>
$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
array. Fix that then:$extension = pathinfo($file_name_encrypted, PATHINFO_EXTENSION);
$temp = explode(".", $file_name_encrypted);
$extension = end($temp);