【问题标题】:Video Uploads in PHP, can't find upload directory?PHP中的视频上传,找不到上传目录?
【发布时间】:2025-12-11 06:25:02
【问题描述】:

我正在使用这个 php 视频上传脚本。我已经将我的目录路径设置为一个名为 video 的文件夹,我使用与 php 文件相同的目录创建了该文件夹。但是我找不到正在上传的视频。

它没有进入我要求的目录?为什么有人可以帮助我。

我没有收到任何错误。

谢谢。

HTML:

<form action="upload_videos_process.php" method="post"   enctype="multipart/form-data">
  <label for="file">Filename:</label>
     <input type="file" name="uploadFile" id="uploadFile" />
<br />
<input type="submit" name="submit" value="Upload File" />
</form>

php 文件:

<?php

//This handles the maximum size for the video file in kbs

define ("MAX_SIZE","500");

//This function reads the extension of the file to ensure that it is an video file

function getExtension($str) {

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

//This variable handles an error and won't upload the file if there is a problem with it

$errors=0;

//checks if the form has been submitted

if(isset($_POST['Submit']))

{

//reads the name of the file the user submitted for uploading

$video=$_FILES['video']['name'];

//if it is not empty

if ($video)

{

//get the original name of the file from the clients machine

$video_filename = stripslashes($_FILES['video']['name']);

$video_extension = getExtension($filename);

$video_extension = strtolower($extension);

//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwise we will do more tests

if (($video_extension != "mpeg") && ($video_extension != "avi") && ($video_extension != "flv") && ($video_extension != "mov"))

{

echo '<h1>Unknown extension!</h1>';

$errors=1;

}

else

{

//get the size of the video

$size=filesize($_FILES['video']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger

if ($size > MAX_SIZE*1024)

{

echo '<h1>You have exceeded the size limit!</h1>';

$errors=1;

}

//give the video a unique name in case a video already exists with the name on the server
$video_name=time().'.'.$extension;

//assign a folder to save the video to on your server

$newname="video/".$video_name;

//verify that the video has been loaded

$copied = copy($_FILES['video']['tmp_name'], $newname);

if (!$copied)

{

echo '<h1>Copy unsuccessful!</h1>';

$errors=1;

}}}}

//If no errors registered, print the success message

if(isset($_POST['Submit']) && !$errors)

{

echo "<h1>File Uploaded Successfully! Try again!</h1>";

}

?>

【问题讨论】:

  • 可能你的错误在php.ini中被抑制了,尝试在脚本之前添加:ini_set('display_errors', 'On'); ini_set('error_reporting', E_ALL); error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  • 在那段代码之后它仍然没有显示任何错误或进入处理页面后它只是空白。
  • 闻起来post_max_size错误,真的是非常无声的错误..

标签: php video upload


【解决方案1】:

你盲目地假设一切都运行良好。事情失败了。第一步:检查上传是否真的做了什么:

if ($_FILES['video']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error code " . $_FILES['video']['error']);
}

错误代码在这里定义:http://php.net/manual/en/features.file-upload.errors.php

同样,一旦您确认上传成功,请勿在上传文件中使用copy()。有move_uploaded_file() 是有原因的——它有额外的安全检查以确保文件没有在服务器上被篡改,并且它实际上移动文件。 copy() 可能会降低性能,尤其是在大文件上,因为您正在复制文件,而不是仅仅做一些文件系统管理。

您还相信用户不会篡改文件名。 NOTHING 可以防止恶意用户在上传之前执行ren nastyvirus.exe cutekittens.avi,并且您的脚本将很乐意接受该 .exe,因为它的文件名已被更改。使用服务器端 mime-detection(例如 http://www.php.net/manual/en/book.fileinfo.phpenter link description here)来解决这个问题。 从不相信用户的任何东西

【讨论】:

    【解决方案2】:

    这可能是因为您的 php 配置不允许上传大文件。尝试设置

    upload_max_filesize = 500M
    

    在 php.ini 中甚至大于 500M 以及在 cmets 中提到的 ppl,启用错误

    ini_set('display_errors', 1);
    ini_set('error_reporting', 8191); 
    

    【讨论】: