Jump to content

Upload logic


graham23s

Recommended Posts

Hi Guys,

 

I'm sure my logic is off somewhere in my code, basicaly i resize an uploaded image and move to it's location depending on whether it came from the US or UK site:

 

Resize Function:

 

function resize_image($uploadDirectory, $newFileName, $us = false)
{
    $original_image   = $uploadDirectory;
    $ext              = substr($original_image, strrpos($original_image, '.') + 1);
    $canvas_width     = 65;
    $canvas_height    = 65;
    $canvas           = imagecreatetruecolor($canvas_width, $canvas_height);
    $white_background = imagecolorallocate($canvas, 255, 255, 255);
    
    imagefill($canvas, 0, 0, $white_background);
    list($image_width, $image_height) = getimagesize($uploadDirectory);
    
    $ratio = $image_width / $image_height;
    if ($ratio > 1) {
        $new_image_width  = 65;
        $new_image_height = 65 / $ratio;
    } //$ratio > 1
    else {
        $new_image_width  = (float) 65 * $ratio;
        $new_image_height = 65;
    }
    
    if ($ext == "jpg") {
        $original_image = imagecreatefromjpeg($original_image);
    } //$ext == "jpg"
    if ($ext == "gif") {
        $original_image = imagecreatefromgif($original_image);
    } //$ext == "gif"
    
    imagecopyresampled($canvas, $original_image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
    
    $new_thumbnail_name = "thumb-$newFileName";
    
    if ($ext == "jpg") {
    
    if ($us)
    {
    
      imagejpeg($canvas, "../../site.com/imgProducts/img-th/$new_thumbnail_name", 100);
      return ("$new_thumbnail_name");
    
    } else {
    
      imagejpeg($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
      return ("$new_thumbnail_name");        
    
    }
     
    } //$ext == "jpg"
    
    if ($ext == "gif") {
    
    if ($us)
    {
    
      imagegif($canvas, "../../first-site.com/imgProducts/img-th/$new_thumbnail_name", 100);
      return ("$new_thumbnail_name");
    
    } else {
    
      imagegif($canvas, "../imgProducts/img-th/$new_thumbnail_name", 100);
      return ("$new_thumbnail_name");      
    
    }   
        
        
    } //$ext == "gif"
    imagedestroy($original_image);
    imagedestroy($canvas);
}

 

Upload Code:

 

<?php
if (isset($_POST['submit-add-product'])) 
{

    $productNM = ucwords($_POST['txt-product-name']);
    $productDS = escape_data($_POST['txt-product-description'], 1);
    $productPM = $_POST['txt-product-p-medicine'];
    $productCT = $_POST['txt-product-category'];
    $productPR = $_POST['txt-product-price'];
    $productST = $_POST['txtSite'];
    $fileName  = $_FILES['txt-product-image']['name'];
    $fileTemp  = $_FILES['txt-product-image']['tmp_name'];
    $fileType  = $_FILES['txt-product-image']['type'];
    $fileSize  = $_FILES['txt-product-image']['size'];

    /*if (!empty($_FILES['txt-product-image-2']['name'])) {
        $fileName2nd        = $_FILES['txt-product-image-2']['name'];
        $fileTemp2nd        = $_FILES['txt-product-image-2']['tmp_name'];
        $fileType2nd        = $_FILES['txt-product-image-2']['type'];
        $fileSize2nd        = $_FILES['txt-product-image-2']['size'];
        $fileExtension      = substr(strrchr($fileName2nd, '.'), 0);
        $fileExtension      = strtolower($fileExtension);
        $replacedName2nd    = str_replace(" ", "-", $productNM . "-2nd");
        $newFileName2nd     = "$replacedName2nd-" . time() . "$fileExtension";
        $uploadDirectory2nd = "../imgProducts/img-fs/$newFileName2nd";
        $generate2ndImage   = true;
    } //!empty($_FILES['txt-product-image-2']['name'])
    
    if (empty($productNM)) {
        print "<p class=\"fcp-message-error\">You never entered a product name.</p>";
    } //empty($productNM)
    if (empty($productDS)) {
        print "<p class=\"fcp-message-error\">You never entered a product description.</p>";
    } //empty($productDS)
    if (empty($productPR)) {
        print "<p class=\"fcp-message-error\">You never entered a product price.</p>";
    } //empty($productPR)
    if (empty($fileSize)) {
        print "<p class=\"fcp-message-error\">You never entered a product image.</p>";
    } //empty($fileSize)
    if (!isImageFile($fileType)) {
        print "<p class=\"fcp-message-error\">The file you uploaded doesn't seem to be an image.</p>";
    } //!isImageFile($fileType)*/
    
    $fileExtension   = substr(strrchr($fileName, '.'), 0);
    $fileExtension   = strtolower($fileExtension);
    $replacedName    = str_replace(" ", "-", $productNM);
    $newFileName     = "$replacedName-" . time() . "$fileExtension";
    $uploadDirectory = "../imgProducts/img-fs/$newFileName";
    
    // US //
    if ($productST == "US")
    {
      $uploadDirectory = "../../first-choice-pharmacy.com/imgProducts/img-fs/$newFileName";
      //move_uploaded_file($fileTemp, $uploadDirectory2nd);
      
      if (move_uploaded_file($fileTemp, $uploadDirectory)) {
          $productWM = watermark_image($uploadDirectory, $newFileName, 1);
          $productTH = resize_image($uploadDirectory, $newFileName, 1);
      } //move_uploaded_file($fileTemp, $uploadDirectory)
      
      /*if (isset($generate2ndImage)) {
          watermark_image($uploadDirectory2nd, $fileTemp2nd);
          move_uploaded_file($fileTemp2nd, $uploadDirectory2nd);
      } //isset($generate2ndImage)*/
      
      $qI = mysql_query("INSERT INTO `fcpv3_products` (`id`,`category_id`,`product_name`,`product_description`,`product_thumbnail`,`product_fullsize`,`product_fullsize_second`,`product_price`,`product_weight`,`p_med`,`is_breakable`,`site`,`date_added`) VALUES ('','$productCT','$productNM','$productDS','$productTH','$newFileName','$newFileName2nd','$productPR','0.00','$productPM','N','US',NOW())");
      
      if ($_POST['updateTwitter'] == "yes") {
          $last_product_id = mysql_insert_id();
          $twitter_url     = generate_seo_friendly_links($productNM, $last_product_id);
          $username        = 'firstchoicephar';
          $password        = '';
          $status          = urlencode(stripslashes(urldecode('We just added ' . $productNM . ' http://www.firstchoicepharmacy.co.uk/product.php?id=' . mysql_insert_id())));
          if ($status) {
              $tweetUrl = 'http://www.twitter.com/statuses/update.xml';
              $curl     = curl_init();
              curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
              curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
              curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
              $result      = curl_exec($curl);
              $resultArray = curl_getinfo($curl);
              if ($resultArray['http_code'] == 200) {
                  print "<p class=\"fcp-message-success\">Tweet made successfully .</p>";
              } //$resultArray['http_code'] == 200
              else {
                  print '<p class="fcp-message-error">OOPS, an error has occured Posting to twitter, please contact the site administrator.</p>';
                  curl_close($curl);
              }
          } //$status
          
          if ($qI) {
              print "<p class=\"fcp-message-success\">You have successfully added a new product.</p>";
          } //$qI
          else {
              print '<p class="fcp-message-error">OOPS, an error has occured please contact the site administrator.</p>';
          }
      } //$_POST['updateTwitter'] == "yes"
      
    } elseif($productST == "UK") {
       
          if (move_uploaded_file($fileTemp, $uploadDirectory)) {
              $productWM = watermark_image($uploadDirectory, $newFileName);
              $productTH = resize_image($uploadDirectory, $newFileName);
          } //move_uploaded_file($fileTemp, $uploadDirectory)
          
          /*if (isset($generate2ndImage)) {
              watermark_image($uploadDirectory2nd, $fileTemp2nd);
              move_uploaded_file($fileTemp2nd, $uploadDirectory2nd);
          } //isset($generate2ndImage)*/
          
          $qI = mysql_query("INSERT INTO `fcpv3_products` (`id`,`category_id`,`product_name`,`product_description`,`product_thumbnail`,`product_fullsize`,`product_fullsize_second`,`product_price`,`product_weight`,`p_med`,`is_breakable`,`site`,`date_added`) VALUES ('','$productCT','$productNM','$productDS','$productTH','$newFileName','$newFileName2nd','$productPR','0.00','$productPM','N','UK',NOW())");
          
          if ($_POST['updateTwitter'] == "yes") {
              $last_product_id = mysql_insert_id();
              $twitter_url     = generate_seo_friendly_links($productNM, $last_product_id);
              $username        = 'firstchoicephar';
              $password        = 'milr';
              $status          = urlencode(stripslashes(urldecode('We just added ' . $productNM . ' http://www.firstchoicepharmacy.co.uk/product.php?id=' . mysql_insert_id())));
              if ($status) {
                  $tweetUrl = 'http://www.twitter.com/statuses/update.xml';
                  $curl     = curl_init();
                  curl_setopt($curl, CURLOPT_URL, "$tweetUrl");
                  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl, CURLOPT_POST, 1);
                  curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
                  curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
                  $result      = curl_exec($curl);
                  $resultArray = curl_getinfo($curl);
                  if ($resultArray['http_code'] == 200) {
                      print "<p class=\"fcp-message-success\">Tweet made successfully .</p>";
                  } //$resultArray['http_code'] == 200
                  else {
                      print '<p class="fcp-message-error">OOPS, an error has occured Posting to twitter, please contact the site administrator.</p>';
                      curl_close($curl);
                  }
              } //$status
              
              if ($qI) {
                  print "<p class=\"fcp-message-success\">You have successfully added a new product.</p>";
              } //$qI
              else {
                  print '<p class="fcp-message-error">OOPS, an error has occured please contact the site administrator.</p>';
              }
          } //$_POST['updateTwitter'] == "yes"
       
    }

} //isset($_POST['submit-add-product'])
?>

 

When i change the $us flag the US site works and the UK one doesn't, the full size image always gets uploaded fine it appears to be my logic in the resize function (i think :))

 

a fresh pair of eyes would be great

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/213796-upload-logic/
Share on other sites

didn't read thru all your code; however, it would seem that a simple IF statement would solve the problem.

 

You would need to make the destination folders variable names, then ...

 

if location = US

image dir = x

thumb dir = x1

else

image dir = y

thumb dir = y1

endif

 

 

OR

You could adjust your database table to have a field that says which location, and simply filter your queries to the appropriate location.

 

ie WHERE location = "US"

 

all images could be in one folder with UNIQUE names

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/213796-upload-logic/#findComment-1112774
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.