你好guyys我想用2天从php上传mp4文件...我开始讨厌php:D我改变了php.ini中的帖子设置...我的代码是:
<?php
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("mp4","ogg","webm");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a MP4, OGG, or WEBM formats...";
}
if($file_size > 800000000){
$errors[]='File size must be excately 800 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"videos/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
当我尝试上传任何图像文件时,代码正常工作,显示正确的错误信息,但是当我尝试上传mp4文件时,它没有显示任何错误,我嘲笑“视频”文件夹,没有内部。注意:我正在使用localhost ..
答案 0 :(得分:0)
这是我上传文件的部分代码。它适用于所有类型的文件 我的PHP:
if(isset($_POST["submit"]))
{
$target_dir = $basedir."/".$dir;
$target_file_name = basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir."/".$target_file_name;
$uploadOk = 1;
if (file_exists($target_file))
{
$text = "Sorry, item already exists.";
$bulean = 1;
alert($text);
$uploadOk = 0;
}
if ($uploadOk == 0)
{
$text = "Sorry, your item was not uploaded.";
$bulean = 1;
alert($text);
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
$text = "The item ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$bulean = 1;
alert($text);
}
else
{
$text = "Sorry, there was an error uploading your item.";
$bulean = 1;
alert($text);
}
}
HTML
<form action="form_upload.php?dir=<?php echo $dir;?>" method="post" enctype="multipart/form-data" >
...
Select data to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="upload" name="submit">
答案 1 :(得分:0)
对您的代码进行了更改。试试这个。现在工作得很好。 本地测试
<html>
<body>
<form method="POST" action="test.php" enctype="multipart/form-data">
File: <input type="file" name="file"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST)){
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
//print_r($_FILES['file']['name']);
$file_ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
//$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("mp4","ogg","webm");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a MP4, OGG, or WEBM formats...";
}
if($file_size > 800000000){
$errors[]='File size must be excately 800 MB';
}
//print_r($file_tmp); exit;
if(empty($errors)==true){
move_uploaded_file($file_tmp,"videos/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
}
答案 2 :(得分:0)
我相信您可以通过以下链接解决您的问题:
https://stackoverflow.com/search?q=php+upload+large+file
当我使用PHP执行我的第一个上传代码时,我做了业余错误,忽略了在apache2设置中需要更改的内容的文档(我认为当时只有PHP的apache2)。
您需要了解的是,您发送的文件大于设置中允许的文件,因此PHP无法执行操作,并且您的POST将呈现为空白。
您需要根据规范更改php.ini并在客户端上导入验证的上传限制(在上传之前)。