gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:18:50 25 4
gpt4 key购买 nike

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

我进行了很多搜索并找到了一些代码。该代码适用于图像。我从图像到视频做了一些修改,但没有成功。

这是我的代码部分。

<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。

最佳答案

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

第三步

 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 等文件。这提供了更多的安全性,但并不能保证防火。

关于php - 使用 PHP/MySQL 将视频上传到文件夹及其到数据库的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com