0

我对这段代码有一个难以置信的问题。此代码为 mp4、mp3 运行。但是当它是一个 3gp 文件时,数据会转到数据库表中,但没有 3gp 文件上传到远程服务器。请告诉我哪里错了:

<?php
$conn = mysql_connect("localhost","root","0123");
if(!$conn)
  {
   print"Could not connect to the server " .mysql_error();
  }
$db = mysql_select_db ("data", $conn);

  if(!$db)
     {
     print"Could not connect to the Database " .mysql_error();
     }
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=yes" name="viewport">

<title >My Choice</title>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>
    <body>
    <div class="main">


    <form action="index.php" method="post" enctype="multipart/form-data">

<p style="color:#663366"><h><br>Post Your audio/video title : </h2></p>
<input type="text" name="title" />
<p style="color:#333366"><br><br>
Select audio/video:</p> <input type="file" id="file" name="file"/><br><br>
<input align="middle" type="submit" name="submit" value="Submit"><br><br>
</form>

<?php

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

if(isset($_POST['submit'])) {
    $allowedExts = array("mp3","mp4", "3gp", "3gpp");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if(($_FILES["file"]["type"] == "video/mp4") ||($_FILES["file"]["type"] == "audio/mpeg")|| ($_FILES["file"]["type"] == "video/3gpp") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) {
        if($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if(file_exists("upload/" . $_FILES["file"]["name"])) {
                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }
}

$dir = $dir.$_FILES['file']['name'];
$sql = "insert into 3gp values ('','$title','$dir')";
$send = mysql_query($sql);
if($send)
echo "<br> <b>It is posted for all. </b>";


?>  </div>
    </html>
4

2 回答 2

1

你的代码已经坏了。$_FILES[$file]["type"]是没有意义的。它由上传文件的浏览器提供,而不是由服务器提供,因此经常丢失或出错。它应该被忽略。

将初始条件更改为简单:

if (($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) {

它仍然会检查数组中文件的扩展名。

这应该工作:)

于 2013-09-18T09:34:40.690 回答
0
if(($_FILES["file"]["type"] == "video/mp4") ||($_FILES["file"]["type"] == "audio/mpeg")|| ($_FILES["file"]["type"] == "video/3gpp") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) {

改成:

if(($_FILES["file"]["type"] == "video/mp4") ||($_FILES["file"]["type"] == "audio/mpeg")|| ($_FILES["file"]["type"] == "video/3gpp") || ($_FILES["file"]["type"] == "video/x-3gp") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts)) {

希望video/x-3gp会成功,请告诉我结果!:)

于 2013-09-18T09:26:33.380 回答