1
votes

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

3
You need / between $uploads_dir.$file_name it would be $uploads_dir."/".$file_nameSaty
not working... i triedJishad P
Please post your form code!!!Saty
added form in question pls checkJishad P
you should give $uploads_dir = './pic/';Abbas

3 Answers

0
votes

Try this fix to your code:

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

// Get the path to the upload directory.

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

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']!=''){