Problem with PHP/MYSQL Image gallery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
onoffpaul
Forum Newbie
Posts: 3
Joined: Thu Jul 10, 2008 11:20 am

Problem with PHP/MYSQL Image gallery

Post by onoffpaul »

Hi,

I have recently built a PHP/MYSQL gallery and I had it working locally (and also live on a test hosting server where I tested it).

I have now moved it to the live server where the site is and while I am not getting any error messages, I cannot insert data (images/albums/text titles and descriptions of pics, etc.) into the db tables OR upload images via the content management system I built (and was working fine locally and on the test server!).

Basically when I go to add an album (and upload the album cover image) the page loads as if it was upload sucessfully but neither the data has been written to the database or the image uploads!

I have obviously changed all the username/password details in my config file but no luck.

I feel that there is something really obvious I am overlooking but I just can't see it! Some help would be really appreciated!

Here is the code for the 'add_album.php' page (for add an album and uploading a flyer and cover page):

Code: Select all

 
 
<?php
require_once '../library/config.php';
require_once '../library/functions.php';
if(isset($_POST['txtName']))
{
    $albumName = $_POST['txtName'];
    $albumDesc = $_POST['mtxDesc'];
    $newsDesc = $_POST['newsDesc'];
 
    $imgName   = $_FILES['fleImage']['name'];
    $tmpName   = $_FILES['fleImage']['tmp_name'];
    
    /*-- Code for adding the newsletter image--*/
    $imgName2   = $_FILES['fleImage2']['name'];
    $tmpName2   = $_FILES['fleImage2']['tmp_name'];
 
    // we need to rename the image name just to avoid
    // duplicate file names
    // first get the file extension
    $ext = strrchr($imgName, ".");
    $ext2 = strrchr($imgName2, ".");
    
    // then create a new random name
    $newName = md5(rand() * time()) . $ext;
    $newName2 = md5(rand() * time()) . $ext2;
 
    // the album image will be saved here
    $imgPath = ALBUM_IMG_DIR . $newName;
    
    // the flyer image will be saved here
    $imgPath2 = FLYER_IMG_DIR . $newName2;
    
    // resize all album image
    $result = createThumbnail($tmpName, $imgPath,ALBUM_THUMBNAIL_WIDTH, ALBUM_THUMBNAIL_HEIGHT);
    
    // resize all flyer image
    $result2 = createThumbnail($tmpName2, $imgPath2, FLYER_THUMBNAIL_WIDTH, FLYER_THUMBNAIL_HEIGHT);
    
    if (!$result) {
        echo "Error uploading file - NB: NO FIELDS MUST BE LEFT BLANK!";
        exit;
    }
    
    if (!$result2) {
        echo "Error uploading file - NB: NO FIELDS MUST BE LEFT BLANK!";
        exit;
    }
    
    if (!get_magic_quotes_gpc()) {
        $albumName  = addslashes($albumName);
        $albumDesc  = addslashes($albumDesc);
        //$newsDesc  = addslashes($newsDesc);
    }  
 
                            /*$query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date) 
                            VALUES ('$albumName', '$albumDesc', '$newName', NOW())";
                            
                            $query2 = "INSERT INTO tbl_newsletter (news_description) VALUES ('$newsDesc')";
                            
                            mysql_query($query) or die('Error, add album failed : ' . mysql_error());
                            mysql_query($query2) or die('Error, add album failed : ' . mysql_error());*/
                            
                            
                            
                            // Inserting the data into users
                            // -----------------------------
                            
                            $data = $_FILES['fleImage'];
                            
                            $sql = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date, bin_data,filename,filesize,filetype) 
                            VALUES ('$albumName', '$albumDesc', '$newName', NOW(), '$data','$form_data_name','$form_data_size','$form_data_type')";
                            
                            $result = mysql_query( $sql,$conn );//CHECK THIS VARIABLE!!!
                            
                            
                            if(isset($_POST['txtName']))
                            {
                               
                              $user_id = mysql_insert_id( $conn );
                            
                              
                              $sql = "INSERT INTO tbl_newsletter (news_album_id, news_image) VALUES ($user_id, '$newName2')";
                              
                              $result = mysql_query($sql, $conn);
                            }
                            mysql_free_result( $result );
                            // -----------------------------                    
 
    // the album is saved, go to the album list 
    echo "<script>window.location.href='index.php?page=list-album';</script>";
    exit;
}       
?>
 
<form action="" method="post" enctype="multipart/form-data" name="frmAlbum" id="frmAlbum">
    <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_grey">
        <tr> 
            <th width="150">Album Name</th>
            <td width="80" bgcolor="#FFFFFF"> <input name="txtName" type="text" id="txtName"></td>
        </tr>
        <tr> 
            <th width="150">Description</th>
            <td bgcolor="#FFFFFF"> <textarea name="mtxDesc" cols="50" rows="4" id="mtxDesc"></textarea>            </td>
        </tr>
        <tr> 
            <th width="150">Club Flyer</th>
            <td bgcolor="#FFFFFF"> <input name="fleImage" type="file" class="box" id="fleImage"><input type="hidden" name="MAX_FILE_SIZE" value="1000000"></td>
        </tr>
        <tr>
          <th width="150">Newsletter Image</th>
          <td bgcolor="#FFFFFF"><input name="fleImage2" type="file" class="box" id="fleImage2" /></td>
        </tr>
        <tr> 
            <td width="150" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF"> <input name="btnAdd" type="submit" id="btnAdd" value="Add Album"> 
                <input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();"></td>
        </tr>
    </table>
</form>
 
 
Here is the code for the functions page:

Code: Select all

 
<?php
/*
    Upload an image and create the thumbnail. The thumbnail is stored 
    under the thumbnail sub-directory of $uploadDir.
    
    Return the uploaded image name and the thumbnail also.
*/
function uploadImage($inputName, $uploadDir)
{
    $image     = $_FILES[$inputName];
    $imagePath = '';
    $thumbnailPath = '';
    
    // if a file is given
    if (trim($image['tmp_name']) != '') {
        $ext = substr(strrchr($image['name'], "."), 1); 
 
        // generate a random new file name to avoid name conflict
        // then save the image under the new file name
        $imagePath = md5(rand() * time()) . ".$ext";
        $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
            
        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";
            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
            
            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            // the image cannot be uploaded
            $imagePath = $thumbnailPath = '';
        }
        
    }
    
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
 
/*
    Create a thumbnail of $srcFile and save it to $destFile.
    The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $height, $quality = 75)
{
    $thumbnail = '';
    
    if (file_exists($srcFile)  && isset($destFile))
    {
        $size        = getimagesize($srcFile);
        $w           = number_format($width, 0, ',', '');
        $h           = number_format($height, 0, ',', '');
        
        $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
    }
    
    // return the thumbnail file name on sucess or blank on fail
    return basename($thumbnail);
}
 
 
 
//Function to create the album thumbnails
function createThumbnail2($srcFile, $destFile, $width, $height, $quality = 75)
{
    $thumbnail = '';
    
    if (file_exists($srcFile)  && isset($destFile))
    {
        $size        = getimagesize($srcFile);
        $w           = number_format($width, 0, ',', '');
        $h           = number_format($height, 0, ',', '');
        
        $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
    }
    
    // return the thumbnail file name on sucess or blank on fail
    return basename($thumbnail);
}
 
/*
    Copy an image to a destination file. The destination
    image size will be $w X $h pixels
    
    
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
{
   $destFile  = substr_replace($destFile, 'jpg', -3);
   $dest      = imagecreatetruecolor($w, $h);
   //imageantialias($dest, TRUE);
} elseif ($tmpDest['extension'] == "jpeg")
{
  $destFile  = substr_replace($destFile, 'jpg', -4);
   $dest      = imagecreatetruecolor($w, $h);   
} elseif ($tmpDest['extension'] == "png") {
   $dest = imagecreatetruecolor($w, $h);
   //imageantialias($dest, TRUE);
} else {
  return false;
} 
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);
 
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
{
   $destFile  = substr_replace($destFile, 'jpg', -3);
   $dest      = imagecreatetruecolor($w, $h);
   //imageantialias($dest, TRUE);
} elseif ($tmpDest['extension'] == "jpeg")
{
  $destFile  = substr_replace($destFile, 'jpeg', -4);
   $dest      = imagecreatetruecolor($w, $h);   
} elseif ($tmpDest['extension'] == "png") {
   $dest = imagecreatetruecolor($w, $h);
   //imageantialias($dest, TRUE);
} else {
  return false;
} 
 
    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }
 
    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
 
    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;
 
}
 
?>
 
Thanks!

Paul
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with PHP/MYSQL Image gallery

Post by califdon »

Since the script was working both on your development system and another hosting server, the problem would appear not to be with your code. Are you sure that the database and table exist on the production server? Can you access the database with phpMyAdmin or similar software? Or even write a short test script just to connect and return a record count or something?
Post Reply