简体   繁体   中英

PHP file Upload move_uploaded_file not working

I am developing a web application with a file upload.

I just writted an php code to upload an image.

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();
}   

When I run this code I can get output like on file upload if error on upload

I give 777 permission to pic folder

What is the issue, Any Idea,,

Form..

<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

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

Thanks

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

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

// Get the path to the upload directory.

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

Try this :

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();
}   

And make sure that this file and folder are at same location . And the folder has 777 permission.

EDITED :

OR instead of this main condition

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

you can check using this, which means replace the line with this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM