将视频上传到文件夹中,并使用PHP/MySQL将其链接到数据库 [英] uploading videos into a folder and its link to database using PHP/MySQL

查看:455
本文介绍了将视频上传到文件夹中,并使用PHP/MySQL将其链接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传视频并将其保存在文件夹中,以及将其路径保存在数据库中,但是视频没有插入到特定的文件夹中.

I am trying to upload a video and save it in a folder as well as savings its path in the database, but the videos are not inserting into the specific folder.

我进行了很多搜索并找到了一些代码.该代码适用于图像.我已经完成了从图像到视频的一些修改,但这没用.

I have searched a lot and found some code. The code is working for images. I have done some modifications from images to videos, but that didn't work.

这是我的代码的一部分.

Here is the parts of my code.

<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代码是

and my php code to upload video is

<?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";
}

}

?>

请帮助我哪里出问题了.

Please help me where I am going wrong.

我所有的php.ini修改都已完成,视频大小仅为7mb.

All my php.ini modifications are done, and video size is only 7mb.

推荐答案

步骤1

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

This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.

 <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",这是我们将在实际上传文件的旁边创建的.

This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.

第2步

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

The actual file upload is very simple:

<?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表单发送给它的文件.

This very small piece of code will upload files sent to it by your HTML form.

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权利

upload files to www.yours.com/files/upload/yourfile.gif. Be sure you remember to create this folder! with 777 rights

第3步

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

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

Assuming that you didn't change the form field in our HTML form (so it is still named uploaded), this will check to see the size of the file. If the file is larger than 350k, they are given a file too large error, and we set $ok to equal 0.

如果需要,可以将350000更改为其他数字,以将此行更改为更大或更小.或者,如果您不关心文件大小,只需将这些行留在外面

You can change this line to be a larger or smaller size if you wish by changing 350000 to a different number. Or if you don't care about file size, just leave these lines out

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等文件.这样可以提供更高的安全性,但不能确保防火.

Obviously if you are allowing file uploads you are leaving yourself open to people uploading lots of undesirable things. One precaution is not allowing them to upload any php, html, cgi, etc. files that could contain malicious code. This provides more safety but is not sure fire protection.

这篇关于将视频上传到文件夹中,并使用PHP/MySQL将其链接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆