CODIGO INSERTAR FOTO INPUT TYPE EN BD MYSQL PHP JSON API

 <?php


if(isset($_POST) && !empty($_FILES['image']['name'])){


   $name = $_FILES['image']['name'];

   list($txt, $ext) = explode(".", $name);

   $image_name = time().".".$ext;

   $tmp = $_FILES['image']['tmp_name'];



   if(move_uploaded_file($tmp, 'uploads/'.$image_name)){

      echo "<img width='300px' src='uploads/".$image_name."' class='preview'>";

   }else{

      echo "image uploading failed";

   }

}else{

   echo "Please select image";

}



?>



///////////////////////////////////

<?php

require_once 'conn.php';

 

if(!empty($_FILES['image'])){

 

$image_name = $_FILES['image']['name'];

$image_temp = $_FILES['image']['tmp_name'];

$image_size = $_FILES['image']['size'];

 

$exp = explode(".", $image_name);

$ext = end($exp);

$allowed_ext = array('jpg', 'jpeg', 'png');

 

if(in_array($ext, $allowed_ext)){

$image = time().'.'.$ext;

$location = "upload/".$image;

if($image_size < 5242880){

move_uploaded_file($image_temp, $location);

mysqli_query($conn, "INSERT INTO `image` VALUES('', '$image', '$location')") or die(mysqli_error());

echo "success";

}else{

echo "error3";

}

}else{

echo "error2";

}

}else{

echo "error1";

}

 

?>


/////////////////////

<?php 

// File upload folder 

$uploadDir = 'uploads/'; 

 

// Allowed file types 

$allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg'); 

 

// Default response 

$response = array( 

    'status' => 0, 

    'message' => 'Form submission failed, please try again.' 

); 

 

// If form is submitted 

if(isset($_POST['name']) || isset($_POST['email']) || isset($_POST['file'])){ 

    // Get the submitted form data 

    $name = $_POST['name']; 

    $email = $_POST['email']; 

     

    // Check whether submitted data is not empty 

    if(!empty($name) && !empty($email)){ 

        // Validate email 

        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ 

            $response['message'] = 'Please enter a valid email.'; 

        }else{ 

            $uploadStatus = 1; 

             

            // Upload file 

            $uploadedFile = ''; 

            if(!empty($_FILES["file"]["name"])){ 

                // File path config 

                $fileName = basename($_FILES["file"]["name"]); 

                $targetFilePath = $uploadDir . $fileName; 

                $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 

                 

                // Allow certain file formats to upload 

                if(in_array($fileType, $allowTypes)){ 

                    // Upload file to the server 

                    if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ 

                        $uploadedFile = $fileName; 

                    }else{ 

                        $uploadStatus = 0; 

                        $response['message'] = 'Sorry, there was an error uploading your file.'; 

                    } 

                }else{ 

                    $uploadStatus = 0; 

                    $response['message'] = 'Sorry, only '.implode('/', $allowTypes).' files are allowed to upload.'; 

                } 

            } 

             

            if($uploadStatus == 1){ 

                // Include the database config file 

                include_once 'dbConfig.php'; 

                 

                // Insert form data in the database 

                $sqlQ = "INSERT INTO form_data (name,email,file_name,submitted_on) VALUES (?,?,?,NOW())"; 

                $stmt = $db->prepare($sqlQ); 

                $stmt->bind_param("sss", $name, $email, $uploadedFile); 

                $insert = $stmt->execute(); 

                 

                if($insert){ 

                    $response['status'] = 1; 

                    $response['message'] = 'Form data submitted successfully!'; 

                } 

            } 

        } 

    }else{ 

         $response['message'] = 'Please fill all the mandatory fields (name and email).'; 

    } 

 

// Return response 

echo json_encode($response);


///////////////////////////////////////////


<?php 

 

 if($_SERVER['REQUEST_METHOD']=='POST'){

 

 //Getting actual file name

 $name = $_FILES['photo']['name'];

 

 //Getting temporary file name stored in php tmp folder 

 $tmp_name = $_FILES['photo']['tmp_name'];

 

 //Path to store files on server

 $path = 'uploads/';

 

 //checking file available or not 

 if(!empty($name)){

 //Moving file to temporary location to upload path 

 move_uploaded_file($tmp_name,$path.$name);

 

 //Displaying success message 

 echo "Upload successfully";

 }else{

 //If file not selected displaying a message to choose a file 

 echo "Please choose a file";

 }

 }

Comentarios

Entradas populares de este blog

INSERTAR Y UPDATE FECHA CON PHP MYSQL EN FORMULARIO METODO POST CON JQUERY ETIQUETA DATE

Copia de Seguridad de Archivo MS Access (.mdb) a Pendrive con VB6

CONVERTIR IMÁGEN JPG O PNG A WEBP CON PHP DE FORMA SIMPLE