1

I need help, I want to create a form that can upload two images, one for thumbnail and one for the main image. They should both be saved in different folders but in one database table. Thank you here is what I have done but the problem is 1. The images don't save in the database

<?php
require_once('includes/rypecms.php');
error_reporting(0);
if($_POST['submit'])
{

$lname=basename($_FILES['file_upload']['name']);
$ltname=$_FILES['file_upload']['tmp_name'];
$tname=basename($_FILES['file_upload']['tname']);
$ttname=$_FILES['file_upload']['tmp_name'];
$imagename = $_POST['imagename'];
$content = $_POST['content'];
$links = $_POST['links'];
$dir='image';
$dir2='thumb';
if(move_uploaded_file($ltname,$dir."/".$lname)){
        if(move_uploaded_file($ttname,$dir2."/".$tname)){

        {
            mysql_select_db($database_rypecms, $rypecms);
            $qur="INSERT INTO portweb (id, name, tname, imagename, content, links, pathl, patht) VALUES(' ', '$lname', '$tname', '$imagename', '$content', '$links', 'image/$lname' , 'thumb/$tname' )";
            $res=mysql_query($qur , $rypecms);
            echo 'files upload success';

        }

    }

}
}
?>

here is the form. two file upload buttons.one for thumbnail and the other for the main image

<html>
<head>
<title>upload pictures portfolio web</title>
</head>
<body>
<a href="portweb.php">Back</a>
<br />
<form action="addportweb.php" method="post" enctype="multipart/form-data">
<label>Main image:</label><input type="file" name="file_upload" /><br />
<label>Thumbnail:</label><input type="file" name="file_upload" /><br />
<label>Name:</label> <input type="text" name="imagename" class="text_input" maxlength="100" /><br />
<label>Description:</label>
<textarea name="content" style="width: 300px; height:80px; padding: 5px; resize:none;" ></textarea>
<br />
<label>Link:</label> <textarea name="links" style="width: 100px; height:50px; padding: 5px; resize:none;" ></textarea><br />
<input type="submit" name="submit" value="upload" /> 
</form>
</body>
</html>
wayzz
  • 585
  • 6
  • 23
  • show us your HTML code of the form. – vaso123 Oct 22 '14 at 13:42
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 22 '14 at 13:44
  • Choose unique name for each file input & handle accordingly. – Jenson M John Oct 22 '14 at 13:50

2 Answers2

1

Or you can use file field array in this way:

<input type="file" name="file_upload[]" />
<input type="file" name="file_upload[]" />

In this way var_dump $_FILES['file_upload'] and you will get the structure to access.

Hope it helps!

SBO
  • 623
  • 2
  • 8
  • 22
0

You should change the names of the file upload so each one would be a separate variable.

<input type="file" name="file_upload" />
<input type="file" name="file_upload_thumb" />

Then in php you should call the $_FILES['file_upload'] for the main file and the $_FILES['file_upload_thumb'] for the thumbnail file.

$lname=basename($_FILES['file_upload']['name']);
$ltname=$_FILES['file_upload']['tmp_name'];
$tname=basename($_FILES['file_upload_thumb']['tname']);
$ttname=$_FILES['file_upload_thumb']['tmp_name'];

This should help for you not to change the whole structure of the code.

wayzz
  • 585
  • 6
  • 23