Skip to main content

How can I save an image from a file input field using PHP & MySQL?

How can I save an image from a file input field using PHP & MySQL?


 <?php
    $image = $_POST['pic'];
    //Stores the filename as it was on the client computer.
    $imagename = $_FILES['pic']['name'];
    //Stores the filetype e.g image/jpeg
    $imagetype = $_FILES['pic']['type'];
    //Stores any error codes from the upload.
    $imageerror = $_FILES['pic']['error'];
    //Stores the tempname as it is given by the host when uploaded.
    $imagetemp = $_FILES['pic']['tmp_name'];

    //The path you wish to upload the image to
    $imagePath = "images/";

    if(is_uploaded_file($imagetemp)) {
        if(move_uploaded_file($imagetemp, $imagePath . $imagename)) {
            echo "Sussecfully uploaded your image.";
        }
        else {
            echo "Failed to move your image.";
        }
    }
    else {
        echo "Failed to upload your image.";
    }
?>

Comments

Popular posts from this blog

How to edit or update Table values in HTML webpage using php

 How to edit or update Table values in HTML webpage using php update.php <!DOCTYPE html> <html> <head> <title>Edit/Update Data</title> </head> <body> <?php include_once('config.php'); //Fetching all data (rows or tuples) from the student_details table $roll_no = $_GET['id']; $query = "SELECT * FROM students_details WHERE roll_no=$roll_no"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $name= $row['name']; $email = $row['email']; $address = $row['address']; $phone_number = $row['phone_number']; ?> <h1 align="center">Edit/Update Data</h1> <form action="config.php" method="POST"> <table border='1px' align="center" style="width:90%;">          <tr>             <th>Roll No</th>             <td>                 <input type="text" name="roll_n...