使用php将视频上传到文件夹及其与数据库的链接

时间:2013-11-15 06:29:04

标签: php mysql video

我正在尝试上传视频并将其保存在数据库中的文件夹及其路径中。 但视频未插入特定文件夹。 我搜索了很多,发现了一些代码。代码正在为图像工作。我已经从图像到视频做了一些修改,但是没有用。 这是我的代码的一部分。

<form action="" method="post" enctype="multipart/form-data">
<div class="form_search">
<label>Upload Video Profile:</label>
<span class="form_input">
<input type="file" name="uploadvideo"  />
</span>
</div>

<div class="form_search">
<label> &nbsp;</label>
<span class="form_input">
<input type="submit" name="submitdetails" value="Upload" class="button"/>
</span>
</div>
</form>

我上传视频的php代码是

<?php
if(isset($_POST['submitdetails']))
{
$name=$_FILES['uploadvideo']['name'];
$type=$_FILES['uploadvideo']['type'];
//$size=$_FILES['uploadvideo']['size'];
$cname=str_replace(" ","_",$name);
$tmp_name=$_FILES['uploadvideo']['tmp_name'];
$target_path="company_profile/";
$target_path=$target_path.basename($cname);
if(move_uploaded_file($_FILES['uploadvideo']['tmp_name'],$target_path))
{
    echo "hi";
echo $sql="UPDATE employer_logindetails SET (video) VALUES('".$cname."')"; 
$result=mysql_query($sql);
echo "Your video ".$cname." has been successfully uploaded";
}

}

?>

请在我出错的地方帮助我。 我的所有php.ini修改都已完成,视频大小仅为7mb。

3 个答案:

答案 0 :(得分:2)

步骤-1

此脚本允许您使用PHP将文件从浏览器上传到主机。我们需要做的第一件事是创建一个HTML表单,允许人们选择他们想要上传的文件。

 <form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="uploaded" type="file" /><br />
 <input type="submit" value="Upload" />
 </form> 

此表单将数据发送到文件“upload.php”,这是我们接下来要创建的实际上传文件的内容。

步骤-2

实际的文件上传非常简单:

<?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
 } 
 else {
 echo "Sorry, there was a problem uploading your file.";
 }
 ?> 

这一小段代码将上传HTML表单发送给它的文件。

The first line $target = "upload/"; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file. So for example, if your file was at www.yours.com/files/upload.php then it would 

将文件上传到www.yours.com/files/upload/yourfile.gif。一定要记得创建 这个文件夹!拥有777权利

步骤3

 if ($uploaded_size > 350000)
 {
 echo "Your file is too large.<br>"; 
 $ok=0;
 } 

假设您没有更改HTML表单中的表单字段(因此它仍然被命名为已上载),这将检查文件的大小。如果文件大于350k,则会给出一个文件太大的错误,我们将$ ok设置为等于0.

如果您希望将350000更改为其他数字,则可以将此行更改为更大或更小的尺寸。或者,如果您不关心文件大小,请将这些行留下来

We are not using $ok=1; at the moment but we will later in the tutorial.

We then move the uploaded file to where it belongs using move_uploaded_file (). This places it in the directory we specified at the beginning of our script. If this fails the user is given an error message, otherwise they are told that the file has been uploaded.

全力以赴

<?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 

 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 

 //This is our limit file type condition 
 if ($uploaded_type =="text/php") 
 { 
 echo "No PHP files<br>"; 
 $ok=0; 
 } 

 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 

 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 
 ?>

显然,如果您允许上传文件,那么您可以向上传大量不良内容的人开放。一个预防措施是不允许他们上传任何可能包含恶意代码的php,html,cgi等文件。这提供了更多的安全性,但不确定防火。

答案 1 :(得分:0)

试试这个:$ _FILES [&#39; userfile&#39;]代替$ _FILES [&#39; uploadvideo&#39;]无处不在。

答案 2 :(得分:0)

我在表单中看不到uploadvideo的任何名称。

但是在php中你正在使用$_FILES['uploadvideo']

尝试提供文件输入的确切名称

例如:

<input type="file" name="uploadvideo">