abelvf
abelvf

Reputation: 49

Upload process don't save the right name in database

I have a code in php with validation ok all working properly but my problem is that when I try to save in database I obtain something like this:

img_id   img_small           img_big           
  5      /tmp/phpdlYkiG      /tmp/phph3dhka

I don't know why php save that name because the images have a diffent names like koala.jpg and horse.jpg

Here is my code in order to see if somebody have any suggestion...

<form enctype="multipart/form-data" action="upload_type_1.php" method="POST" >        
  <input type="file" name="img_small_1" id="img_small_1">
  <input type="file" name="img_big_1" id="img_big_1">

  <input type="submit" value="Upload" name="submit">                                   
</form>

and this is my php code:

if ( (move_uploaded_file($_FILES["img_small_1"]["tmp_name"], $target)) && (move_uploaded_file($_FILES["img_big_1"]["tmp_name"], $target2)) ){
    $img_title_1 = $_POST['img_title_1'];

    $sql = "INSERT INTO press (img_title, img_small, img_big) VALUES ('$img_title_1', '$img_small_1', '$img_big_1')";

    $retval = mysql_query( $sql, $conn );

    if(!$retval) {
      die('Could not enter data: ' . mysql_error());
    }

    mysql_close($conn);

    echo "Your files has been uploaded";        

} else {

    echo "Sorry, there was an error uploading your files."; 

    exit;
}

This code work properly the only problem is that save into database that strange names and I need to use that names...

Thanks! - Waiting for help!

Upvotes: 1

Views: 33

Answers (3)

user3419778
user3419778

Reputation: 866

A Simple Example of File Uploading

$uploadDir = "Your_upload_dir";
$img_small = $_FILES['img_small_1'];

$img_small_name = $img_small['name']; // get image name
$img_small_tmpName = $img_small['tmp_name'];
$img_small_fileSize = $img_small['size'];
$img_small_fileType = $img_small['type'];

if ($img_small['error'] == 0)
{
    $img_small_filePath = $uploadDir . $img_small_name;
    $result = move_uploaded_file($img_small_tmpName,  img_small_filePath); //return true or false

}

Upvotes: 0

Sachink
Sachink

Reputation: 1530

$img_title_1 = $_POST['img_title_1'];

Should be:

$img_title_1 = $_FILES["img_small_1"]["name"]

Upvotes: 0

Sean
Sean

Reputation: 12433

Your issue is probably not in the code that you are showing but in the code you are not showing, which is your variable declarations for $img_small_1 && $img_big_1. Taking a guess you have

$img_small_1 = $_FILES["img_small_1"]["tmp_name"];
$img_big_1 = $_FILES["img_big_1"]["tmp_name"];

but you want/need

$img_small_1 = $_FILES["img_small_1"]["name"];
$img_big_1 = $_FILES["img_big_1"]["name"];

Upvotes: 2

Related Questions