1

Hi there,

I've found quite a nice flash / javascript / php based multiple image uploader called FancyUpload.

http://digitarald.de/project/fancyupload/

I've got it working fine, however i need to be able to insert the data into a database

Below is my database structure:

photo_ref
event_ref
photo_thumb
heat

Basically i want to be able to populate the database. because at the moment the script only put the images on the webspace.

So for example if i upload 5 images the database should look like the following:

photo_ref: 1, 2, 3, 4, 5 (auto increment)
event_ref: 1, 1, 1, 1, 1
photo_thumb: image_1.jpg, image_2.jpg, image_3.jpg, etc
heat: test heat, etc

I also want to be able to rename the original file name so that it matches what goes into the database, so image_1_test_heat.jpg. something along the lines of that.

I've managed to do this with my own script but that involves having 5 seperate input files, not the one that fancyupload is using.

If anyone can point me in the right direction i should be able to crack it myself! (Hopefully).

Cheers,

Dan

below is my code for uploading the image:

<?php
include("JSON.php");
$result = array();
 
if (isset($_FILES['photoupload']) )
{
	$file = $_FILES['photoupload']['tmp_name'];
	$error = false;
	$size = false;
 
	if (!is_uploaded_file($file) || ($_FILES['photoupload']['size'] > 2 * 1024 * 1024) )
	{
		$error = 'Please upload only files smaller than 2Mb!';
	}
	if (!$error && !($size = @getimagesize($file) ) )
	{
		$error = 'Please upload only images, no other files are supported.';
	}
	if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) )
	{
		$error = 'Please upload only images of type JPEG.';
	}
	if (!$error && ($size[0] < 25) || ($size[1] < 25))
	{
		$error = 'Please upload an image bigger than 25px.';
	}
	else {
		move_uploaded_file($_FILES['photoupload']['tmp_name'], "uploadedfiles/".$_FILES['photoupload']['name']);
	}
 
	$addr = gethostbyaddr($_SERVER['REMOTE_ADDR']);
 
	$log = fopen('script.log', 'a');
	fputs($log, ($error ? 'FAILED' : 'SUCCESS') . ' - ' . preg_replace('/^[^.]+/', '***', $addr) . ": {$_FILES['photoupload']['name']} - {$_FILES['photoupload']['size']} byte\n" );
	fclose($log);
 
	if ($error)
	{
		$result['result'] = 'failed';
		$result['error'] = $error;
	}
	else
	{
		$result['result'] = 'success';
		$result['size'] = "Uploaded an image ({$size['mime']}) with  {$size[0]}px/{$size[1]}px.";
	}
 
}
else
{
	$result['result'] = 'error';
	$result['error'] = 'Missing file or internal error!';
}
 
if (!headers_sent() )
{
	header('Content-type: application/json');
}
 
echo json_encode($result);
 
?>
4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by kyawswarthant
0

Do you know how to preform a database query? setup a connection?
If not you need to check out ww.php.net for some uide lines and where to start like mysql_connect, mysql_query, etc.
If you do, then i cant see any database manipulation in your code so...
Well either way thats a place to start, so begin! let me know how you go!

0

Hi Omnix,

Cheers for replying, yes i do know how to set up a connection etc. I have managed to get somewhere using the following code

echo $files = $_FILES['photoupload']['name'];
		
		for($i=0;$i<count($files);$i++) {
		
		// Open Connection to DB
		require_once ('../include/config.inc_admin.php');
		require_once (MYSQL);
		
		$q = "INSERT INTO ep_images (event_ref, photo_thumb, heat_cat) VALUES ('1', '$files[$i]', 'test')";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		}

That loops through the images and inserts them into the database, however the image name only inserts the first letter of the file name.

So for example if my image was called mountains.jpg it would insert as :

photo_thumb -> m

and miss out the rest!

Any idea on why its doing that?

I'll re post my full code with the sql statement in so you know what i'm working with

<?php
include("JSON.php");
$result = array();
 
if (isset($_FILES['photoupload']) )
{
	$file = $_FILES['photoupload']['tmp_name'];
	$error = false;
	$size = false;
 
	if (!is_uploaded_file($file) || ($_FILES['photoupload']['size'] > 2 * 1024 * 1024) )
	{
		$error = 'Please upload only files smaller than 2Mb!';
	}
	if (!$error && !($size = @getimagesize($file) ) )
	{
		$error = 'Please upload only images, no other files are supported.';
	}
	if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) )
	{
		$error = 'Please upload only images of type JPEG.';
	}
	if (!$error && ($size[0] < 25) || ($size[1] < 25))
	{
		$error = 'Please upload an image bigger than 25px.';
	}
	else {
		move_uploaded_file($_FILES['photoupload']['tmp_name'], "uploadedfiles/".$_FILES['photoupload']['name']);
	}
 
	$addr = gethostbyaddr($_SERVER['REMOTE_ADDR']);
 
	$log = fopen('script.log', 'a');
	fputs($log, ($error ? 'FAILED' : 'SUCCESS') . ' - ' . preg_replace('/^[^.]+/', '***', $addr) . ": {$_FILES['photoupload']['name']} - {$_FILES['photoupload']['size']} byte\n" );
	fclose($log);
 
	if ($error)
	{
		$result['result'] = 'failed';
		$result['error'] = $error;
	}
	else
	{
	
		echo $files = $_FILES['photoupload']['name'];
		
		for($i=0;$i<count($files);$i++) {
		
		// Open Connection to DB
		require_once ('../include/config.inc_admin.php');
		require_once (MYSQL);
		
		$q = "INSERT INTO ep_images (event_ref, photo_thumb, heat_cat) VALUES ('1', '$files[$i]', 'test')";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		}
	
		$result['result'] = 'success';
		$result['size'] = "Uploaded an image ({$size['mime']}) with  {$size[0]}px/{$size[1]}px.";
	}
 
}
else
{
	$result['result'] = 'error';
	$result['error'] = 'Missing file or internal error!';
}
 
if (!headers_sent() )
{
	header('Content-type: application/json');
}
 
echo json_encode($result);
 
?>

If you or anyone else could help it would be greatly appreciated!

Cheers,

Dan

0

This is your problem, when you insert a row.

$q = "INSERT INTO ep_images (event_ref, photo_thumb, heat_cat) VALUES ('1', '$files[$i]', 'test')";
'$files[$i]' // You are refering to the first character in your file name.
$_FILES["file"]["name"] [$i] // Start with $_FILES["file"]["name"] then work to a multi dimensional array maybe?

Hope that helps and let me know how you go :)

Edited by OmniX: n/a

0

I can not make this step.

So for example if my image was called mountains.jpg it would insert as :

photo_thumb -> m

and miss out the rest!

Any idea on why its doing that?

I'll re post my full code with the sql statement in so you know what i'm working with....

how to write multidimensional array...
please, show this code....

0

please, I'm beginner...
I study your code...
at now, I'm not ok..
I want to know that code..
I write this sample. But, I'm not ok..

0
<?php
$result = array();
$result['time'] = date('r');
$result['addr'] = substr_replace(gethostbyaddr($_SERVER['REMOTE_ADDR']), '******', 0, 6);
$result['agent'] = $_SERVER['HTTP_USER_AGENT'];
if (count($_GET)) {
    $result['get'] = $_GET;
}
if (count($_POST)) {
    $result['post'] = $_POST;
}
if (count($_FILES)) {
    $result['files'] = $_FILES;
}
$log = @fopen('script.log', 'a');

if ($log) { 
    fputs($log, print_r($result, true) . "\n---\n");
    fclose($log);
}

$error = false;
if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
    $error = 'Invalid Upload';
}
else
{
move_uploaded_file($_FILES['Filedata']['tmp_name'], 'uploads/' . $_FILES['Filedata']['name']);
$return['src'] = 'uploads/' . $_FILES['Filedata']['name'];

$normalfilepath='uploads/' . $_FILES['Filedata']['name'];
$thumbfilepath='thumb/' ."test_".$_FILES['Filedata']['name'];
$thumbfilepath['src']='thumb/' ."test_".$_FILES['Filedata']['name'];

copy($HTTP_POST_FILES['Filedata']['tmp_name'],$normalfilepath);     
    $newfile=$normalfilepath;
    list ($width,$height)=getimagesize($newfile);
//New file size
        $newwidth="90";
        $newheight="67";
//Load File
    $source = imagecreatefromjpeg($newfile);
    $thumb = imagecreatetruecolor($newwidth, $newheight);           

imagecopyresized($thumb,$source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Copy into thumb file
imagejpeg($thumb,$thumbfilepath);   
}

if ($error) {

    $return = array(
        'status' => '0',
        'error' => $error
    );

} else {
    $a=null;
    $b=null;
    $c=null;
    $d=null;
     $file=$_FILES['Filedata']['name'];
    //foreach($file as $key=>$value)
for($i=0;$i<count($file);$i++)
    {
            $con=mysql_connect("localhost","root","");
            mysql_select_db("upload",$con); 
            $rel=mysql_query("INSERT INTO tbl_upload(jpg_1,count,jpg_2,jpg_3) VALUES('$file','$file','$file','$file')");
    }       

}

/* if (isset($_REQUEST['response']) && $_REQUEST['response'] == 'xml') {
     header('Content-type: text/xml');  
    echo '<response>';   
    foreach ($return as $key => $value) {
        echo "<$key><![CDATA[$value]]></$key>";        
    }   
    echo '</response>';
} else {
     header('Content-type: application/json');
    echo json_encode($return);
} */

?>

how should I do....
I want to get multiple column in a row...
I'm not ok.
pls, help me..

Edited by mike_2000_17: Fixed formatting

This question has already been answered. 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.