trollette swagga
trollette swagga

Reputation: 13

image only in this input in my form

How would i go about making this image only urls before acceptance

            <section>
                <label class="input">
                    <i class="icon-append icon-user"></i>
                    <input type="text" value="<?php if(isset($avatar)) echo $avatar; ?>" name="avatar">
                    <b class="tooltip tooltip-bottom-right">change your avatar!</b>
                </label>
            </section>

php script

else if(isset($_POST['updateProfileBtn'])){

  //initialize an array to store any error message from the form

    $form_errors = array();

 // form validation

    $required_fields = array('email', 'avatar');


 //call the function to check empty field and merge the return data into form_error array

   $form_errors = array_merge($form_errors, check_empty_fields($required_fields));


 //field that requires checking for minimum length

   $fields_to_check_length = array('avatar' => 1);

 //call the function to check minimum required length and merge the return data into form_error array

  $form_errors = array_merge($form_errors, check_min_length($fields_to_check_length));

 //email validation / merge the return data into form_error array

  $form_errors = array_merge($form_errors, check_email($_POST));

 //Collect form data and store in variables

   $email = $_POST['email'];
   $avatar = $_POST['avatar'];
   $hidden_id = $_POST['hidden_id'];

  if(empty($form_errors)){
     try{
         //create SQL update statement
               $sqlUpdate = "UPDATE users SET avatar =:avatar, email =:email WHERE id =:id";

                //use PDO prepared to sanitize data
                $statement = $db->prepare($sqlUpdate);

                //update the record in the database
                $statement->execute(array(':email' => $email, ':avatar' => $avatar, ':id' => $hidden_id));

         //check if one new row was created
             if($statement ->rowCount() == 1) {
                  $result = flashMessage("Profile Updated Successfully!!");
                } else{
                    $result = flashMessage("No changes were made!");
                }
           }catch (PDOException $ex){
                $result = flashMessage("An error occurred: ".$ex->getMessage()."");
            }
        }
    else{
        if(count($form_errors) == 1){
            $result = flashMessage("There was 1 error in the form<br>");
        }else{
            $result = flashMessage("There were " .count($form_errors). " errors in the form <br>");
        }
    }
}

i am basically just wanting to stop anything outside of image urls being posted so in a sense restricting it to... png, jpg & gif

i am only keeping it on urls only so avatars are not being hosted on my server.

Upvotes: 0

Views: 27

Answers (1)

Luke Paoloni
Luke Paoloni

Reputation: 174

If you’re wanting to restrict the user to only upload images. <input type=“file” name=“image” accept=“image/*”>.

In your php file, $image = $_FILES[‘image’];. You can then just use $image[‘name’] for the url of the file. To retrieve it from the server, you would need to store it on the server using move_uploaded_file(‘file name’, ‘location’).

http://php.net/manual/en/function.move-uploaded-file.php

Upvotes: 1

Related Questions