Jump to content

[SOLVED] Inserting output value from function in mySQL


Recommended Posts

Hola hola.

 

I'm having trouble with receiving the output value from the function upload($image). I want the image path and new image name to be INSERTED into mySQL. The code to upload and resize is not my own, but GNU.

 

function upload($image) {

$path_thumbs = "img/miniatura";
$path_big = "img/grande";
$img_thumb_width = 90; 
$extlimit = "no"; 
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

if (!is_writeable($path_thumbs)){
   die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
}
if (!is_writeable($path_big)){
    die ("Error: The directory <b>($path_big)</b> is NOT writable");
}

       $file_type = $_FILES['imagen']['type'];
       $file_name = $_FILES['imagen']['name'];
       $file_size = $_FILES['imagen']['size'];
       $file_tmp = $_FILES['imagen']['tmp_name'];

       //check if you have selected a file.
       if(!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
       }
       //check file extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit();
       }
       //get the file extension.
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
       //get the new width variable.
       $ThumbWidth = $img_thumb_width;
       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list width and height and keep height ratio.
           list($width, $height) = getimagesize($file_tmp);
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //save image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
        }

        //upload the big image
        move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

        //set output for mySQL
        $imagen = "$rand_name.$file_ext";

 

For helps sake I have included my INSERT statement below:

 

$col_img = upload($_POST['image']);
$colaborador=$_POST['colaborador'];
$insertSQL = sprintf("INSERT INTO colaboraciones (image, colaborador) VALUES ('$col_img', '$colaborador');");

mysql_select_db($database_connect, $connect);
$Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

 

The frustrating part is that I had this working once! Any and all help appreciated. Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.