PHP文件上传move_uploaded_file不能正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP文件上传move_uploaded_file不能正常工作相关的知识,希望对你有一定的参考价值。

我开发一个文件上传的Web应用程序。

我只是创作的一个php代码上传图片。

if(isset($_FILES['equipmentPictureName'])){

 $file_tmp =$_FILES['equipmentPictureName']['tmp_name'];
 $file_name = $_FILES['equipmentPictureName']['name'];

 $uploads_dir = '/pic';
 echo "on file upload if";
 if(move_uploaded_file($file_tmp,$uploads_dir.$file_name)){
    echo "uploaded";
    exit();
 }else
 {
    echo "error on upload";
    exit();
 }  
 }else{
 echo "File Not Present";
 exit();
}   

当我运行这段代码我可以上传文件获取输出一样,如果错误上载

我给777权限的文件夹PIC

问题是什么,任何想法,,

形成..

<form action="manage-ahri-action.php" method="post" name="equipment" enctype="multipart/form-data" id="multiple_upload_form" >

  <div class="col-lg-6 col-sm-6 row" style="margin-bottom: 15px;">
                            <div class="form-group">
                                <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Equipment Picture</label>
                                <div class="col-lg-8 col-sm-8">
                                   <div class="option-group uploading none">
                                      <span class="file-button btn-primary" style="margin-right: 14px;">Choose File</span>
                                       <input name="equipmentPictureName" type="file" class="form-control form-white"/>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="file col-lg-12 col-sm-12 col-md-12">
                            <button type="submit" class="btn btn-primary btn-square" style="margin-top: 20px">Submit</button>
                        </div>

 </form>

的print_r

阵列([equipmentPictureName] =>数组([名称] =>猫01.JPG [型] =>图像/ JPEG [tmp_name的值] =>的/ tmp / php96PS5A [错误] => 0 [尺寸] => 72413)认证资格] =>阵列([名称] => [式] => [tmp_name的值] => [错误] => 4 [尺寸] => 0)[小册子] =>阵列([名称] => [式] = > [tmp_name的值] => [错误] => 4 [尺寸] => 0))

谢谢

答案

试试这个修复您的代码:

$uploads_dir = '/pic/'; //Add another slash to complete the directory path
另一答案

//获取路径上传目录。

 $wp_upload_dir = wp_upload_dir();
$uploads_dir= $wp_upload_dir['url'] . '/pic/' ;
另一答案

尝试这个 :

if(isset($_FILES['equipmentPictureName']['name'])){

 $file_tmp =$_FILES['equipmentPictureName']['tmp_name'];
 $file_name = $_FILES['equipmentPictureName']['name'];

 $uploads_dir = 'pic/';
 echo "on file upload if";
$move=move_uploaded_file($file_tmp,$uploads_dir.$file_name);
 if($move){
    echo "uploaded";
    exit();
 }else
 {
    echo "error on upload";
    exit();
 }  
 }else{
 echo "File Not Present";
 exit();
}   

并确保该文件和文件夹都在同一个位置。而文件夹有777权限。

编辑:

或代替这主要条件

if(isset($_FILES['equipmentPictureName']['name'])){

你可以检查使用,这就意味着这个替换行:

if($_FILES['file_equipmentPictureName']['name']!=''){

以上是关于PHP文件上传move_uploaded_file不能正常工作的主要内容,如果未能解决你的问题,请参考以下文章

PHP 文件上传,文件在 move_uploaded_files 之前从 /tmp 消失

PHP文件上传move_uploaded_file不能正常工作

PHP文件上传 move_uploaded_file() 参数的正确写法

解决PHP move_uploaded_file函数移动图片失败

在PHP中,move_uploaded_file将文件上传到一个目录,但无法上传到另一个目录?

PHP 文件上传之常见问题($_FILES为空,move_uploaded_file()函数报错)