我尝试用 php 编写一个图像上传器。但是当我尝试时它给出了一个错误。
错误是:
严格的标准:只有变量应该通过引用在第 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表单中获取文件名,没有问题