将视频上传到PHP中的特定文件夹

时间:2016-03-14 08:46:18

标签: php video

所以我有一个Android应用程序可以将选定的视频上传到我的服务器。现在视频上传得很好,但它并没有放到我上传视频时创建的新目录。即使我认为现在 move_uploaded_file 中的位置错误,它仍会上传到ProductVideos。

我希望它创建一个新目录,然后将视频存储在那里。 (新目录有777权限,即使我知道它很糟糕,新目录是在 / var / www / html / ProductVideos / 中创建的)

PHP代码:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];


    $ProductAccountName = $_POST['ProductAccountName'];
    $ProductName = $_POST['ProductName'];

    $NewDirectory = "/var/www/html/ProductVideos/" . $ProductAccountName;
    mkdir($NewDirectory, 0777, true);


    $conn = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "Products");

    $sql_query = "select Product_Name from Product_Details where Product_Name like '$ProductName';";

    $result = mysqli_query($conn, $sql_query);

    if(mysqli_num_rows($result) > 0 ){
    $statement = mysqli_prepare($conn, "UPDATE Product_Details SET 7sec_File_Path = ? WHERE Product_Name = '$ProductName'");

    $FileLocation = $NewDirectory."/".$ProductName;

    mysqli_stmt_bind_param($statement, "s", $FileLocation);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
    }
    mysqli_close($conn);



 $location = "/var/www/html/ProductVideos/".$ProductAccountName."/";
 move_uploaded_file($temp_name, $location.$file_name);
 //move_uploaded_file($location.$file_name,       "/var/www/html/ProductVideos/".$ProductAccountName."/");
 echo   "Uploaded!";
 }else{
 echo "Error";
 }
?>

1 个答案:

答案 0 :(得分:0)

试试这个......

<?php
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        if (isset($_FILES['myFile']))
        {
            $file_name = $_FILES['myFile']['name'];
            $file_size = $_FILES['myFile']['size'];
            $file_type = $_FILES['myFile']['type'];
            $temp_name = $_FILES['myFile']['tmp_name'];
        }

        $ProductAccountName = isset($_POST['ProductAccountName'])?$_POST['ProductAccountName']:rand();
        $ProductName = isset($_POST['ProductName']):$_POST['ProductName']:rand();

        $NewDirectory = "/var/www/html/ProductVideos/" . $ProductAccountName;
        if (!file_exists($NewDirectory)) 
        {
            mkdir($NewDirectory, 0777, true);
        }

        $conn = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "Products");

        $sql_query = "select Product_Name from Product_Details where Product_Name like '$ProductName';";

        $result = mysqli_query($conn, $sql_query);

        if(mysqli_num_rows($result) > 0 )
        {
            $statement = mysqli_prepare($conn, "UPDATE Product_Details SET 7sec_File_Path = ? WHERE Product_Name = ?");

            $FileLocation = $NewDirectory."/".$ProductName;

            mysqli_stmt_bind_param($statement, "s", $FileLocation,$ProductName);
            mysqli_stmt_execute($statement);
            mysqli_stmt_close($statement);
        }
        mysqli_close($conn);

        move_uploaded_file($temp_name, $NewDirectory.$file_name);
        //move_uploaded_file($location.$file_name,       "/var/www/html/ProductVideos/".$ProductAccountName."/");
        echo   "Uploaded!";
     }
     else
     {
        echo "Error";
     }
?>

希望它对你有用。