PHP文件上传move_uploaded_file不起作用

Posted

技术标签:

【中文标题】PHP文件上传move_uploaded_file不起作用【英文标题】:PHP file Upload move_uploaded_file not working 【发布时间】:2016-03-11 16:53:29 【问题描述】:

我正在开发一个带有文件上传功能的网络应用程序。

我刚刚写了一个 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();
   

当我运行此代码时,我可以得到类似 on file upload if upload on error

的输出

我授予 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>

打印_R

Array ( [equipmentPictureName] => Array ( [name] => cat-01.jpg [type] => image/jpeg [tmp_name] => /tmp/php96PS5A [error] => 0 [size] => 72413 ) [accreditations] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) [Brochures] => Array ( [name] => [ type] => [tmp_name] => [error] => 4 [size] => 0 ) )

谢谢

【问题讨论】:

你需要在$uploads_dir.$file_name 之间的/ 它将是$uploads_dir."/".$file_name 不工作...我试过了 请发布您的表单代码!!! 添加有问题的表格请检查 你应该给$uploads_dir = './pic/'; 【参考方案1】:

尝试对您的代码进行此修复:

$uploads_dir = '/pic/'; //Add another slash to complete the directory path

【讨论】:

【参考方案2】:

// 获取上传目录的路径。

 $wp_upload_dir = wp_upload_dir();
$uploads_dir= $wp_upload_dir['url'] . '/pic/' ;

【讨论】:

【参考方案3】:

试试这个:

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

已编辑:

OR 代替这个主要条件

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()函数报错)