0

Hello i want to make upload button so users can upload images/gifs/videos/music but i cannot understand how i can do that i fuond a code on w3schools but it just uploads the image to folder in my computer called /uploads i want to store the images into database which they will be available/shown in the website... please someone help me

3
Contributors
5
Replies
29
Views
2 Years
Discussion Span
Last Post by jkon
0

I forgot to upload the code...

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" && $imageFileType != "mp4" && $imageFileType != "wmv" 
    && $imageFileType != "mp3") {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
0

I suggest storing the filename in the database, and not the file. Just add an insert query when move_uploaded_file is successful.

0

So this should be like this ?

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    $sql = "INSERT INTO `pictures` VALUES ('', '".$_FILES["fileToUpload"]["name"]."')";
    $result = $conn->query($sql);
} else {
    echo "Sorry, there was an error uploading your file.";
}
0

BTW this code it stores the name in database but the picture is in folder uploads into the website file directory, i cant understand how can that picture be shown in the website available for all users... also when other user will upload picture where it will go that picture if he doesn't have folder called uploads ?

0

Stefan_1 there is the concept of repository that works together with db. Very quickly how it works : we have a folder named repository above public_html (not accessible through browsers) there we can have sub folders for the different scope of the image / file. Then we have a main repository table , there can be the scope of the file (subfolder) and more (e.g. extension) info about it (if we are talking only about images you don't need a second table with image dimensions , you can store it there) also this has an auto increment id (can have a reference id also , but this varies through different implementations). When we upload an image we first add a row in this table and then save the image in the appropriate folder. You can have a second or n tables giving authorities in this specific image use.

Next you make your own naming policies e.g. lets say you have a product named “blue jean” in the category men clothes , then the url can be {rootUrl}/Image/Product/men-clothes/blue-jean_4323.jpg , you can easy get the id of the image in the repository and the folder of it through the Image controller. (You can do what ever validation you need there) after that you just readfile and output this to the user.

Of course this is a simplified repository logic and in real life you can make it as complicated as needed. But I always start programming as simple and clean as possible since it is more than certain that real life needs will add its own complexities.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.