Upload Image does not work correctly :(
09-18-2010, 08:12 AM
|
Upload Image does not work correctly :(
|
Posts: 32
Name: Kleidi
|
Hello to everyone!
Since i have get many help from users on this forum i'm trying to get help this time to and hopping that someone can help me...again...again. I'm still learning, reading, testing but, since i'll be good to help myself i'm asking for help
This time i have a problem with a form that have included a upload field that uploads image in server and the path is inserted in the database. The upload and path insert is made by a uploader class found somewhere on web. Since here, everything is working great: the image is uploaded in server and the image path is inserted on db. But, the problem is on edit page of the same form. When i try to edit a row added by that form, i'm having problem with image path that is updated(blank) when i save changes. I want to have a way that offers me to not update the image path on the db if no new image is browsed; and, if a new image is browsed, to update that path.
For references i'm posting the add.php page, edit.php page and the uploader class.
add.php
PHP Code:
<? include('header.php'); include ('includes/uploader.class.php'); $uploader = new uploader(); // Setting properties then Uploading the image $uploader->destDir = "D:/Program Files/VertrigoServ/www/oldi/imazhet/artikuj/"; $uploader->upload($_FILES['LajmeImg']); include('lart.php'); // Remember any variables incase the form validation finds errors. $LajmeEmri = $_POST['LajmeEmri']; $KatLajmeID = $_POST['KatLajmeID']; // Kategoria Lajmeve $LajmeIntro = $_POST['LajmeIntro']; $LajmeTxt = $_POST['LajmeTxt']; $LajmeAktiv = $_POST['LajmeAktiv']; $LajmeTag = $_POST['LajmeTag']; $LajmeVideo = $_POST['LajmeVideo']; //$LajmeImg = $_FILES['LajmeImg']['name']; // Me autoName = False; regjistron ne db emrin real te fotografise $LajmeImg = $uploader->source['name']; // Me autoName = true; regjistron ne db emrin random te fotografise i gjeneruar ne base64 $LajmeLink = preg_replace("#[^a-z0-9\-_]#i", "",str_replace(' ', '_', $LajmeEmri)); // Ben Url automatikisht nga titulli (LajmeEmri) i lajmit $LajmeAutori = $_SESSION['username']; // Merr emrin e autorit te lajmit dhe e ve ne variabel qe te shtohet me pas ne db if(isset($_POST['add_lajme'])) {
$LajmeEmri = $_POST['LajmeEmri']; $KatLajmeID = $_POST['KatLajmeID']; // Kategoria Lajmeve $LajmeIntro = $_POST['LajmeIntro']; $LajmeTxt = $_POST['LajmeTxt']; $LajmeAktiv = $_POST['LajmeAktiv']; $LajmeTag = $_POST['LajmeTag']; $LajmeVideo = $_POST['LajmeVideo']; //$LajmeImg = $_FILES['LajmeImg']['name']; // Me autoName = False; regjistron ne db emrin real te fotografise $LajmeImg = $uploader->source['name']; // Me autoName = true; regjistron ne db emrin random te fotografise i gjeneruar ne base64 $LajmeLink = preg_replace("#[^a-z0-9\-_]#i", "",str_replace(' ', '_', $LajmeEmri)); // Ben Url automatikisht nga titulli (LajmeEmri) i lajmit $LajmeAutori = $_SESSION['username']; // Merr emrin e autorit te lajmit dhe e ve ne variabel qe te shtohet me pas ne db
if(trim($KatLajmeID) == '0') { $error = '<div class="error_message">Attention! Nuk ke zgjedhur kategorine.</div>'; } else if(trim($LajmeIntro) == '') { $error = '<div class="error_message">Attention! Nuk ke vene Intro.</div>'; } else if(trim($LajmeEmri) == '') { $error = '<div class="error_message">Attention! Nuk ke vene emrin.</div>'; } $count = mysql_num_rows(mysql_query("SELECT * FROM lajme WHERE LajmeLink='".$LajmeLink."'")); if($count > 0) { $error = '<div class="error_message">Ky emer/url gjendet ne database.</div>'; } if($error == '') {
$sql = "INSERT INTO lajme (KatLajmeID, LajmeEmri, LajmeLink, LajmeIntro, LajmeTxt, LajmeAktiv, LajmeTag, LajmeVideo, LajmeImg, LajmeData, LajmeAutori) VALUES ('$KatLajmeID', '$LajmeEmri', '$LajmeLink', '$LajmeIntro', '$LajmeTxt', '$LajmeAktiv', '$LajmeTag', '$LajmeVideo', '$LajmeImg', NOW(), '$LajmeAutori')"; $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
echo "<h2>Success!</h2>"; echo "<div class='success_message'>U Shtua me sukses lajmi <b>$LajmeEmri</b> ne kategorine <b>$KatLajmeID</b> .</div>"; echo "<h2>Te Dhenat e Lajmit</h2>"; echo "<ul class='success-reg'>"; echo "<li><span class='success-info'><b>Titulli</b></span>$LajmeEmri</li>"; echo "<li><span class='success-info'><b>Url</b></span>$LajmeLink</li>"; echo "<li><span class='success-info'><b>Kategoria</b></span>$KatLajmeID</li>"; echo "<li><span class='success-info'><b>Aktive</b></span>$LajmeAktiv</li>"; echo "</ul>"; echo "<h2>Cfare deshiron te besh tani?</h2><br />"; echo "Shko tek <a href='lajme_edit.php'>modifikimi i lajmeve</a>.</li>"; }
}
if(!isset($_POST['add_lajme']) || $error != '') {
echo $error;
?>
<h2>Shto Lajme</h2> <form action="" method="post" enctype="multipart/form-data"> <hr /><label><b>Kategoria</b></label> <select id="KatLajmeID" name="KatLajmeID" size="1"> <?php $query="SELECT * FROM lajme_kategori "; $result=mysql_query($query); $num = mysql_num_rows ($result); mysql_close(); ?> <option selected value="0">Perzgjidh Kategorine</option> <? if ($num > 0 ) { $i=0; while ($i < $num) { $LKatID = mysql_result($result,$i,"LKatID"); $LKatEmri = mysql_result($result,$i,"LKatEmri"); $LKatLink = mysql_result($result,$i,"LKatLink"); $LKatAktiv= mysql_result($result,$i,"LKatAktiv"); ?> <option value="<?php echo $LKatID ?>"><?php echo $LKatEmri ?></option> <?php ++$i; } } ?> </select>
<script type="text/javascript"> function toggle_LajmeLink(LajmeID) { if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } handle = document.getElementById(LajmeID); var url = '../ajax.php?'; if(handle.value.length > 0) { var fullurl = url + 'do=check_LajmeEmri_exists&LajmeEmri=' + encodeURIComponent(handle.value); http.open("GET", fullurl, true); http.send(null); http.onreadystatechange = statechange_LajmeEmri; }else{ document.getElementById('LajmeEmri').className = ''; } }
function statechange_LajmeEmri() { if (http.readyState == 4) { var xmlObj = http.responseXML; var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; document.getElementById('LajmeEmri').className = html; } } </script> <br />
<hr /><label><b>Titulli</b></label><input type="text" name="LajmeEmri" value="<?=$LajmeEmri;?>" onchange="toggle_LajmeEmri('LajmeEmri')" class="lf" /><br />
<hr /><label><b>Intro</b></label><textarea id="LajmeIntro" name="LajmeIntro" value="<?=$LajmeIntro;?>"></textarea> <script type="text/javascript"> CKEDITOR.replace( 'LajmeIntro', { filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html', filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images', filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash', filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files', filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images', filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash' }); </script><br />
<hr /><label><b>Lajmi</b></label><textarea id="LajmeTxt" name="LajmeTxt" value="<?=$LajmeTxt;?>"></textarea> <script type="text/javascript"> CKEDITOR.replace( 'LajmeTxt', { filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html', filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images', filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash', filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files', filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images', filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash' }); </script><br />
<hr /><label><b>Fotografia</b></label> <input type="file" size="60" value="<?=$LajmeImg;?>" id="LajmeImg" name="LajmeImg" /><br /> <hr /><label><b>Video</b></label><textarea id="LajmeVideo" name="LajmeVideo" value="<?=$LajmeVideo;?>" rows="4" wrap="soft" style="width:100%;" /></textarea><br /> <hr /><label><b>Tag</b></label><input id="LajmeTag" type="text" name="LajmeTag" value="<?=$LajmeTag;?>" class="lf" /><br /> <hr /><label><b>Aktive</b></label><input id="LajmeAktiv" name="LajmeAktiv" type="checkbox" value="PO"><br /> <hr /><input type="submit" value="Vazhdo" name="add_lajme" class="button" />
</form>
<? }
include('poshte.php');?>
edit.php
PHP Code:
<?php include('header.php'); include ('includes/uploader.class.php'); $uploader = new uploader(); // Setting properties then Uploading the image $uploader->destDir = "D:/Program Files/VertrigoServ/www/oldi/imazhet/artikuj/"; $uploader->upload($_FILES['LajmeImg']); include('lart.php'); if(!$_GET['lajmeid'] && !isset($_POST['do_edit']) && !isset($_POST['edit_lajme'])) {
echo $error;
// Shfaq listen e lajmeve lajmet_lista();
}
// Has the edit form been submitted?
if(isset($_POST['do_edit'])) { $LajmeID = $_POST['LajmeID']; $LajmeEmri = $_POST['LajmeEmri']; $KatLajmeID = $_POST['KatLajmeID']; // Kategoria Lajmeve $LajmeIntro = $_POST['LajmeIntro']; $LajmeTxt = $_POST['LajmeTxt']; $LajmeAktiv = $_POST['LajmeAktiv']; $LajmeTag = $_POST['LajmeTag']; $LajmeVideo = $_POST['LajmeVideo']; $LajmeLink = $_POST['LajmeLink']; //$LajmeImg = $_FILES['LajmeImg']['name']; // Me autoName = False; regjistron ne db emrin real te fotografise $LajmeImg = $uploader->source['name']; // Me autoName = true; regjistron ne db emrin random te fotografise i gjeneruar ne base64 $LajmeImg = $_POST['LajmeImg']; $fshij = $_POST['fshij'];
// Ticked the 'delete category' box? If so, delete and echo message. if($fshij == 'fshij_LajmeID' && $error == '') { $sql = "DELETE FROM lajme WHERE LajmeID=$LajmeID LIMIT 1"; $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
echo "<h3>U Fshi</h3>"; echo "<div class='success_message'>Lajme <b>$LajmeID</b> u fshi nga sistemi.</div>"; echo "<h2>What to do now?</h2><br />"; echo "Go to the <a href='lajme_edit.php'>edit lajme</a> page.</li>"; } else { // Validate the submitted information if(trim($LajmeID) == '0') { $error = '<div class="error_message">LajmeID - Attention! You cannot edit the main Administrator, use database.</div>'; } else if(trim($LajmeEmri) == '') { $error = '<div class="error_message">LajmeEmri - Attention! You must enter a first name.</div>'; } else if(trim($LajmeLink) == '') { $error = '<div class="error_message">LajmeUrl - Attention! You must enter a last name.</div>'; } // Password been entered? If so, validate and update information. if(trim($KatLajmeID) == '') { $error = '<div class="error_message">Attention! Nuk ke zgjedhur kategorine.</div>'; } else if(trim($LajmeIntro) == '') { $error = '<div class="error_message">Attention! Nuk ke vene Intro.</div>'; } else if(trim($LajmeEmri) == '') { $error = '<div class="error_message">Attention! Nuk ke vene emrin.</div>'; } if($error == '') { $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'"; $query = mysql_query($sql) or die("Fatal error: ".mysql_error()); // $sql2 = "SELECT * FROM lajme_kategori WHERE LKatAktiv = PO"; //$sql2 = "SELECT B.LKatEmri, A.LajmeID AS LajmeID, A.LajmeEmri AS LajmeEmri, A.LajmeLink AS LajmeLink, A.LajmeIntro AS LajmeIntro, A.LajmeTxt AS LajmeTxt, A.LajmeTag AS LajmeTag, A.LajmeVideo AS LajmeVideo, A.LajmeAktiv AS LajmeAktiv, B.LKatAktiv = LKatAktiv FROM lajme A LEFT JOIN lajme_kategori B ON A.KatLajmeID = B.LKatID WHERE B.LKatID = KatLajmeID AND LKatAktiv = PO AND LajmeAktiv = PO "; //$sql2 = "SELECT * FROM lajme_Kategori WHERE LKatID = '$KatLajmeID'"; //$result2 = mysql_query($sql2); echo "<h2>U Modifikua</h2>"; echo "<div class='success_message'>Lajmi u modifikua: <a href='lajme_edit.php?lajmeid=$LajmeID'><b>$LajmeEmri</b></a> me ID <b>$LajmeID </b>.</div>"; echo "<h2>C'deshironi te beni tani?</h2><br />"; echo "Vizitoni <a href='lajme_edit.php'>listen e lajmeve</a>.</li>"; } } }
// Has a category been selected to edit?
if($_GET['lajmeid'] && !isset($_POST['do_edit']) && !isset($_POST['edit_lajme']) || $error != '') {
$LajmeID = $_GET['lajmeid']; $sql = "SELECT B.LKatEmri, A.LajmeID AS LajmeID, A.LajmeEmri AS LajmeEmri, A.LajmeLink AS LajmeLink, A.LajmeIntro AS LajmeIntro, A.LajmeTxt AS LajmeTxt, A.LajmeTag AS LajmeTag, A.LajmeImg AS LajmeImg, A.LajmeVideo AS LajmeVideo, A.LajmeAktiv AS LajmeAktiv, A.KatLajmeID AS KatLajmeID FROM lajme A LEFT JOIN lajme_kategori B ON A.KatLajmeID = B.LKatID WHERE B.LKatID = KatLajmeID AND A.LajmeID='$LajmeID'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $LajmeEmri = $row['LajmeEmri']; $LajmeLink = $row['LajmeLink']; $LajmeIntro = $row['LajmeIntro']; $LajmeTxt = $row['LajmeTxt']; $LajmeVideo = $row['LajmeVideo']; $LajmeTag = $row['LajmeTag']; $LajmeImg = $row['LajmeImg']; $LajmeAktiv = $row['LajmeAktiv']; $KatLajmeID = $row['KatLajmeID']; $LKatEmri = $row['LKatEmri']; echo $error; echo "<h2>Informacionet per lajmin ( ".stripslashes($row['LajmeEmri'])." )</h2>"; ?>
<form action="" method="post" enctype="multipart/form-data" /> <input type="hidden" name="LajmeID" value="<?=$row['LajmeID'];?>" />
<hr /><label>Kategoria</label> <select name="KatLajmeID"> <option selected value="<?=stripslashes($row['KatLajmeID']);?>"><?=$LKatEmri ?></option> <?php // Marrja e te dhenave ekzistente ?> <?php $sql2 = "SELECT * FROM lajme_kategori WHERE LKatAktiv = 'PO'"; // Query per te marre kategorite nga database $result2 = mysql_query($sql2); ?> <? while($kategoria = mysql_fetch_array($result2)) { echo '<option value="'.stripslashes($kategoria['LKatID']).'">'.stripslashes($kategoria['LKatEmri']).'</option>'; // Lista e kategorive } ?> </select>
<hr /><label>Titulli</label> <input type="text" name="LajmeEmri" value="<?=stripslashes($row['LajmeEmri']);?>" class="lf" /><br />
<hr /><label>Linku</label> <input type="text" name="LajmeLink" value="<?=stripslashes($row['LajmeLink']);?>" class="lf" /><br />
<hr /><label>Intro</label><textarea id="LajmeIntro" name="LajmeIntro" /><?=stripslashes($row['LajmeIntro']);?></textarea> <script type="text/javascript"> CKEDITOR.replace( 'LajmeIntro', { filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html', filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images', filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash', filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files', filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images', filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash', }); </script><br />
<hr /><label>Lajmi</label> <textarea id="LajmeTxt" name="LajmeTxt" /><?=stripslashes($row['LajmeTxt']);?></textarea> <script type="text/javascript"> CKEDITOR.replace( 'LajmeTxt', { filebrowserBrowseUrl : 'includes/ckfinder/ckfinder.html', filebrowserImageBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Images', filebrowserFlashBrowseUrl : 'includes/ckfinder/ckfinder.html?Type=Flash', filebrowserUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files', filebrowserImageUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images', filebrowserFlashUploadUrl : 'includes/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash' }); </script><br />
<hr /><label>Video</label> <textarea id="LajmeVideo" name="LajmeVideo" rows="6" wrap="soft" style="width:100%;" /><?=stripslashes($row['LajmeVideo']);?></textarea><br />
<hr />Fotografia: <img src="../imazhet/artikuj/<?=stripslashes($row['LajmeImg']);?>" width="150" align="middle" /><br /><br /> <label>Fotografia</label> <input type="file" size="60" name="LajmeImg" id="LajmeImg" class="lf" /><br />
<hr /><label>Tag</label> <input type="text" name="LajmeTag" value="<?=stripslashes($row['LajmeTag']);?>" class="lf" /><br />
<hr /><label style="width: 50%;">Aktive</label> <input id="LajmeAktiv" name="LajmeAktiv" type="checkbox" <?php if ($LajmeAktiv == "PO") { echo "checked";} ?> value="PO"> <br /><br /> <hr /><div class="error_message">Do fshish kete lajm? (Nuk kthehet mbrapsh!) <input type="checkbox" class="checkbox" name="fshij" value="fshij_LajmeID"></div> <hr /><input type="submit" value="Ruaj Ndryshimet" name="do_edit" class="button" /> </form>
<? } include('poshte.php'); ?>
uploader class
PHP Code:
<?php /** * Uploader is a file transfer class. it can upload files and images. * It also can resize and crop images. * Works on PHP 5 * @author Alaa Al-Hussein * @link http://www.freelancer-id.com/projects * @version 1.0 * */ class uploader{ /** * Array, The file object as $_FILES['element']. * String, file location. */ public $source; /** * Destination file location as folder. */ public $destDir; /** * Directory for Resized images. */ public $resizeDir; /** * Directory for Cropped images. */ public $cropDir; /** * stores information for uploading file */ private $info = ''; /** * Enabling autoName will generate an auto file name for the uploaded file. */ public $autoName = true; /** * Handles the error when it occurs. */ private $errorMsg = ''; /** * new width for resizing and cropping. */ public $newWidth; /** * new height for resizing and cropping. */ public $newHeight; /** * TOP postion to cropping image. */ public $top = 0; /** * LEFT position for cropping image. */ public $left = 0; /** * JPG quality (0 - 100). used for image resizing or cropping. */ public $quality = 60; public function __construct(){ //nothing } /** * Uploads the file to the server. * @param Array $_FILES[] */ public function upload($source){ if($source != ""){ $this->source = $source; } if(is_array($this->source)){ if($this->fileExists()){ return false; } return $this->copyFile(); } else { return $this->source; } } /** * return the error messages. * @return String messages. */ public function getError(){ return $this->errorMsg; } /** * Get uploading information. */ public function getInfo(){ return $this->info; } /** * Get file ex. */ function getEx(){ return strtolower(substr($this->source['name'], strrpos($this->source['name'], '.') + 1)); } /** * Get random name. */ function randName(){ return substr(base64_encode(uniqid()),0,10); } /** * Copy the uploaded file to destination. */ private function copyFile(){ if(!$this->isWritable()){ $this->errorMsg .= '<div>Error, the directory: ('.$this->destDir.') is not writable. Please fix the permission to be able to upload.</div>'; return false; } $this->source['name'] = ($this->autoName) ? $this->randName().'.'.$this->getEx(): $this->source['name'];
if(copy($this->source['tmp_name'],$this->destDir . $this->source['name'])){ // Done. $this->info .= '<div>file was uploaded successfully.</div>'; } else { $this->errorMsg .= '<div>Error, the file was not uploaded correctly because of an internal error. Please try again, if you see this message again, please contact web admin.</div>'; } } /** * Checks if the file was uploaded. * @return boolean */ private function uploaded(){ if($this->source['tmp_name']=="" || $this->source['error'] !=0){ $this->errorMsg .= '<div>Error, file was not uploaded to the server. Please try again.</div>'; return false; } else return true; } /** * Prepares the directory. */ private function preDir(){ if($this->destDir!="" && substr($this->destDir, -1,1) != "/"){ $this->destDir = $this->destDir . '/'; } if($this->resizeDir!="" && substr($this->resizeDir, -1,1) != "/"){ $this->destDir = $this->resizeDir . '/'; } if($this->cropDir!="" && substr($this->cropDir, -1,1) != "/"){ $this->destDir = $this->cropDir . '/'; } } /** * Check if the folder is writable or not. * @return boolean */ private function isWritable(){ $err = false; if(!is_writeable($this->destDir) && $this->destDir!=""){ $this->errorMsg .= '<div>Error, the directory ('.$this->destDir.') is not writeable. File could not be uploaded.</div>'; $err = true; } if(!is_writeable($this->resizeDir) && $this->resizeDir!=""){ $this->errorMsg .= '<div>Error, the directory ('.$this->resizeDir.') is not writeable. File could not be resized.</div>'; $err = true; } if(!is_writeable($this->cropDir) && $this->cropDir!=""){ $this->errorMsg .= '<div>Error, the directory ('.$this->cropDir.') is not writeable. File could not be cropped.</div>'; $err = true; } if($err == true){ return false; } else { return true; } } /** * Checks if the file exists on the server * @return boolean */ private function fileExists(){ $this->preDir(); if(file_exists($this->destDir.$this->source)){ $this->errorMsg .= '<div>Upload error because file already exists.</div>'; return true; } else { return false; } } /** /586742130./8532 Crops image. * @return String fileName or False on error */ public function crop($file='',$width='',$height='',$top='',$left=''){ if($file!=""){ $this->source = $file;} if ($width != '') $this->newWidth = $width; if ($height != '') $this->newHeight = $height; if ($top != '') $this->top = $top; if ($left != '') $this->left = $left; return $this->_resize_crop(true); } /** * Resizes an image. * @return String fileName or False on error */ public function resize($file='',$width='',$height=''){ if($file!=""){ $this->source = $file; } if($width != '') $this->newWidth = $width; if($height != '') $this->newHeight = $height; return $this->_resize_crop(false); } /** * Get the Temp file location for the file. * If the Source was a file location, it returns the same file location. * @return String Temp File Location */ private function getTemp(){ if(is_array($this->source)){ return $this->source['tmp_name']; } else { return $this->source; } } /** * Get the File location. * If the source was a file location, it returns the same file location. * @return String File Location */ private function getFile(){ if(is_array($this->source)){ return $this->source['name']; } else { return $this->source; } } /** * Resize or crop- the image. * @param boolean $crop * @return String fileName False on error */ private function _resize_crop ($crop) { $ext = explode(".",$this->getFile()); $ext = strtolower(end($ext)); list($width, $height) = getimagesize($this->getTemp()); if(!$crop){ $ratio = $width/$height; if ($this->newWidth/$this->newHeight > $ratio) { $this->newWidth = $this->newHeight*$ratio; } else { $this->newHeight = $this->newWidth/$ratio; } } $normal = imagecreatetruecolor($this->newWidth, $this->newHeight); if($ext == "jpg") { $src = imagecreatefromjpeg($this->getTemp()); } else if($ext == "gif") { $src = imagecreatefromgif ($this->getTemp()); } else if($ext == "png") { $src = imagecreatefrompng ($this->getTemp()); } if($crop){ $pre = 'part_'; if(imagecopy($normal, $src, 0, 0, $this->top, $this->left, $this->newWidth, $this->newHeight)){ $this->info .= '<div>image was cropped and saved.</div>'; } $dir = $this->cropDir; } else { $pre = 'thumb_'; if(imagecopyresampled($normal, $src, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $width, $height)){ $this->info .= '<div>image was resized and saved.</div>'; } $dir = $this->resizeDir; } if($ext == "jpg" || $ext == "jpeg") { imagejpeg($normal, $dir . $pre . $this->getFile(), $this->quality); } else if($ext == "gif") { imagegif ($normal, $dir . $pre . $this->getFile()); } else if($ext == "png") { imagepng ($normal, $dir . $pre . $this->getFile(),0); } imagedestroy($src); return $src; } } ?>
db field of image is: LajmeImg
Thank you in advance and hope that i'll get help on this issue too.
|
|
|
|
09-18-2010, 08:34 AM
|
Re: Upload Image does not work correctly :(
|
Posts: 366
Name: Steve
Location: Miami, FL, Earth
|
Your problem is in this line:
$sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'";
if LajmeImg is blank, this piece of the SQL string should be left out:
, LajmeImg = '$LajmeImg'
write a piece of conditional code to do that.
__________________
- Steve
President, Please login or register to view this content. Registration is FREE
|
|
|
|
09-18-2010, 08:41 AM
|
Re: Upload Image does not work correctly :(
|
Posts: 32
Name: Kleidi
|
Probably you mean something like this:
PHP Code:
if browse field is blank { use this string }
else { use this string }
|
|
|
|
09-18-2010, 07:41 PM
|
Re: Upload Image does not work correctly :(
|
Posts: 366
Name: Steve
Location: Miami, FL, Earth
|
Yeah, that will work.
__________________
- Steve
President, Please login or register to view this content. Registration is FREE
|
|
|
|
09-19-2010, 11:49 AM
|
Re: Upload Image does not work correctly :(
|
Posts: 32
Name: Kleidi
|
Quote:
Originally Posted by smoseley
Yeah, that will work.
|
If tried in this way but it's not working. The file is uploaded but the path is not updated on the database. Seems that variable $LajmeImg didn't get the information name from the uploader string.
PHP Code:
if(empty($_FILES['LajmeImg']) && ($error == '')) { $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'"; $query = mysql_query($sql) or die("Fatal error: ".mysql_error()); } else if (!empty($_FILES['LajmeImg']) && ($error == '')) { $sql = "UPDATE lajme SET KatLajmeID = '$KatLajmeID', LajmeEmri = '$LajmeEmri', LajmeLink = '$LajmeLink', LajmeIntro = '$LajmeIntro', LajmeTxt = '$LajmeTxt', LajmeVideo = '$LajmeVideo', LajmeTag = '$LajmeTag', LajmeImg = '$LajmeImg', LajmeAktiv = '$LajmeAktiv' WHERE LajmeID = '$LajmeID'"; $query = mysql_query($sql) or die("Fatal error: ".mysql_error());
Any suggestion please? Should be this issue a uploader class bug?
Thank you!
|
|
|
|
|
« Reply to Upload Image does not work correctly :(
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|