使用PHP脚本上传图片时发生严格的标准错误 [英] Strict Standarts Error while image upload with PHP script

查看:112
本文介绍了使用PHP脚本上传图片时发生严格的标准错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试用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));



例如 filename.jpg79054025255fb1a26e4bc422aef54eb4 $ allowedExts 数组中的任何内容。修正那个:

也改变这一行:

  $ extension = pathinfo($ file_name_encrypted,PATHINFO_EXTENSION); 

或爆炸,然后将爆炸传递给end()函数。

  $ temp = explode(。,$ file_name_encrypted); 
$ extension = end($ temp);


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: $extension = end(explode(".", $file_name_encrypted));


image upload script

<?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>";
  }
?>

Note:script is getting file name from html form, there is no problem

解决方案

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));

e.g filename.jpg79054025255fb1a26e4bc422aef54eb4

So it will never match any in your $allowedExts array. Fix that then:

Change that line too:

$extension = pathinfo($file_name_encrypted, PATHINFO_EXTENSION);

Or explode then pass the exploded to the end() function.

$temp = explode(".", $file_name_encrypted);
$extension = end($temp);

这篇关于使用PHP脚本上传图片时发生严格的标准错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆