How to upload a video using php?

大憨熊 提交于 2020-02-26 03:42:55

问题


So I want to upload a video to my folder as well. Before this, I can upload any images to the folder but now I want to upload a video but I don't know how because I'm new to PHP. When I try with the following PHP code, and upload a video, A lot of errors come out like these:

Warning: exif_read_data(): Unable to open file in C:\xampp\htdocs\Uploading_System Complete\index.php on line 60

Warning: getimagesize(uploads/259949.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\Uploading_System Complete\index.php on line 103

Notice: Undefined variable: im in C:\xampp\htdocs\Uploading_System Complete\index.php on line 105

Warning: imagesx() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Uploading_System Complete\index.php on line 105

Notice: Undefined variable: im in C:\xampp\htdocs\Uploading_System Complete\index.php on line 106

Warning: imagesy() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Uploading_System Complete\index.php on line 106

Notice: Undefined variable: im in C:\xampp\htdocs\Uploading_System Complete\index.php on line 125

Warning: imagecopyresampled() expects parameter 2 to be resource, null given in C:\xampp\htdocs\Uploading_System Complete\index.php on line 125

And this is my code for the PHP:

if (isset($_POST['btn-add'])) {
    $name     = $_POST['user_name'];
    $images   = $_FILES['profile']['name'];
    $path     = "uploads/";
    $rand     = rand(1000, 1000000);
    $filename = "$rand.jpg";
    $filepath = "$path$filename";


    function correctImageOrientation($filename)
    {
        if (function_exists('exif_read_data')) {
            $exif = exif_read_data($filename);
            if ($exif && isset($exif['Orientation'])) {
                $orientation = $exif['Orientation'];
                if ($orientation != 1) {
                    $img = imagecreatefromjpeg($filename);
                    $deg = 0;
                    switch ($orientation) {
                        case 3:
                            $deg = 180;
                            break;
                        case 6:
                            $deg = 270;
                            break;
                        case 8:
                            $deg = 90;
                            break;
                    }
                    if ($deg) {
                        $img = imagerotate($img, $deg, 0);
                    }
                    // then rewrite the rotated image back to the disk as $filename 
                    imagejpeg($img, $filename, 95);
                } // if there is some rotation necessary
            } // if have the exif orientation info
        } // if function exists      
    }

    $imgExt = strtolower(pathinfo($images, PATHINFO_EXTENSION));
    if (move_uploaded_file($_FILES["profile"]["tmp_name"], $filepath)) {
        $images = imagecreatefromjpeg($filepath);
    }

    correctImageOrientation($filepath);


    if ($imgExt == "jpeg" || $imgExt == "jpg") {
        $im = imagecreatefromjpeg($filepath);
    } elseif ($imgExt == "png") {
        $im = imagecreatefrompng($filepath);
    } elseif ($imgExt == "gif") {
        $im = imagecreatefromgif($filepath);
    }

    $size = getimagesize($filepath);

    $width     = imagesx($im);
    $height    = imagesy($im);
    $newwidth  = 300;
    $newheight = 300;


    if (($width / $newwidth) < ($height / $newheight)) {
        $y = 0;
        $x = $width - (($height * $newwidth) / $newheight);
    } else {
        $x = 0;
        $y = $height - (($width * $newheight) / $newwidth);
    }

    $virtual_image = imagecreatetruecolor(301, 301);
    imagealphablending($virtual_image, true);
    imagesavealpha($virtual_image, true);


    imagecopyresampled($virtual_image, $im, 0, 0, $x / 2, $y / 2, $newwidth, $newheight,
        $width - $x, $height - $y);
    $bgcolor = imagecolorallocatealpha($virtual_image, 255, 255, 255, 127);

    imagefill($virtual_image, 0, 0, $bgcolor);

    imagecolortransparent($virtual_image, $bgcolor);


    imagejpeg($virtual_image, $filepath, 100);


    $stmt = $db_conn->prepare('INSERT INTO tbl_user(username, picprofile) VALUES (:uname, :upic)');
    $stmt->bindParam(':uname', $name);
    $stmt->bindParam(':upic', $filename);
    if ($stmt->execute()) {
        ?>
        <script>

        </script>
        <?php

    } else {
        ?>
        <script>

        </script>
        <?php
    }

}
?>


<script>
    function unlock() {
        document.getElementById('submit').removeAttribute("disabled");
    }
</script>

I just want to know how to upload video as well. Thanks

来源:https://stackoverflow.com/questions/59484902/how-to-upload-a-video-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!