简体   繁体   中英

PHP move_uploaded_file to upload video into MySQL not working

I want to upload a video into my server, right now I'm testing locally using XAMPP in my Macbook. I've a column named "path" in my database, this is meant to store the path and name of the file. After my code ends it saves a $path into the path column this part works perfectly, yet it doesn't moves the file.

I've already given 777 permissions to the folder and it still doesn't move the file, and of course my PHP file is in the same folder as my destination in this case "material/videos/"

This is how my code looks.

`

<?php

function uploadVideo($target_dir, $id1) {

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))

&& ($_FILES["file"]["size"] < 500000000)
&& 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("material/videos/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
// EVEN THOUGH THIS IS IN SAME ELSE CONDITION THE MOVE IS NOT DONE
      move_uploaded_file($_FILES["file"]["tmp_name"], "material/videos/");
      echo "Stored in: " . "material/videos/" . $_FILES["file"]["name"];

//HERE IS WHERE I RETURN $PATH CORRECTLY TO SAVE IT INTO DATABASE
      $path = "material/videos/" . $_FILES["file"]["name"];
      return $path;
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}

?>`

you have to specify the whole path in move_uploaded_file . This includes the new filename. ie:

move_uploaded_file($_FILES["file"]["tmp_name"], "material/videos/".$_FILES["file"]["name"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM