What's new

Pano maisave to sa database

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,166
Solutions
2
Reaction
103
Points
496
Age
30
pano po ito maisave sa database sa folder lang kasi naisasave po..

ito code ko:

<div class="mt-3">
<label for="formFile" class="form-label ">Upload Image</label>
<input class="form-control" type="file" name="profile-pic" onchange ="displayPreview(this.files)" id="formFile">
</div>
<center>
<span id="preview"></span>
</center>
<hr class="my-4">
<button class="w-100 mb-2 btn btn-sm btn-lg rounded-3 btn-primary" name= "update" type="submit">Update</button>
</div>
</div>
</form>
<?php
$target_dir = "../photo_folder/";
$uploadErr = "";
if (isset($_POST["update"])){
$target_file = $target_dir . "/" . basename($_FILES["profile-pic"]["name"]);
$uploadOk = 1;
if(file_exists($target_dir)) {
$target_file = $target_dir . rand(1,9) . rand(1,9) . rand(1,9) . rand(1,9) . "_" .basename($_FILES["profile-pic"]["name"]);
$uploadOk = 1;
}
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($_FILES["profile-pic"]["size"] > 5000000000000000000 ) {
$uploadErr = "Sorry, your file is too large";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$uploadErr = "Sorry, only JPG, PNG, JPEG and GIF files are allowed";
$uploadOk = 0;
}
if($uploadOk == 1) {
if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)){
echo "the file has been uploaded";
}else {
echo "sorry , there was an error uploading file.";
}
}

}
?>
<span class="error"><?php echo $uploadErr; ?></span>

<script type = "application/javascript">
var _URL = window.URL || window.webkitURL;
function displayPreview(files){
var file = files[0];
var img = new Image();
var sizeKB = file.size / 1024;
img.onload =function(){
$(img).addClass('resized-image');
$('#preview').append(img);
}
img.src = _URL.createObjectURL(file);
}
</script>
 
Based on your code, it seems like you are trying to save an uploaded image to a folder on your server and also store the file path in a database.

To save the image to a folder, you can use the move_uploaded_file() function. However, you need to make sure the target directory has the correct permissions for writing files. In your code, you have defined the target directory as $target_dir = "../photo_folder/";. Make sure this directory exists and has the necessary write permissions.

To store the file path in a database, you need to establish a database connection and execute an SQL query to insert the file path into the appropriate table. Since you haven't provided any information about your database structure, I'll assume you have a table called images with a column called file_path.

Below is an updated version of your code that saves the image to a folder and stores the file path in the database:

PHP:
<?php
$target_dir = "../photo_folder/";
$uploadErr = "";

if (isset($_POST["update"])) {
    $target_file = $target_dir . "/" . basename($_FILES["profile-pic"]["name"]);
    $uploadOk = 1;

    // Check if the target directory exists
    if (!file_exists($target_dir)) {
        mkdir($target_dir, 0777, true); // Create the directory if it doesn't exist
    }

    // Generate a unique file name if a file with the same name already exists
    if (file_exists($target_file)) {
        $target_file = $target_dir . uniqid() . "_" . basename($_FILES["profile-pic"]["name"]);
    }

    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

    // Check file size
    if ($_FILES["profile-pic"]["size"] > 5000000) {
        $uploadErr = "Sorry, your file is too large";
        $uploadOk = 0;
    }

    // Check file type
    if (
        $imageFileType != "jpg" &&
        $imageFileType != "png" &&
        $imageFileType != "jpeg" &&
        $imageFileType != "gif"
    ) {
        $uploadErr = "Sorry, only JPG, PNG, JPEG and GIF files are allowed";
        $uploadOk = 0;
    }

    if ($uploadOk == 1) {
        if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)) {
            // File uploaded successfully

            // Store the file path in the database
            // Replace 'your-db-credentials' with your actual database credentials
            $db_host = 'your-db-host';
            $db_name = 'your-db-name';
            $db_user = 'your-db-user';
            $db_pass = 'your-db-password';

            $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);

            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $file_path = mysqli_real_escape_string($conn, $target_file);
            $sql = "INSERT INTO images (file_path) VALUES ('$file_path')";

            if ($conn->query($sql) === TRUE) {
                echo "The file has been uploaded and the file path has been stored in the database.";
            } else {
                echo "Sorry, there was an error storing the file path in the database: " . $conn->error;
            }

            $conn->close();
        } else {
            echo "Sorry, there was an error uploading the file.";
        }
    }
}
?>

Make sure to replace 'your-db-credentials' with your actual database credentials and update the table and column names as per your database structure.

Also, note that I've added some error handling and improved the file name generation logic in your code.
 

Similar threads

Back
Top