1

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

12
  • 1
    You need / between $uploads_dir.$file_name it would be $uploads_dir."/".$file_name Commented Dec 7, 2015 at 6:24
  • not working... i tried Commented Dec 7, 2015 at 6:28
  • Please post your form code!!! Commented Dec 7, 2015 at 6:29
  • added form in question pls check Commented Dec 7, 2015 at 6:42
  • you should give $uploads_dir = './pic/'; Commented Dec 7, 2015 at 6:43

3 Answers 3

0

Try this fix to your code:

$uploads_dir = '/pic/'; //Add another slash to complete the directory path
Sign up to request clarification or add additional context in comments.

Comments

0

// Get the path to the upload directory.

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.