class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
I did everything as you said but I get same result each time. The already uploaded images and recorded in database been erased if those input fields are empty in new upload process.
For example, I upload images for specific members on first and third input fields and successfully recorded in database (in DB columns pic1 and pic3 now got records and pic2 are empty) if I next time try to upload new image at field two, pic1 and pic3 will be erased and pic2 will be recorded for the same members.
As far as understand script send an empty value that overwrites existing records in database for pic1 and pic3. How can I escape this and keep existing DB record if first and third fields are empty?
This is the code that I creat by your instruction:
include_once '../classes/class.upload.php';
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
// we now remove invalid entries for non-uploaded images
foreach ($files as $k => $v) {
if ($v['error'] != 0) unset($files[$k]);
}
$uploaded = array();
foreach (array_keys($files) as $k) {
$handle = new upload($files[$k]);
$this_upload = array();
$time = time();
$dir = date("mY", $time);$slash="/";
$fb=date("dHis",$time);$fe=rand(0,999);
$fn =$fb."-".$fe;
$intpic = $fn.'.';
$handle->jpeg_quality = 70;
$handle->image_watermark = 'watermark.png';
$handle->image_watermark_position = 'B';
$handle->file_new_name_body = $intpic;
$handle->Process("uploads/$dir");
if ($handle->uploaded) {
if ($handle->processed) {
$this_upload['large'] = $handle->file_dst_name;
}
}
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 80;
$handle->file_new_name_body = $intpic;
$handle->Process("uploads/tmb/$dir");
if ($handle->processed) {
// store the small image filename
$this_upload['small'] = $handle->file_dst_name;
$handle->clean();
}
// add this set of pictures to the main array
$uploaded[] = $this_upload;
}
$msc0 = (($uploaded[0]['large'] !='')) ? $dir.'/' : '';
$msc1 = (($uploaded[1]['large'] !='')) ? $dir.'/' : '';
$msc2 = (($uploaded[2]['large'] !='')) ? $dir.'/' : '';
$mpic[1]=", pic1='".$msc0.$uploaded[0]['large']."'";
$mpic[2]=", pic2='".$msc1.$uploaded[1]['large']."'";
$mpic[3]=", pic3='".$msc2.$uploaded[2]['large']."'";
$last_id = $_SESSION['m'];
$usr = ((C_ID) || (!isset($username))) ? '' :
"username='".$username."',";
mysql_query("UPDATE ".C_MYSQL_MEMBERS." SET ".$usr."fname='".
$fname."',lname='".$lname."',birthday='".$year."-".
$month."-".$day."',gender='".$gender."', purposes='".
$purpose."',country='".$country."',email='".
$email."',url='".$url."', icq='".$icq."',aim='".
$aim."',phone='".$phone."',city='".$city."',marstat='".
$marstat."',child='".$child."',height='".
$height."',weight='".$weight."',hcolor='".
$hcolor."',ecolor='".$ecolor."',etnicity='".
$etnicity."',religion='".$religion."',smoke='".
$smoke."',drink='".$drink."',education='".
$education."',job='".$job."',hobby='".$hobby."',descr='".
$descr."',sgender='".$sgender."',setnicity='".
$setnicity."',sreligion='".$sreligion."',agef='".
$agef."',aget='".$aget."',heightf='".$heightf."',heightt='".
$heightt."',weightf='".$weightf."',weightt='".
$weightt."',horo='".horo($month, $day).
"',editdate=(now('')+INTERVAL 7 HOUR),ip='".ip2int(ip())
."',status='".$cst."' ".$mpic[1].$mpic[2].$mpic[3]
." WHERE id='".$last_id."'") or die(mysql_error());
if (array_key_exists(0, $uploaded))
$mpic[1]=", pic1='".$msc0.$uploaded[0]['large']."'";
if (array_key_exists(1, $uploaded))
$mpic[2]=", pic2='".$msc1.$uploaded[1]['large']."'";
if (array_key_exists(2, $uploaded))
$mpic[3]=", pic3='".$msc2.$uploaded[2]['large']."'";
So that you are updating ONLY the fields which have an image uploaded.
Note that you will also have to change this line
$uploaded[] = $this_upload;
into something like:
$n++;
$uploaded[$n] = $this_upload;
so that each picture from $_FILES has proper key.
Please contact me privately should you want more information. This is a basic PHP/MySQL problem, and has nothing to do with this forum.Reply
For example, I upload images for specific members on first and third input fields and successfully recorded in database (in DB columns pic1 and pic3 now got records and pic2 are empty) if I next time try to upload new image at field two, pic1 and pic3 will be erased and pic2 will be recorded for the same members.
As far as understand script send an empty value that overwrites existing records in database for pic1 and pic3. How can I escape this and keep existing DB record if first and third fields are empty?
This is the code that I creat by your instruction:
Even if some images are not uploaded, you are still updating the database! You always update the three image fields in your query.
This code below:
Should be something like:
So that you are updating ONLY the fields which have an image uploaded.
Note that you will also have to change this line
into something like:
so that each picture from $_FILES has proper key.
Please contact me privately should you want more information. This is a basic PHP/MySQL problem, and has nothing to do with this forum.