-6

I know that i should use LONGBLOB, and i've written this code so far that uploads text to my server. How do i upload images instead of text?.

//if(isset...)
$connectionText = mysql_connect("localhost", "root");

if(!$connectionText){
die("Noget gik galt?". mysql_connect_error());
}
mysql_select_db("textbox", $connectionText);

$t = $_POST['tekst'];

$sqlScript = "INSERT INTO form (tekst) VALUES ('$t')";

mysql_query($sqlScript, $connectionText);
mysql_close($connectionText);

It should save the path to the mysql database. The image should be stored on the servers' computer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
R. A
  • 23
  • 2
  • 7
  • 2
    Please do NOT use mysql-functions! Consider using mysqli instead. – BenRoob Jul 04 '17 at 09:37
  • *How do i upload images instead of text?* Well you don't store image on a database, just store the images on a file system then store the path on a database. or find your self an image CDN and host them there – Masivuye Cokile Jul 04 '17 at 09:40
  • Yeah i know now lol. I am swithching to mysqli now. Thanks for reminding me. – R. A Jul 04 '17 at 10:28

2 Answers2

0

You don't need to store image directly into to database. Just store the image name in the database and fetch it when you want to show. For e.g.

$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" ) {
    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)) {
        // execute your insert query here
       $sqlScript = "INSERT INTO form (image) VALUES ('".basename( $_FILES["fileToUpload"]["name"])."')";


    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
Sehdev
  • 5,486
  • 3
  • 11
  • 34
  • 2
    *First of all stop using `mysql_*` function. They are vulnerable of SQL injections* So does your code – Masivuye Cokile Jul 04 '17 at 09:46
  • That is actually pretty smart. The code worked like a charm with the file size check removed. But how do i show the file. It just says [BLOB - 19 B] in the database on all images? – R. A Jul 04 '17 at 10:23
  • Ok i'll try writing it in mysqli ;) – R. A Jul 04 '17 at 10:24
-2

alternative: convert image in base64 and save like text.

$connectionText = mysql_connect("localhost", "root");

if(!$connectionText){
die("Noget gik galt?". mysql_connect_error());
}
mysql_select_db("textbox", $connectionText);

$image_string = base64_encode(file_get_contents($myimage));

$sqlScript = "INSERT INTO form (images) VALUES ('$image_string')";

mysql_query($sqlScript, $connectionText);
mysql_close($connectionText);