-2

I am currently storing an image into the database directly. I learnt that it is not a good idea to store an image to database, instead i should upload the image to a directory and insert the file name to database. This is how i upload the image to directory.

<?php

$newImageSubmitted = isset( $_POST['new-image'] );
if ( $newImageSubmitted ) 
{
    $output = upload();
}
return $output;
/function upload()
{
include_once "Uploader.class.php";

$uploader = new Uploader( "image-data" );
$uploader->saveIn("img");
$fileUploaded = $uploader->save();
if ( $fileUploaded )
 {

$out = "<span class='rare'>Image uploaded success!!!..</span>";

} else {
$out = "something went wrong";
}
return $out;
}

and my form is as below:

<form method='post' action='upload.php' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpg'/>
<input type='submit' value='upload' name='new-image' />
</form>

Now how can i store the image name to database??:

$db = new mysqli("localhost", "root","","rec");

if ($db->connect_error) {
    die("Connection failed this is the error: " . $db->connect_error);
}

$stmt = $db->prepare("INSERT INTO records (image) VALUES (?)");
if($stmt)
{
    $stmt>bind_param("b",$newImageSubmitted);
    $stmt->execute();
    echo"<center>Image path stored.</center>";
}

here is uploader.class.php if required.

<?php

class Uploader {
private $filename;
private $fileData;
private $destination;

public function __construct( $key ) {
$this->filename = $_FILES[$key]['name'];
$this->fileData = $_FILES[$key]['tmp_name'];
}
public function saveIn( $folder ) {
$this->destination = $folder;
}
public function save(){
$folderIsWriteAble = is_writable( $this->destination );
if( $folderIsWriteAble ){
$name = "$this->destination/$this->filename";
$succes = move_uploaded_file( $this->fileData, $name );
} else {
trigger_error("cannot write to $this->destination");
$succes = false;
}
return $succes;
}
}
Steve
  • 1,622
  • 5
  • 21
  • 39

2 Answers2

0

Do a small adjustment like

public function save(){
$folderIsWriteAble = is_writable( $this->destination );
if( $folderIsWriteAble ){
 $name = "$this->destination/$this->filename";
// new code
 if($succes = move_uploaded_file( $this->fileData, $name )){
return $name; 
}     
} else {
trigger_error("cannot write to $this->destination");
$succes = false;
}
return $succes;
}

Then small tweak in the calling page like :-

$fileUploaded = $uploader->save();
if ( $fileUploaded )
 {
   $insert="Insert into images (img_filename) values (?)";
   $stmt=$con->prepare($insert);
   $stmt->bind_param('s', $fileUploaded);
   $stmt->execute();

$out = "<span class='rare'>Image uploaded success!!!..</span>";

} else {
$out = "something went wrong";
}
rahul
  • 841
  • 1
  • 8
  • 18
0

In your last code, fill the $newImageSubmitted variable with the path to the uploaded image.

$fileUploaded from your first part seems to be an object, that might offer a getFilename method or something similar.

To help you, you have to provide more code. What does your Uploader-Class look like?

-- Update --

Make your Uploader-Class return $name in save-method.

$name = "$this->destination/$this->filename";
$succes = move_uploaded_file( $this->fileData, $name );

in case of success:

return $name;
Steve
  • 1,622
  • 5
  • 21
  • 39
Caz
  • 25
  • 6
  • uploading image is not a problem for me. storing image name in database is. – Steve Feb 16 '16 at 14:17
  • This is why i advise you to return the $name-variable in your save()-method. you can then use this $name variable (which is the path to your uploaded file) to store the data into your database. – Caz Feb 16 '16 at 14:20