0

I am trying to upload an image to server using PHP and save inside a dir, and then returning the image url.

html code:

 <form method="post">
  <fieldset>
    <div class="form-group col-md-6 col-lg-6 col-sm-12">
       <input type="file" class="form-control" name="image" required="required">
    </div>
    <div class="form-group">
       <button type="submit" name="Save" class="btn btn-primary">Create</button>
    </div>
  </fieldset>
</form> 

PHP code for upload image:

<?php
    if(isset($_POST['Save']))
    {
     /*image */
     $traget_dir="image/";
     $traget_file=$traget_dir .basename($_FILES["image"]["name"]);
     $uploadOk=1;
     $imageFileType = pathinfo($traget_file,PATHINFO_EXTENSION);
     $check=getimagesize($_FILES["image"]["tmp_name"]);
     if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
     } else{
        echo "File is not an image.";
       $uploadOk = 0;
     }
     /*end image upload*/
    }
    ?>

error on upload time:

Notice: Undefined index: image in

second error->

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in

thank for help and view.

1
  • can you show your full php code and html? Commented Mar 18, 2017 at 6:26

4 Answers 4

2

Do this action after submit and check with if condition like following code. where 'SUBMIT' is the name of your button.

if(isset($_POST['SUBMIT']))
{
 /*image */
 $traget_dir="image/";
 $traget_file=$traget_dir .basename($_FILES["image"]["name"]);
 $uploadOk=1;
 $imageFileType = pathinfo($traget_file,PATHINFO_EXTENSION);
 $check=getimagesize($_FILES["image"]["tmp_name"]);
 if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
 } else{
    echo "File is not an image.";
   $uploadOk = 0;
 }
 /*end image upload*/
}
Sign up to request clarification or add additional context in comments.

2 Comments

You've to add this in your form enctype="multipart/form-data"
like <form method="post" enctype="multipart/form-data">
1

For security reasons you have to check mime-type and extension also, and the best practice is to upload your files in /year/month/file to avoid file system problems, take a look at this code https://github.com/MoustafaElkady/SimpleUploader

Comments

0

You need to tell the form that its also going to be uploading files.

You can do this by using enctype=multipart/form-data

So, your form should look like:

<form method="post" enctype="multipart/form-data">
  <fieldset>
    <div class="form-group col-md-6 col-lg-6 col-sm-12">
       <input type="file" class="form-control" name="image" required="required">
    </div>
    <div class="form-group">
       <button type="submit" name="Save" class="btn btn-primary">Create</button>
    </div>
  </fieldset>
</form> 

Comments

0

Replace this `

<form method="post">

with this

<form method="post" enctype="multipart/form-data">

and you are all good to go :)

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.