how to use your own upload.php script with uploadify

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dana Schuett
    New Member
    • Oct 2010
    • 1

    how to use your own upload.php script with uploadify

    I was just wondering if you ever figured out how to use your own upload.php script with uploadify? If so, could you explain how you got it to work? here is the script I would like to use with uploadify:

    Code:
    <?php
      if (isset($_POST['upload'])) {
      $con = mysql_connect("localhost", "sbslogin", "sbsdatab@53!") or die("cannot connect");
      mysql_select_db("sbsdat", $con) or die("cannot select DB");
          $img = $_FILES["image"]["name"];
          foreach ($img as $key => $value) {
              $name = $_FILES["image"]["name"][$key];
              $tname = $_FILES["image"]["tmp_name"][$key];
              $size = $_FILES["image"]["size"][$key];
              $oext = getExtention($name);
              $ext = strtolower($oext);
              $base_name = "uploads/".getBaseName($name).".$ext";
    	   $whois = $_SERVER['REMOTE_ADDR'];
              if ($ext == "jpg" || $ext == "jpeg" || $ext == "bmp" || $ext == "gif") {
                  if ($size < 1024 * 1024) {
                      if (file_exists("uploads/" . $name)) {
                          move_uploaded_file($tname, "uploads/" . $name);
                          $result = 1;
                          //list($width, $height) = getimagesize("uploads/" . $name);
                          $qry = "select id from img where img_base_name='$base_name' and img_ext='$ext'";
                          $res = mysql_fetch_array(mysql_query($qry));
                          $id = $res['id'];
                          $qry = "UPDATE pictures SET path='$base_name', type='$ext', size='$size', date=NOW() where id=$id";
                          mysql_query($qry);
                          ?><div style="float:right; text-align:left; width:400px;"><?php echo "Image '$name' <font color='blue'>updated</font><br />";
                      } else {
                          move_uploaded_file($tname, "uploads/" . $name);
                          $result = 1;
                          //list($width, $height) = getimagesize("uploads/" . $name);
                          $qry = "INSERT INTO pictures(id, path, type, size, email, whois, date) VALUES ('', '$base_name', '$ext', '$size', 'd.schuett@gmail.com', '$whois', NOW())";
                          mysql_query($qry, $con);
                          //Error Checking off by default. Uncomment if you run into problems.
                          //mysql_error();
                          //print_r($qry);
                          ?><div style="float:right; text-align:left; width:400px;"><?php echo "Image '$name' <font color='green'>uploaded</font><br />";
                      }
                  } else { 
                      ?><div style="float:right; text-align:left width:400px;"><?php echo "<font color='red'><B>Image size excedded.<br />File size should be less than 1Mb</B></font><br />";
                  }
              } else {
                  ?><div style="float:right; text-align:left; width:400px;"><?php  echo "<font color='red'><B>Invalid file extention '.$oext'</B></font><br />";
              }
          }
      }
      function getExtention($image_name)
      {
          return substr($image_name, strrpos($image_name, '.') + 1);
      }
      function getBaseName($image_name)
      {
          return substr($image_name, 0, strrpos($image_name, '.'));
      }
    ?>
    Last edited by Niheel; Oct 10 '10, 04:29 AM.
  • oranoos3000
    New Member
    • Jan 2009
    • 107

    #2
    hi
    you must add a form to the page and sumbit attritue of this form you must be add address of this file ,
    $_FILES[NAME] , NAME is the name attribute of the file element in this form ,
    in this script extension file is check, size of the file is checked ,... and otherwise any disobey this conditional file is uploaded and after upload path of the file is saved on the database

    for more details , study chapter file upload in php
    thanks alot

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      hi dana,

      I intergrated my own upload script with the one that came with uploadify... it was the easiest way and because uploadify looks for certain vars it was a pain to use my complete own so i had to mix the two as simple as possible as well due to the fact that uploadify kept on messing up on me... below is the code i used, it's not the answer you are looking for as I wrote this for a social networking script... there really isn't much to explain apart from i upload to a temp folder change the image sizes then rename and move the images to the correct folder, the majority of it is still uploadify's standard script

      Code:
      $userPhoto=mkdir('user/'.$userName.'/photos/'.$uploadID.'/', 0777);
      $userTemp=mkdir('user/'.$userName.'/photos/'.$uploadID.'/tmp/', 0777); 
      $userThumb=mkdir('user/'.$userName.'/photos/'.$uploadID.'/tmp/t/', 0777); 
      $userMain=mkdir('user/'.$userName.'/photos/'.$uploadID.'/tmp/l/', 0777); 
      //	specify file parameter name
      $file_param_name = 'file';
      //	retrieve uploaded file name
      $file_name = $_FILES[ $file_param_name ][ 'name' ];
      //	retrieve uploaded file path (temporary stored by php engine)
      $source_file_path = $_FILES[ $file_param_name ][ 'tmp_name' ];
      //	construct target file path (desired location of uploaded file) -
      //	here we put to the web server document root (i.e. '/home/wwwroot')
      //	using user supplied file name
      if (isset($userName)){
      	$target_file_path = "user/".$userName ."/photos/". $uploadID . "/tmp/l/" . $file_name;
      } else {
      	$target_file_path = "user/" . $file_name;
      }
      //	move uploaded file
      if( move_uploaded_file( $source_file_path, $target_file_path ) ) {	
      $pathA = "user/".$userName ."/photos/". $uploadID . "/tmp/l/";
      $pathB = "user/".$userName ."/photos/". $uploadID . "/tmp/t/";
      if ($handle = opendir($pathA)) {
         	while (false !== ($file = readdir($handle))) {
          	if ($file != "." && $file != "..") {
              	if(!is_dir($file)){
      				$source = $pathA . $file;
      				$destination = $pathB . $file;
                      copy($source, $destination);		
       $details = array();
       $details = getimagesize($source);
       $detailsWidth = $details[0];
       $detailsHeight = $details[1];
                   if ($detailsWidth > $detailsHeight){
                   $image = new SimpleImage();
                   $image->load("$source");
                   $image->resize(450,350);
                   $image->save("$source");
        
                   $image = new SimpleImage();
                   $image->load("$destination");
                   $image->resize(100,70);
                   $image->save("$destination"); 
                   } 
                   elseif ($detailsHeight > $detailsWidth) {
                   $image = new SimpleImage();
                   $image->load("$source");
                   $image->resize(240,350);
                   $image->save("$source"); 
      
                   $image = new SimpleImage();
                   $image->load("$destination");
                   $image->resize(70,100);
                   $image->save("$destination"); 
                   }
          		}
          	}
      		
         	} closedir($handle);   	
      }
      }
      //variable used for the name of each file
      $rnA = "user/".$userName ."/photos/". $uploadID . "/tmp/l/";
      $rnB = "user/".$userName ."/photos/". $uploadID . "/";
      $od = opendir($rnA);
      while (false !== ($filename = readdir($od)))
      {
      	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
      	if($filename != '.' && $filename != '..' && !is_dir($rnA.$filename))
      	{
      		//we give files a name - here we use increasing numbers for jpg files:   '_'.$i++.'
      		if(rename($rnA.$filename, $rnB.$uploadID.zonedate('dmYH-i-s',true).'.jpg'))
      		echo '<br />';
      	}
      }
      closedir($od);
      $rnC = "user/".$userName ."/photos/". $uploadID . "/tmp/t/";
      $rnD = "user/".$userName ."/photos/". $uploadID . "/";
      $odT = opendir($rnC);
      while (false !== ($filename = readdir($odT)))
      {
      	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
      	if($filename != '.' && $filename != '..' && !is_dir($rnC.$filename))
      	{
      		//we give files a name - here we use increasing numbers for jpg files:   '_'.$i++.'
      		if(rename($rnC.$filename, $rnD.$uploadID.zonedate('dmYH-i-s',true).'_'.'b.jpg'))
      		echo '<br />';
      	}
      }
      closedir($odT);

      Comment

      Working...