Sayuj Raghavan
Sayuj Raghavan

Reputation: 337

uploading videos to server using PHP

I am developing a website which features, users can upload their videos to the website and the videos goes to the server. It perfectly working no problem. But i want to make the below code differently ,when a user uploads the video,it must get stored to new folder in server by creating tht new folder directory(with name as username) and i must be able to fetch it from the server by embedding the video in their respective profiles. Below the snippets are commonly used for uploading by creating an upload folder in the server(upload folder was created manually,I want it in a way tht when user uploads the video a new directly must be formed autonomously in server with name as of the username on it) please help.....

       video.php
      ----------
       <form action="videophp.php" method="post" enctype="multipart/form-data">
       <label for="file"><span>Filename:</span></label>
       <input type="file" name="file" id="file" /> 
       <br />
       <input type="submit" name="submit" value="Upload" />
       </form>

        videophp.php
       -------------
       <?php

       $allowedExts = array("mp4", "flv","jpg","JPEG");
       $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

       if ((($_FILES["file"]["type"] == "video/mp4")

       || ($_FILES["file"]["type"] == "video/flv")
       || ($_FILES["file"]["type"] == "image/jpg")
       || ($_FILES["file"]["type"] == "image/JPEG")


         )

          && ($_FILES["file"]["size"] > 2000000)
          && 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";
        }
    ?>

Upvotes: 0

Views: 5426

Answers (2)

Atul Kumar Srivastava
Atul Kumar Srivastava

Reputation: 91

     $uploaddir = 'uploads/';
   $filename = basename($_FILES['media']['name']);

    $raw = $uploaddir . $filename;
 $_FILES['media']['tmp_name'];
   if (move_uploaded_file($_FILES['media']['tmp_name'], $raw))
        $url = "http://".$_SERVER['HTTP_HOST']."/newsapp/".$raw;

Upvotes: 0

Jeff
Jeff

Reputation: 6953

This is one part of what you're looking for:

$username = 'Max Mustermann';    // you'd have to get that from somewhere
$targetfolder = 'upload/'.$username;

if (!file_exists($targetfolder)) {
    mkdir($targetfolder, 0777, true);
}
if (file_exists($targetfolder . $_FILES["file"]["name"])) {
     echo $_FILES["file"]["name"] . " already exists. ";
}
else {
    move_uploaded_file($_FILES["file"]["tmp_name"],
        $targetfolder . $_FILES["file"]["name"]);

    echo "Stored in: " . $targetfolder . $_FILES["file"]["name"];
}

You still need to send a username to the server. With that you can also access is on his next visit.

BUT I recommend to give the folder and the file other names and store them in db, connected to the user's data. This way you have better control of who can access what file (video), you don't have to search the filesystem (which is slower then searching n db).

Upvotes: 2

Related Questions