userb Posted August 23, 2011 Posted August 23, 2011 (edited) Hi Guys, How to change the Ajax upload code to change image name when uploading from original name to model name eg.: Product XXX model A112 image name w1.jpg w2.jpg w3.jpg After uplad we should get: A112.jpg original was w1.jpg A112-1.jpg original was w2.jpg A112-2.jpg original was w3.jpg Here is the code: <?php function uploadImage($fileName, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH = null){ $folder = $relPath; $maxlimit = $maxSize; $allowed_ext = "jpg,jpeg,gif,png,bmp"; $match = ""; $filesize = $_FILES[$fileName]['size']; if($filesize > 0){ $filename = strtolower($_FILES[$fileName]['name']); $filename = preg_replace('/\s/', '_', $filename); if($filesize < 1){ $errorList[] = "File size is empty."; } if($filesize > $maxlimit){ $errorList[] = "File size is too big."; } if(count($errorList)<1){ $file_ext = preg_split("/\./",$filename); $allowed_ext = preg_split("/\,/",$allowed_ext); foreach($allowed_ext as $ext){ if($ext==end($file_ext)){ $match = "1"; // File is allowed $NUM = time(); $front_name = substr($file_ext[0], 0, 15); $newfilename = $front_name.".".end($file_ext); $front_name = $_FILES[$fileName]['name']; $newfilename = $front_name; $filetype = end($file_ext); $save = $folder.$newfilename; #var_dump($save); #var_dump($_FILES); #print "</pre><br /><br />";exit; move_uploaded_file( $_FILES[$fileName]['tmp_name'], $save); } } } }else{ $errorList[]= "NO FILE SELECTED"; } if(!$match){ $errorList[]= "File type isn't allowed: $filename"; } if(sizeof($errorList) == 0){ return $fullPath.$newfilename; }else{ $eMessage = array(); for ($x=0; $x<sizeof($errorList); $x++){ $eMessage[] = $errorList[$x]; } return $eMessage; } } $filename = strip_tags($_REQUEST['filename']); $maxSize = strip_tags($_REQUEST['maxSize']); $maxW = strip_tags($_REQUEST['maxW']); $fullPath = strip_tags($_REQUEST['fullPath']); $relPath = strip_tags($_REQUEST['relPath']); $colorR = strip_tags($_REQUEST['colorR']); $colorG = strip_tags($_REQUEST['colorG']); $colorB = strip_tags($_REQUEST['colorB']); $maxH = strip_tags($_REQUEST['maxH']); $filesize_image = $_FILES[$filename]['size']; $new_picture=false; if($_FILES['new_picture']['name']!='') $new_picture=true; /* print "<pre>"; var_dump($_POST); var_dump($filename); var_dump($_FILES); print "</pre><br /><br />"; exit; */ if($filesize_image > 0){ $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH); if(is_array($upload_image)){ foreach($upload_image as $key => $value) { if($value == "-ERROR-") { unset($upload_image[$key]); } } $document = array_values($upload_image); for ($x=0; $x<sizeof($document); $x++){ $errorList[] = $document[$x]; } $imgUploaded = false; }else{ $imgUploaded = true; } }else{ $imgUploaded = false; $errorList[] = "File Size Empty: ".$filename; } ?> <?php if($imgUploaded) { ?> <table style="margin-bottom: 10px;" width="100%"> <?php require_once('newfileupload.inc.php'); if($filename=='new_picture') { $sql="insert into `products_pictures` set `products_id`='".$_POST['pID']."', `products_pictures_image`='".$_FILES[$filename]['name']."', `products_pictures_order`='0', `products_pictures_size`='".$filesize_image."';"; mysql_query($sql); }//new_picture else { $sql="update `products_pictures` set `products_id`='".$_POST['pID']."', `products_pictures_image`='".$_FILES[$filename]['name']."', `products_pictures_order`='0', `products_pictures_size`='".$filesize_image."' where `products_pictures_id` = '".$_POST['products_pictures_id']."' ;"; mysql_query($sql); if($_POST['pNR']==1) mysql_query($sql="update `products` set `products_image`='".$_FILES[$filename]['name']."' where `products_id`='".$_POST['pID']."'"); }//else - podmiana /* print "<pre>"; var_dump($filename); var_dump($_POST['pNR']); // var_dump($_FILES); print "</pre>"; */ }else{ echo '<img src="images/error.gif" width="16" height="16px" border="0" style="marin-bottom: -3px;" /> Error(s) Found: '; foreach($errorList as $value){ echo $value.', '; } } ?> Any ideas? Thanks! This helps in orginal upload: open admin/includes/classes/upload.php find parse() function replace: $this->set_filename($file['name']); with: if (isset($this->filename)) $this->set_filename($this->filename.strstr($file['name'], '.')); else $this->set_filename($file['name']); AND open admin/categories.php find in line 442 (but i have installed several contributions) case 'new_product_preview': // copy image only if modified $products_image = new upload('products_image'); and insert: $products_image->filename = $HTTP_POST_VARS['products_model']; But i cannot apply it to Ajax. Edited August 23, 2011 by userb Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.