解决方案
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$file = ($_FILES["file"]["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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"]["type"] == "audio/mpeg")
)
&& ($_FILES["file"]["size"] < 209715200)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}
非工作代码
我已经调整了一个脚本,用于将图像上传到文件夹以上传音频,但音频文件无法保存 - 无论是进入表格的路径还是进入文件夹的音频。 如何更改此选项以检查所有文件的大小而不仅仅是图像? getfilesize给出一个函数不存在的错误?
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
//function to check for valid image formats
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$file = getimagesize($_FILES["file"]["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($file["mime"] == "image/gif")
|| ($file["mime"] == "image/jpeg")
|| ($file["mime"] == "image/jpg")
|| ($file["mime"] == "image/pjpeg")
|| ($file["mime"] == "image/x-png")
|| ($file["mime"] == "image/png")
|| ($file["mime"] == "audio/mp3")
)
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}
答案 0 :(得分:2)
试试这个:
<?php
include "connect.php";
require "authenticate.php";
error_reporting(E_ERROR);
$message = $_GET['message'];
//function to check for valid image formats
function uploadfile($dir){
if(!empty($_FILES)){
$url ='';
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "PNG", "GIF", "mp3");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($file["file"]["type"] == "image/gif")
|| ($file["file"]["type"] == "image/jpeg")
|| ($file["file"]["type"] == "image/jpg")
|| ($file["file"]["type"] == "image/pjpeg")
|| ($file["file"]["type"] == "image/x-png")
|| ($file["file"]["type"] == "image/png")
|| ($file["file"]["type"] == "audio/mp3")
)
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = $dir . $_FILES["file"]["name"];
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$dir . $_FILES["file"]["name"]);
$path = $dir . $_FILES["file"]["name"];
}
}
}
else
{
$message = "Wrong format";
}
}
return $path;
}