Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';无法获取视频和音频的PHP文件扩展名_Php - Fatal编程技术网

我能';无法获取视频和音频的PHP文件扩展名

我能';无法获取视频和音频的PHP文件扩展名,php,Php,我有一个页面可以上传视频图像和音乐文件。 我可以毫无问题地上传图像文件,但当我选择上传视频或音乐文件时,代码甚至无法识别文件扩展名。 此代码在Windows中工作正常,但当我将此代码移到CentOS 6.3时,此代码不适用于视频或音乐文件 <?php session_start(); if (isset($_SESSION['username'])) { $usern = $_SESSION['username']; $fileName = $_FILES["file1"

我有一个页面可以上传视频图像和音乐文件。
我可以毫无问题地上传图像文件,但当我选择上传视频或音乐文件时,代码甚至无法识别文件扩展名。
此代码在Windows中工作正常,但当我将此代码移到CentOS 6.3时,此代码不适用于视频或音乐文件

<?php
session_start();

if (isset($_SESSION['username']))
{
    $usern = $_SESSION['username'];
    $fileName = $_FILES["file1"]["name"];
    $fileTmpLoc = $_FILES["file1"]["tmp_name"];
    $fileType = $_FILES["file1"]["type"];
    $fileSize = $_FILES["file1"]["size"];
    $fileErrorMsg = $_FILES["file1"]["error"];
    $path = $_FILES['file1']['type'];
    $ext = substr($path, strrpos($filename, '.') + 6);

    if ($ext == 'png' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif') {
        if (!$fileTmpLoc) {
            echo "ERROR: Please browse for a file before clicking the upload button";
            exit();
        }

        if (move_uploaded_file($fileTmpLoc, "usersdata/$usern/images/$fileName")) {
            mysql_connect('localhost', 'root', '1') or die(mysql_error());
            mysql_select_db('cloud') or die(mysql_error());
            mysql_query("INSERT INTO images VALUES('','$fileName','usersdata/$usern/images/$fileName','$usern')");

            if (mysql_affected_rows() > 0) {
                echo "upload is complete";
            }

        } else {
            echo "move_uploaded_file function failed";
        }

    } else if ($ext == '.mp4' || $ext == 'avi' || $ext == 'flv') {
        if (!$fileTmpLoc) {
            echo "ERROR: Please browse for a file before clicking the upload button";
            exit();
        }

        if (move_uploaded_file($fileTmpLoc, "usersdata/$usern/video/$fileName")) {
            mysql_connect('localhost', 'root', '1') or die(mysql_error());
            mysql_select_db('cloud') or die(mysql_error());
            mysql_query("INSERT INTO video VALUES('','$fileName','usersdata/$usern/video/$fileName','$usern')");

            if (mysql_affected_rows() > 0) {
                echo "upload is complete";
            }

        } else {
            echo "move_uploaded_file function failed";
        }

    } else if ($ext == 'mpeg') {
        if (!$fileTmpLoc) {
            echo "ERROR: Please browse for a file before clicking the upload button";
            exit();
        }

        if (move_uploaded_file($fileTmpLoc, "usersdata/$usern/musics/$fileName")) {
            mysql_connect('localhost', 'root', '1') or die(mysql_error());
            mysql_select_db('cloud') or die(mysql_error());
            mysql_query("INSERT INTO musics VALUES('','$fileName','usersdata/$usern/musics/$fileName','$usern')");

            if (mysql_affected_rows() > 0) {
                echo "upload is complete";
            }

        } else {
            echo "move_uploaded_file function failed";
        }

    } else {
        echo "The Type You Are Tring To upload not Supported";
    }
}
?>

  • 不要依赖strrpos来提取文件扩展名。你应该改用
  • 您需要以不区分大小写的方式比较文件扩展名。目前,您的脚本将匹配“.jpg”,而不是“.jpg”作为有效扩展名
  • 不要使用很多OR子句,而是将有效扩展存储在数组中并使用来搜索它
  • 例如:

    $image_ext_list = array('jpg', 'jpeg', 'png', 'bmp', 'gif');
    $video_ext_list = array('avi', 'mp4', 'mkv', 'flv');
    $file_path = '/example/file/path/image.jpg';
    
    // Extract the path parts to get the file extension.
    // If there is no extension, $path_parts['extension'] will be null.
    $path_parts = pathinfo($file_path);
    $file_ext = isset($path_parts['extension']) ? $path_parts['extension'] : '';
    
    // Convert the file extension to lower-case so it's not case sensitive.
    $file_ext = strtolower($file_ext);
    
    // Check if the extension is in the list of image extensions
    if (in_array($file_ext, $image_ext_list)) {
        // Uploaded file is an image
    
    } elseif (in_array($file_ext, $video_ext_list)) {
        // Uploaded file is a video
    
    } else {
        // Uploaded file extension isn't in list
    }
    

    什么文件扩展名导致了问题?出现了什么错误?为什么您选中的一个扩展名是
    .mp4
    ,而其他所有扩展名没有
    ?不要使用文件扩展名来确定某人上载的文件类型;他们可以随便叫它什么。您应该使用一个查看文件内容的函数。没有错误,但它会转到视频或音频文件中不支持的要上载的类型,或者,甚至比_array
    中的
    ,一个以扩展名为键的关联数组更好,和作为值键入。很好的解释,但我认为这并不能回答OPs问题。谢谢你的建议,我会更新我的代码,,,但在linux中,我甚至无法访问视频或音频文件信息,,@ArvinSloane你确定你有读取这些文件的适当权限吗?@Kei-你不需要读取文件来检查其扩展名。您只需要能够看到它(更具体地说,它所在的目录)。