用php上传视频或图片

时间:2015-03-15 05:08:07

标签: php html image video upload

我正在尝试使用php将视频或图片上传到我的服务器。但是我一直收到一个错误,好像文件名是空的,我不明白为什么,代码继续执行脚本显示错误,没有任何内容上传到我的文件夹

这是我的代码:

<div class="container" id="step2-video" style="display:none; margin-top:100px" >
                <div class="row">
                    <div class="col-lg-4 col-md-offset-4">
                        <form id="video_uploader" action="ad_creator.php" method="post" enctype="multipart/form-data">
                            <div id="btn-upload-video">
                                <span class="file-wrapper btn ad-choice">
                                    <input type="file" id="video-file-upload" name="video-file-upload" accept="video/mp4"/> 
                                    <span class="button">Choose a video</span>                              
                                </span>
                            </div>                              
                            <div id="video-preview" style="display:none" >      
                                <span class="file-wrapper btn ad-choice">   
                                    <?php

                                    ?>                      
                                    <input type="submit" name="upload_video" value="Submit" style="margin-left:auto" />
                                    <span class="button">Comfirm</span>
                                </span>
                            </div>
                        </form>                 
                    </div>
                </div>
            </div>  

            <!-- 
            Step 2 for pictures of the editor
            -->

            <div class="container" id="step2-picture" style="display:none; margin-top:100px" >
                <div class="row">
                    <div class="col-lg-4 col-md-offset-4">
                        <form id="picture_uploader" action="ad_creator.php" method="post" enctype="multipart/form-data">
                            <div id="btn-upload-picture">
                                <span class="file-wrapper btn ad-choice">
                                    <input type="file" id="picture-file-upload" name="picture-file-upload" accept="image/gif, image/jpeg, image/jpg, image/png"/> 
                                    <span class="button">Choose a picture</span>                                
                                </span>
                            </div>                              
                            <div id="picture-preview" style="display:none" >
                                <div id="imagePreview"></div>
                                <br>        
                                <span class="file-wrapper btn ad-choice">                           
                                    <input type="submit" name="upload_picture" value="Submit" style="margin-left:auto" />
                                    <span class="button">Comfirm</span>
                                </span>
                            </div>
                        </form> 
                    </div>
                </div>
            </div>  
            <?php
                if (isset($_POST['upload_picture'])){

                    $image_name = isset ($_FILES['image']['name']) ? $_FILES['image']['name'] : '' ;
                    $image_type = isset($_FILES['image']['type']) ? $_FILES['image']['type'] : '';
                    $image_size = isset($_FILES['image']['size']) ? $_FILES['image']['size'] : '';
                    $image_tmp_name = isset ($_FILES['image']['tmp_name']) ? $_FILES['image']['tmp_name'] : '';

                    if($image_name == ''){
                        echo "<script> alert ('Error occured for picture upload')</script>";
                        exit();             
                    } else {
                        move_uploaded_file ($image_tmp_name, "uploads/picture/$image_name");
                        echo "<script> alert ('sucess for puciture upload')</script>";
                    }
                }   

                if (isset($_POST['upload_video'])){

                    $video_name = isset ($_FILES['video']['name']) ? $_FILES['video']['name'] : '' ;
                    $video_type = isset($_FILES['video']['type']) ? $_FILES['video']['type'] : '';
                    $video_size = isset($_FILES['video']['size']) ? $_FILES['video']['size'] : '';
                    $video_tmp_name = isset ($_FILES['video']['tmp_name']) ? $_FILES['video']['tmp_name'] : '';

                    if($video_name == ''){
                        echo "<script> alert ('Error occured for video upload')</script>";
                        exit();             
                    } else {
                        move_uploaded_file ($video_tmp_name, "uploads/video/$video_name");
                        echo "<script> alert ('Sucess for video upload')</script>";
                    }
                }   

            ?>  

来自Rescaltt,谢谢。 我正在检查错误的文件输入, 我正在检查$_FILES['image']['name']而不是$_FILES['picture-file-upload']['name']

1 个答案:

答案 0 :(得分:1)

尝试使用函数来执行这两项操作。需要注意的是,你应该做一些检查和平衡:

  1. 清理文件名以获得良好的衡量标准。
  2. 包含文档根目录以获取绝对路径,或确保您具有正确的相对路径。
  3. 您可能需要检查上传文件夹是否存在,如果没有,请将其设为。
  4. 检查上传if(move_uploaded_file(...etc.
  5. 的成功与否
    <?php 
    function MyUploader($uploadtype = 'picture',$savedir = '/uploads/')
        {
            $name       =   (isset($_FILES[$uploadtype.'-file-upload']['name']))? $_FILES[$uploadtype.'-file-upload']['name'] : '' ;
            $type       =   (isset($_FILES[$uploadtype.'-file-upload']['type']))? $_FILES[$uploadtype.'-file-upload']['type'] : '';
            $size       =   (isset($_FILES[$uploadtype.'-file-upload']['size']))? $_FILES[$uploadtype.'-file-upload']['size'] : '';
            $tmp_name   =   (isset($_FILES[$uploadtype.'-file-upload']['tmp_name']))? $_FILES[$uploadtype.'-file-upload']['tmp_name'] : '';
    
            if(empty($name)) 
                return "<script> alert ('Invalid file name.')</script>";             
            else {
                // Make sure you include your ROOT DIRECTORY / correct local path
                $upload_dir =   $_SERVER['DOCUMENT_ROOT'].$savedir.$uploadtype."/";
                // May want to make the directory if not exists
                if(!is_dir($upload_dir))
                    mkdir($upload_dir,0755,true);
                // You should check that it worked with if(move_upload_file(...etc                  
                if(move_uploaded_file($tmp_name, $upload_dir.$name))
                    return "<script> alert ('$uploadtype uploaded successfully')</script>";
                else
                    return "<script> alert ('Could not save file to server.')</script>";
            }
        }
    
        if(isset($_POST['upload_picture']))
            echo MyUploader('picture');
    
        if(isset($_POST['upload_video']))
            echo MyUploader('video'); ?>