我想将视频上传到我的服务器,现在我正在Macbook中使用XAMPP进行本地测试。我的数据库中有一个名为“path”的列,用于存储文件的路径和名称。在我的代码结束后,它将$ path保存到路径列中,这部分可以完美地工作,但它不会移动文件。
我已经给了该文件夹777的权限,它仍然没有移动文件,当然我的PHP文件与我的目的地在同一个文件夹中,在这种情况下“material / videos /”
这就是我的代码的外观。
`
<?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";
}
}
?>`
答案 0 :(得分:1)
您必须在move_uploaded_file
中指定整个路径。这包括新文件名。即:
move_uploaded_file($_FILES["file"]["tmp_name"], "material/videos/".$_FILES["file"]["name"]);