...

View Full Version : Upload Multiple Photos



Jenny Dithe
11-18-2010, 02:37 PM
Hi,

Firstly I'll apologise for posting on this subject as I see there are many similar posts, yet none of them seem to answer my exact problem.

I want a user to upload an album of four photos(original size and thumbnail). I want each each photo to go into my db with it's position in the album e.g.:
album Name| photoID| position
Today's Photos|rand()|photo1
Today's Photos|rand()|photo2
Today's Photos|rand()|photo3
Today's Photos|rand()|photo4

My code works fine for one photo, it is just when I want to upload several that the problem happens. I have seen suggestions of using an array and for each but if I do that I think I lose the photoX to put in the db.

So what I have is the below:


<form action="insertalbum.php" method="post"
enctype="multipart/form-data">

<label>Album Name: </label>
<input type="text" class="input" name="album" value="Add Album Name" size="30%">
<br />
<br />
<div>
<div class="left">
<label for="file">Picture:</label>
<input type="file" name="file1" id="file" />
<br />
<br />

<label for="file">Picture:</label>
<input type="file" name="file3" id="file" />
<br />
<br />
</div>
<div class="right">

<label for="file">Picture:</label>
<input type="file" name="file2" id="file" />
<br />
<br />
<label for="file">Picture:</label>
<input type="file" name="file4" id="file" />
<br />
<br />
</div>
<br />
<br />
<div class="clear"></div>
<input type="submit" value="Submit" /><br />
</form>
<br />


Then insertalbum.php is:


<?php
define('CFDB','mydb');
define('CFDB_USER','user');
define('CFDB_PASS','password');

$status1 = '';
$randomFileName1 = '';
$fileType1 = '';
$output1 = '';

function doConnect(){
$dbh = mysql_connect(localhost, CFDB_USER, CFDB_PASS);
if(!mysql_select_db(CFDB,$dbh)) $dbh = false;
return $dbh;
}

function attemptQuery(){
global $randomFileName1;
$dbh = doConnect();
if(!$dbh) return -2;

$query = sprintf("insert INTO Album (Album, member, position, photo, date) VALUES ('{$_POST['album']}','{$_SESSION['member']}', photo1, {$randomFilename2},NOW())");
mysql_query($query,$dbh);
$state = mysql_affected_rows($dbh);
mysql_close($dbh);

return $state;
}

function handleUpload(){
global $status1, $randomFileName1, $fileType1;

if(!isset($_FILES['file1'])) {
$status1 .= "File not Uploaded. <br />\n";
return false;
}

if($_FILES['file1']['size'] > 2500000) {
$status1 .= "File Size Too Large. <br />\n";
return false;
}

if($_FILES['file1']['error'] !=0) {
$status1 .= "File Upload Error Code: ".$_FILES['file1']['error'].". <br />\n";
return false;
}

switch(strtolower($_FILES['file1']['type'])){
case 'image/jpeg':
$fileType1 = '.jpg';
$status1 .= "Uploaded File Type: JPEG. <br />\n";
break;
case 'image/pjpeg':
$fileType1 = '.jpg';
$status1 .= "Uploaded File Type: PJPEG. <br />\n";
break;
case 'image/png':
$fileType1 = '.png';
$status1 .= "Uploaded File Type: PNG. <br />\n";
break;
case 'image/gif':
$fileType1 = '.gif';
$status1 .= "Uploaded File Type: GIF. <br />\n";
break;
default:
$status1 .= "File Type Not Supported. <br />\n";
return false;
break;
}


$newFileName1 = '';
while(true){
$newFileName1 = md5(time());
if (!file_exists($newFileName1.$fileType1)) break;
}

if(move_uploaded_file($_FILES["file1"]["tmp_name"], 'upload/'.$newFileName.$fileType)!==false) {
$status1 .= "File Successfully Moved to 'upload/".$newFileName1.$fileType1."'. <br />\n";
$randomFileName1 = $newFileName1.$fileType1;
return true;
} else {
$status1 .= "Unable To Move File To Upload Folder. <br />\n";
return false;
}
}


function createThumb($source, $destination, $newWidth = 200, $newHeight = 250){

global $status1, $fileType1;

switch($fileType1){
case '.jpg':
$imageSource1 = imagecreatefromjpeg($source);
break;
case '.gif':
$imageSource1 = imagecreatefromgif($source);
break;
case '.png':
$imageSource1 = imagecreatefromgng($source);
break;
default:
$status1 .= "Thumbnail Cannot Be Created From This File Type. <br />\n";
return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail1, $imageSource1, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(!imagejpeg($thumbnail1,$destination)){
$status1 .= "Unable To Write Thumbnail. <br />\n";
return false;
} else {
$status1 .= "Thumbanil Saved. <br />\n";
return true;
}
}

$uploadOK = handleUpload();

if($uploadOK) {
$output1 .="Upload OK <hr />\n";
$thumbOK1 = createThumb('upload/'.$randomFileName1, 'thumbs/'.$randomFileName1, 200, 250);

if($thumbOK) {
$output1 .= "Thumbnail Created <hr />\n";
$affectedRows1 = attemptQuery($randomFileName1);

switch($affectedRows1) {
case -2:
$output1 .= "<b>DB Error: Unable to Connect to DB</b> <br />\n";
break;
case -1:
$output1 .= "<b>DB Error: Query Failed</b> <br />\n";
break;
case 0:
$output1 .= "<b>DB Error: No Records Updated</b> <br />\n";
break;
default:
$output1 .= "<b>$affectedRows1 Record(s) Updated</b> <hr />\n";
break;
}
} else {
$output1 = "<b>Thumbnail Not Created:</b><br />$status1<hr />\n";
}
} else {
$output1 = "<b>Upload Failed:</b><br />$status1<hr />\n";
}


$status2 = '';
$randomFileName2 = '';
$fileType2 = '';
$output2 = '';

function attemptQuery2(){
global $randomFileName2;
$dbh = doConnect();
if(!$dbh) return -2;

$query = sprintf("insert INTO Album (Album, member, position, photo, date) VALUES ('{$_POST['album']}','{$_SESSION['member']}',photo2, {$randomFilename2},NOW())");
mysql_query($query,$dbh);
$state = mysql_affected_rows($dbh);
mysql_close($dbh);

return $state;
}

function handleUpload2(){
global $status2, $randomFileName2, $fileType2;

if(!isset($_FILES['file2'])) {
$status .= "File not Uploaded. <br />\n";
return false;
}

if($_FILES['file2']['size'] > 2500000) {
$status .= "File Size Too Large. <br />\n";
return false;
}

if($_FILES['file2']['error'] !=0) {
$status .= "File Upload Error Code: ".$_FILES['file']['error'].". <br />\n";
return false;
}

switch(strtolower($_FILES['file2']['type'])){
case 'image/jpeg':
$fileType2 = '.jpg';
$status2 .= "Uploaded File Type: JPEG. <br />\n";
break;
case 'image/pjpeg':
$fileType2 = '.jpg';
$status2 .= "Uploaded File Type: PJPEG. <br />\n";
break;
case 'image/png':
$fileType2 = '.png';
$status2 .= "Uploaded File Type: PNG. <br />\n";
break;
case 'image/gif':
$fileType2 = '.gif';
$status2 .= "Uploaded File Type: GIF. <br />\n";
break;
default:
$status2 .= "File Type Not Supported. <br />\n";
return false;
break;
}


$newFileName2 = '';
while(true){
$newFileName2 = md5(time());
if (!file_exists($newFileName2.$fileType2)) break;
}

if(move_uploaded_file($_FILES["file2"]["tmp_name"], 'upload/'.$newFileName2.$fileType2)!==false) {
$status2 .= "File Successfully Moved to 'upload/".$newFileName2.$fileType2."'. <br />\n";
$randomFileName2 = $newFileName2.$fileType2;
return true;
} else {
$status2 .= "Unable To Move File To Upload Folder. <br />\n";
return false;
}
}


function createThumb2($source, $destination, $newWidth = 200, $newHeight = 250){

global $status2, $fileType2;

switch($fileType2){
case '.jpg':
$imageSource2 = imagecreatefromjpeg($source);
break;
case '.gif':
$imageSource2 = imagecreatefromgif($source);
break;
case '.png':
$imageSource2 = imagecreatefromgng($source);
break;
default:
$status2 .= "Thumbnail Cannot Be Created From This File Type. <br />\n";
return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail2, $imageSource2, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(!imagejpeg($thumbnail2,$destination)){
$status2 .= "Unable To Write Thumbnail. <br />\n";
return false;
} else {
$status2 .= "Thumbanil Saved. <br />\n";
return true;
}
}

$uploadOK = handleUpload();

if($uploadOK) {
$output2 .="Upload OK <hr />\n";
$thumbOK1 = createThumb('upload/'.$randomFileName2, 'thumbs/'.$randomFileName2, 200, 250);

if($thumbOK) {
$output2 .= "Thumbnail Created <hr />\n";
$affectedRows2 = attemptQuery($randomFileName2);

switch($affectedRows2) {
case -2:
$output2 .= "<b>DB Error: Unable to Connect to DB</b> <br />\n";
break;
case -1:
$output2 .= "<b>DB Error: Query Failed</b> <br />\n";
break;
case 0:
$output2 .= "<b>DB Error: No Records Updated</b> <br />\n";
break;
default:
$output2 .= "<b>$affectedRows2 Record(s) Updated</b> <hr />\n";
break;
}
} else {
$output2 = "<b>Thumbnail Not Created:</b><br />$status2<hr />\n";
}
} else {
$output2 = "<b>Upload Failed:</b><br />$status2<hr />\n";
}


$status3 = '';
$randomFileName3 = '';
$fileType3 = '';
$output3 = '';

function attemptQuery3(){
global $randomFileName3;
$dbh = doConnect();
if(!$dbh) return -2;

$query = sprintf("insert INTO Album (Album, member, position, photo, date) VALUES ('{$_POST['album']}','{$_SESSION['member']}', photo3, {$randomFilename3},NOW())");
mysql_query($query,$dbh);
$state = mysql_affected_rows($dbh);
mysql_close($dbh);

return $state;
}

function handleUpload3(){
global $status3, $randomFileName3, $fileType3;

if(!isset($_FILES['file3'])) {
$status .= "File not Uploaded. <br />\n";
return false;
}

if($_FILES['file3']['size'] > 2500000) {
$status .= "File Size Too Large. <br />\n";
return false;
}

if($_FILES['file3']['error'] !=0) {
$status .= "File Upload Error Code: ".$_FILES['file']['error'].". <br />\n";
return false;
}

switch(strtolower($_FILES['file3']['type'])){
case 'image/jpeg':
$fileType3 = '.jpg';
$status3 .= "Uploaded File Type: JPEG. <br />\n";
break;
case 'image/pjpeg':
$fileType3 = '.jpg';
$status3 .= "Uploaded File Type: PJPEG. <br />\n";
break;
case 'image/png':
$fileType3 = '.png';
$status3 .= "Uploaded File Type: PNG. <br />\n";
break;
case 'image/gif':
$fileType3 = '.gif';
$status3 .= "Uploaded File Type: GIF. <br />\n";
break;
default:
$status3 .= "File Type Not Supported. <br />\n";
return false;
break;
}


$newFileName3 = '';
while(true){
$newFileName3 = md5(time());
if (!file_exists($newFileName3.$fileType3)) break;
}

if(move_uploaded_file($_FILES["file3"]["tmp_name"], 'upload/'.$newFileName3.$fileType3)!==false) {
$status3 .= "File Successfully Moved to 'upload/".$newFileName3.$fileType3."'. <br />\n";
$randomFileName3 = $newFileName3.$fileType3;
return true;
} else {
$status3 .= "Unable To Move File To Upload Folder. <br />\n";
return false;
}
}


function createThumb3($source, $destination, $newWidth = 200, $newHeight = 250){

global $status3, $fileType3;

switch($fileType3){
case '.jpg':
$imageSource3 = imagecreatefromjpeg($source);
break;
case '.gif':
$imageSource3 = imagecreatefromgif($source);
break;
case '.png':
$imageSource3 = imagecreatefromgng($source);
break;
default:
$status3 .= "Thumbnail Cannot Be Created From This File Type. <br />\n";
return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail3, $imageSource3, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(!imagejpeg($thumbnail3,$destination)){
$status3 .= "Unable To Write Thumbnail. <br />\n";
return false;
} else {
$status3 .= "Thumbanil Saved. <br />\n";
return true;
}
}

$uploadOK = handleUpload();

if($uploadOK) {
$output3 .="Upload OK <hr />\n";
$thumbOK1 = createThumb('upload/'.$randomFileName3, 'thumbs/'.$randomFileName3, 200, 250);

if($thumbOK) {
$output3 .= "Thumbnail Created <hr />\n";
$affectedRows3 = attemptQuery($randomFileName3);

switch($affectedRows3) {
case -2:
$output3 .= "<b>DB Error: Unable to Connect to DB</b> <br />\n";
break;
case -1:
$output3 .= "<b>DB Error: Query Failed</b> <br />\n";
break;
case 0:
$output3 .= "<b>DB Error: No Records Updated</b> <br />\n";
break;
default:
$output3 .= "<b>$affectedRows3 Record(s) Updated</b> <hr />\n";
break;
}
} else {
$output3 = "<b>Thumbnail Not Created:</b><br />$status3<hr />\n";
}
} else {
$output3 = "<b>Upload Failed:</b><br />$status3<hr />\n";
}


$status4 = '';
$randomFileName4 = '';
$fileType4 = '';
$output4 = '';

function attemptQuery4(){
global $randomFileName4;
$dbh = doConnect();
if(!$dbh) return -2;

$query = sprintf("insert INTO Album (Album, member, position, photo, date) VALUES ('{$_POST['album']}','{$_SESSION['member']}', photo4, {$randomFilename3},NOW())");
mysql_query($query,$dbh);
$state = mysql_affected_rows($dbh);
mysql_close($dbh);

return $state;
}

function handleUpload4(){
global $status4, $randomFileName4, $fileType4;

if(!isset($_FILES['file4'])) {
$status .= "File not Uploaded. <br />\n";
return false;
}

if($_FILES['file4']['size'] > 2500000) {
$status .= "File Size Too Large. <br />\n";
return false;
}

if($_FILES['file4']['error'] !=0) {
$status .= "File Upload Error Code: ".$_FILES['file']['error'].". <br />\n";
return false;
}

switch(strtolower($_FILES['file4']['type'])){
case 'image/jpeg':
$fileType4 = '.jpg';
$status4 .= "Uploaded File Type: JPEG. <br />\n";
break;
case 'image/pjpeg':
$fileType4 = '.jpg';
$status4 .= "Uploaded File Type: PJPEG. <br />\n";
break;
case 'image/png':
$fileType4 = '.png';
$status4 .= "Uploaded File Type: PNG. <br />\n";
break;
case 'image/gif':
$fileType4 = '.gif';
$status4 .= "Uploaded File Type: GIF. <br />\n";
break;
default:
$status4 .= "File Type Not Supported. <br />\n";
return false;
break;
}


$newFileName4 = '';
while(true){
$newFileName4 = md5(time());
if (!file_exists($newFileName4.$fileType4)) break;
}

if(move_uploaded_file($_FILES["file4"]["tmp_name"], 'upload/'.$newFileName4.$fileType4)!==false) {
$status4 .= "File Successfully Moved to 'upload/".$newFileName4.$fileType4."'. <br />\n";
$randomFileName4 = $newFileName4.$fileType4;
return true;
} else {
$status4 .= "Unable To Move File To Upload Folder. <br />\n";
return false;
}
}


function createThumb4($source, $destination, $newWidth = 200, $newHeight = 250){

global $status4, $fileType4;

switch($fileType4){
case '.jpg':
$imageSource4 = imagecreatefromjpeg($source);
break;
case '.gif':
$imageSource4 = imagecreatefromgif($source);
break;
case '.png':
$imageSource4 = imagecreatefromgng($source);
break;
default:
$status4 .= "Thumbnail Cannot Be Created From This File Type. <br />\n";
return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail4, $imageSource4, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(!imagejpeg($thumbnail4,$destination)){
$status4 .= "Unable To Write Thumbnail. <br />\n";
return false;
} else {
$status4 .= "Thumbanil Saved. <br />\n";
return true;
}
}

$uploadOK = handleUpload();

if($uploadOK) {
$output4 .="Upload OK <hr />\n";
$thumbOK1 = createThumb('upload/'.$randomFileName4, 'thumbs/'.$randomFileName4, 200, 250);

if($thumbOK) {
$output4 .= "Thumbnail Created <hr />\n";
$affectedRows4 = attemptQuery($randomFileName4);

switch($affectedRows4) {
case -2:
$output4 .= "<b>DB Error: Unable to Connect to DB</b> <br />\n";
break;
case -1:
$output4 .= "<b>DB Error: Query Failed</b> <br />\n";
break;
case 0:
$output4 .= "<b>DB Error: No Records Updated</b> <br />\n";
break;
default:
$output4 .= "<b>$affectedRows4 Record(s) Updated</b> <hr />\n";
break;
}
} else {
$output4 = "<b>Thumbnail Not Created:</b><br />$status4<hr />\n";
}
} else {
$output4 = "<b>Upload Failed:</b><br />$status4<hr />\n";
}

$html= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http:/www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr">>
<head>
<title></title>

<script type="text/javascript">
function PopUp(){
alert("Thank you your Photo Album has been saved..");
window.self.close();
}
</script>
</head>
<body onload="PopUp()">
'.$output1.'<br /><br />
'.$output2.'<br /><br />
'.$output3.'<br /><br />
'.$output4.'<br /><br />
</body>
</html>';

echo $html;

exit;
?>


At the moment I am getting the error message:
Upload Failed:
File not Uploaded.

--------------------------------------------------------------------------------


Upload Failed:

--------------------------------------------------------------------------------
Upload Failed:

--------------------------------------------------------------------------------
Upload Failed:

--------------------------------------------------------------------------------

I presume this is because I am separating the



enctype="multipart/form-data">

From the individual file scripts(?) but the only other solution I arrived at was to do individual forms which doesn't seem the correct solution.

If I do do separate forms then I the move uploaded file script fails. I know you can not use this twice within a script, but as each action is different I thought it should be ok to use it.

poyzn
11-18-2010, 04:14 PM
try this, place it in one php-file


<?php

define('CFDB','mydb');
define('CFDB_USER','user');
define('CFDB_PASS','password');
define('UPLOADS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/uploads/');
define('THUMBS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/thumbs/');

class ImageUploader {

public $status;

function __construct() {
$this->db = new Mysqli('localhost', CFDB_USER, CFDB_PASS, CFDB);
}

function save($data) {
$this->db->query("INSERT INTO `Album` (`Album`, `member`, `position`, `photo`, `date`)
VALUES ('".$data['album']."', '".$_SESSION['member']."', '".$data['photo']."', '".$data['filename']."', NOW())");
return $this->db->error;
}

function upload($key) {
switch(strtolower($_FILES['file']['type'][$key])) {
case 'image/jpeg': $fileType = '.jpg';
break;
case 'image/pjpeg': $fileType = '.jpg';
break;
case 'image/png': $fileType = '.png';
break;
case 'image/gif': $fileType = '.gif';
break;
default: return false;
break;
}

$newFileName = md5(time() . $key);

if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR . $newFileName.$fileType)) {
if($this->createThumb(UPLOADS_DIR . $newFileName.$fileType, $fileType, THUMBS_DIR . $newFileName.$fileType, 200, 200)) {
$data['album'] = $_POST['album'];
$data['photo'] = $key;
$data['filename'] = $newFileName.$fileType;
if(!$this->save($data)) {
$this->status = 'Photo #'.$key.' is uploaded and saved to the db<br />';
} else {
$this->status = 'Photo #'.$key.' is uploaded but not saved in the db<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded but thumb is not created<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded <br />';
}
unlink(UPLOADS_DIR . $newFileName.$fileType);
}


function createThumb($source, $fileType, $destination, $newWidth = 200, $newHeight = 250) {

switch($fileType) {
case '.jpg': $imageSource = imagecreatefromjpeg($source);
break;
case '.gif': $imageSource = imagecreatefromgif($source);
break;
case '.png': $imageSource = imagecreatefrompng($source);
break;
default: return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail, $imageSource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(imagejpeg($thumbnail, $destination)) {
return true;
} else {
return false;
}
}



}




if(isset($_POST['upload'])) {
$uploader = new ImageUploader();
foreach($_FILES['file']['name'] as $key => $image) {
if($image) {
$uploader->upload($key);
echo $uploader->status;
}
}

} else {

?><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.add').live('click', function() {
$('<tr><td><label for="file">Picture:</label></td><td><input type="file" name="file[]" /></td><td></td></tr>').insertAfter($("#text_field"));
});
});
</script>
<form method="post" enctype="multipart/form-data">
<table>
<tr><td><label>Album Name: </label></td>
<td colspan="2"><input type="text" class="input" name="album" value="Add Album Name" size="30%"></td></tr>
<tr id="text_field"><td><label for="file">Picture:</label></td>
<td><input type="file" name="file[]" /></td>
<td><input type="button" class="add" value=" + " /></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="upload" value="Submit" /></td></tr>
</table>
</form><?php

}


UPDATED

Jenny Dithe
11-19-2010, 07:06 AM
Thank you. I really like the + button.

It is not currently uploading nor giving me any output to let to me know where it is failing.

would you have any recommendations on where to focus?

poyzn
11-19-2010, 10:07 AM
lets focus on the uploading first
check your uploads and thumbs folders, their paths in the beginning of the script and if they are exist and writeble for the script


<?php

error_reporting(E_ALL);

// define('CFDB','mydb');
// define('CFDB_USER','user');
// define('CFDB_PASS','password');
define('UPLOADS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/uploads/');
define('THUMBS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/thumbs/');

class ImageUploader {

public $status;

/* function __construct() {
$this->db = new Mysqli('localhost', CFDB_USER, CFDB_PASS, CFDB);
}

function save($data) {
$this->db->query("INSERT INTO `Album` (`Album`, `member`, `position`, `photo`, `date`)
VALUES ('".$data['album']."', '".$_SESSION['member']."', '".$data['photo']."', '".$data['filename']."', NOW())");
return $this->db->error;
} */

function upload($key) {
switch(strtolower($_FILES['file']['type'][$key])) {
case 'image/jpeg': $fileType = '.jpg';
break;
case 'image/pjpeg': $fileType = '.jpg';
break;
case 'image/png': $fileType = '.png';
break;
case 'image/gif': $fileType = '.gif';
break;
default: return false;
break;
}

$newFileName = md5(time() . $key);

if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR . $newFileName.$fileType)) {
if($this->createThumb(UPLOADS_DIR . $newFileName.$fileType, $fileType, THUMBS_DIR . $newFileName.$fileType, 200, 200)) {
$data['album'] = $_POST['album'];
$data['photo'] = $key;
$data['filename'] = $newFileName.$fileType;
//if(!$this->save($data)) {
if(true) {
$this->status = 'Photo #'.$key.' is uploaded and saved to the db<br />';
} else {
$this->status = 'Photo #'.$key.' is uploaded but not saved in the db<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded but thumb is not created<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded <br />';
}
unlink(UPLOADS_DIR . $newFileName.$fileType);
}


function createThumb($source, $fileType, $destination, $newWidth = 200, $newHeight = 250) {

switch($fileType) {
case '.jpg': $imageSource = imagecreatefromjpeg($source);
break;
case '.gif': $imageSource = imagecreatefromgif($source);
break;
case '.png': $imageSource = imagecreatefrompng($source);
break;
default: return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail, $imageSource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(imagejpeg($thumbnail, $destination)) {
return true;
} else {
return false;
}
}



}




if(isset($_POST['upload'])) {
$uploader = new ImageUploader();
foreach($_FILES['file']['name'] as $key => $image) {
if($image) {
$uploader->upload($key);
echo $uploader->status;
}
}

} else {

?><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.add').live('click', function() {
$('<tr><td><label for="file">Picture:</label></td><td><input type="file" name="file[]" /></td><td></td></tr>').insertAfter($("#text_field"));
});
});
</script>
<form method="post" enctype="multipart/form-data">
<table>
<tr><td><label>Album Name: </label></td>
<td colspan="2"><input type="text" class="input" name="album" value="Add Album Name" size="30%"></td></tr>
<tr id="text_field"><td><label for="file">Picture:</label></td>
<td><input type="file" name="file[]" /></td>
<td><input type="button" class="add" value=" + " /></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="upload" value="Submit" /></td></tr>
</table>
</form><?php

}

Jenny Dithe
11-19-2010, 04:54 PM
OK, that works fine for the uploads. I started slowly adding other lines back in and I get the error:
Class 'Mysqli' not found

I tried with Mysql as well and received the same error.

poyzn
11-19-2010, 05:00 PM
ok, try with this.
it must be that the mysqli extension is disabled on your system.
I've changed script for plain mysql function, but almost forgot how to use them :)


<?php

error_reporting(E_ALL);

define('CFDB','mydb');
define('CFDB_USER','user');
define('CFDB_PASS','password');
define('UPLOADS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/uploads/');
define('THUMBS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/thumbs/');

class ImageUploader {

public $status;

function __construct() {
$dbh = mysql_connect(localhost, CFDB_USER, CFDB_PASS);
mysql_select_db(CFDB, $dbh);
}

function save($data) {
if(mysql_query("INSERT INTO `Album` (`Album`, `member`, `position`, `photo`, `date`)
VALUES ('".$data['album']."', '".$_SESSION['member']."', '".$data['photo']."', '".$data['filename']."', NOW())")) {
return true;
}

return;
}

function upload($key) {
switch(strtolower($_FILES['file']['type'][$key])) {
case 'image/jpeg': $fileType = '.jpg';
break;
case 'image/pjpeg': $fileType = '.jpg';
break;
case 'image/png': $fileType = '.png';
break;
case 'image/gif': $fileType = '.gif';
break;
default: return false;
break;
}

$newFileName = md5(time() . $key);

if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR . $newFileName.$fileType)) {
if($this->createThumb(UPLOADS_DIR . $newFileName.$fileType, $fileType, THUMBS_DIR . $newFileName.$fileType, 200, 200)) {
$data['album'] = $_POST['album'];
$data['photo'] = $key;
$data['filename'] = $newFileName.$fileType;
if(!$this->save($data)) {
$this->status = 'Photo #'.$key.' is uploaded and saved to the db<br />';
} else {
$this->status = 'Photo #'.$key.' is uploaded but not saved in the db<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded but thumb is not created<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded <br />';
}
unlink(UPLOADS_DIR . $newFileName.$fileType);
}


function createThumb($source, $fileType, $destination, $newWidth = 200, $newHeight = 250) {

switch($fileType) {
case '.jpg': $imageSource = imagecreatefromjpeg($source);
break;
case '.gif': $imageSource = imagecreatefromgif($source);
break;
case '.png': $imageSource = imagecreatefrompng($source);
break;
default: return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail, $imageSource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(imagejpeg($thumbnail, $destination)) {
return true;
} else {
return false;
}
}



}




if(isset($_POST['upload'])) {
$uploader = new ImageUploader();
foreach($_FILES['file']['name'] as $key => $image) {
if($image) {
$uploader->upload($key);
echo $uploader->status;
}
}

} else {

?><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.add').live('click', function() {
$('<tr><td><label for="file">Picture:</label></td><td><input type="file" name="file[]" /></td><td></td></tr>').insertAfter($("#text_field"));
});
});
</script>
<form method="post" enctype="multipart/form-data">
<table>
<tr><td><label>Album Name: </label></td>
<td colspan="2"><input type="text" class="input" name="album" value="Add Album Name" size="30%"></td></tr>
<tr id="text_field"><td><label for="file">Picture:</label></td>
<td><input type="file" name="file[]" /></td>
<td><input type="button" class="add" value=" + " /></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="upload" value="Submit" /></td></tr>
</table>
</form><?php

}

Jenny Dithe
11-20-2010, 07:18 AM
OK the result message I get is:
Photo #0 is uploaded but not saved in the db
Photo #1 is uploaded but not saved in the db

When I echo out the mysql_query I only see the $_SESSION['member'] information.

Which makes me think $data needs to be defined - possibly?

Thank you for helping with this.

poyzn
11-20-2010, 07:32 AM
sorry, there was wrong condition rule, try this script
$data is defined in the upload function



<?php

error_reporting(E_ALL);

define('CFDB','mydb');
define('CFDB_USER','user');
define('CFDB_PASS','password');
define('UPLOADS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/uploads/');
define('THUMBS_DIR', $_SERVER['DOCUMENT_ROOT'] . '/thumbs/');

class ImageUploader {

public $status;

function __construct() {
$dbh = mysql_connect(localhost, CFDB_USER, CFDB_PASS);
mysql_select_db(CFDB, $dbh);
}

function save($data) {
if(mysql_query("INSERT INTO `Album` (`Album`, `member`, `position`, `photo`, `date`)
VALUES ('".$data['album']."', '".$_SESSION['member']."', '".$data['photo']."', '".$data['filename']."', NOW())")) {
return true;
}

return;
}

function upload($key) {
switch(strtolower($_FILES['file']['type'][$key])) {
case 'image/jpeg': $fileType = '.jpg';
break;
case 'image/pjpeg': $fileType = '.jpg';
break;
case 'image/png': $fileType = '.png';
break;
case 'image/gif': $fileType = '.gif';
break;
default: return false;
break;
}

$newFileName = md5(time() . $key);

if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR . $newFileName.$fileType)) {
if($this->createThumb(UPLOADS_DIR . $newFileName.$fileType, $fileType, THUMBS_DIR . $newFileName.$fileType, 200, 200)) {
$data['album'] = $_POST['album'];
$data['photo'] = $key;
$data['filename'] = $newFileName.$fileType;
if($this->save($data)) {
$this->status = 'Photo #'.$key.' is uploaded and saved to the db<br />';
} else {
$this->status = 'Photo #'.$key.' is uploaded but not saved in the db<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded but thumb is not created<br />';
}
} else {
$this->status = 'Photo #'.$key.' is uploaded <br />';
}
unlink(UPLOADS_DIR . $newFileName.$fileType);
}


function createThumb($source, $fileType, $destination, $newWidth = 200, $newHeight = 250) {

switch($fileType) {
case '.jpg': $imageSource = imagecreatefromjpeg($source);
break;
case '.gif': $imageSource = imagecreatefromgif($source);
break;
case '.png': $imageSource = imagecreatefrompng($source);
break;
default: return false;
break;
}

list($oldWidth, $oldHeight) = getimagesize($source);

$thumbnail = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($thumbnail, $imageSource, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);

if(imagejpeg($thumbnail, $destination)) {
return true;
} else {
return false;
}
}



}




if(isset($_POST['upload'])) {
$uploader = new ImageUploader();
foreach($_FILES['file']['name'] as $key => $image) {
if($image) {
$uploader->upload($key);
echo $uploader->status;
}
}

} else {

?><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.add').live('click', function() {
$('<tr><td><label for="file">Picture:</label></td><td><input type="file" name="file[]" /></td><td></td></tr>').insertAfter($("#text_field"));
});
});
</script>
<form method="post" enctype="multipart/form-data">
<table>
<tr><td><label>Album Name: </label></td>
<td colspan="2"><input type="text" class="input" name="album" value="Add Album Name" size="30%"></td></tr>
<tr id="text_field"><td><label for="file">Picture:</label></td>
<td><input type="file" name="file[]" /></td>
<td><input type="button" class="add" value=" + " /></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="upload" value="Submit" /></td></tr>
</table>
</form><?php

}

Jenny Dithe
11-20-2010, 08:09 AM
Of course data is, my mistake.

I'm afraid I can't see where the alteration is you have made, could you comment or reference it for me.

poyzn
11-20-2010, 11:30 AM
in the upload function if($this->save($data)) versus if(!$this->save($data)) in previous scripts. Does the script work correctly?

Jenny Dithe
11-20-2010, 12:37 PM
All works fantastically. Thank you so much. I really appreciate your help and time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum