Uploaded files will unlink if I press submit button

HI I have added two addition upload buttons where zip files can be uploaded on my dashboard and made available for visitors to download,

they upload fine and are available no problem , but if I have to go back into the the backend page editor to change anything and press the submit button the files get unlinked and are no longer available in my downloads page.

this is code in the view addedit file

						<div class="control-group">
							<label class="control-label" for="cleo_mk3_presets">CLEO MK3 Presets</label>
							<div class="controls">
								<input class="input-xlarge" id="input_cleo_mk3_presets" value="<?php if(!empty($recorddata[0]->cleo_mk3_presets)){echo $recorddata[0]->cleo_mk3_presets;}?>" name="input_cleo_mk3_presets" type="hidden" >
								<input class="input-xlarge" id="cleo_mk3_presets " name="cleo_mk3_presets" type="file">
								<?php if(!empty($recorddata[0]->cleo_mk3_presets)){
									if(file_exists(DOC_ROOT_FRONT."/software/presets/".$recorddata[0]->cleo_mk3_presets)){ ?>
									<a href="<?php echo FRONT_URL; ?>/software/presets/<?php if(!empty($recorddata[0]->cleo_mk3_presets)){echo $recorddata[0]->cleo_mk3_presets;}?>" target="_blank" title="Open File"><i class="glyphicon glyphicon-file"></i></a>
									 <a href="javascript:void(0)" onclick="deletefiles('<?php echo rtrim(strtr(base64_encode("path=".DOC_ROOT_FRONT."/software/presets/".$recorddata[0]->cleo_mk3_presets."&id=".$recorddata[0]->prod_id."&field=cleo_mk3_presets"), '+/', '-_'), '=')?>');" title="Delete"><i class="icon-remove-sign"></i></a>
								<?php }}?>
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label" for="oyster_presets">oyster presets</label>
							<div class="controls">
								<input class="input-xlarge" id="input_oyster_presets" value="<?php if(!empty($recorddata[0]->oyster_presets)){echo $recorddata[0]->oyster_presets;}?>" name="input_oyster_presets" type="hidden" >
								<input class="input-xlarge" id="oyster_presets " name="oyster_presets" type="file">
								<?php if(!empty($recorddata[0]->oyster_presets)){
									if(file_exists(DOC_ROOT_FRONT."/software/presets/".$recorddata[0]->oyster_presets)){ ?>
									<a href="<?php echo FRONT_URL; ?>/software/presets/<?php if(!empty($recorddata[0]->oyster_presets)){echo $recorddata[0]->oyster_presets;}?>" target="_blank" title="Open File"><i class="glyphicon glyphicon-file"></i></a>
									 <a href="javascript:void(0)" onclick="deletefiles('<?php echo rtrim(strtr(base64_encode("path=".DOC_ROOT_FRONT."/software/presets/".$recorddata[0]->oyster_presets."&id=".$recorddata[0]->prod_id."&field=oyster_presets"), '+/', '-_'), '=')?>');" title="Delete"><i class="icon-remove-sign"></i></a>
								<?php }}?>
							</div>
						</div>

And here is the code from the controller…

		if(isset($_FILES) && isset($_FILES["cleo_mk3_presets"]["name"]))
		{
			/* Unlink previous file */
			if(!empty($_POST['prod_id']))
			{
				$result = $this->productsmodel->getFormdata($_POST['prod_id']);
			
				if(!empty($result[0]->cleo_mk3_presets))
				{			
					$cleo_mk3_presets_path = DOC_ROOT_FRONT."/software/presets/".$result[0]->cleo_mk3_presets;
					if(file_exists($cleo_mk3_presets_path))
					{
						unlink($cleo_mk3_presets_path);
					}
				}
			}
			
			$thumnail_value = '';
			$config['upload_path'] = DOC_ROOT_FRONT."/software/presets/";
			$config['max_size']       = '1000000';
			$config['allowed_types'] = '*';
			//$config['file_name']     = md5(uniqid("100_ID", true));
			$config['file_name'] =  $_FILES["cleo_mk3_presets"]["name"];
			
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			
			if (!$this->upload->do_upload("cleo_mk3_presets"))
			{
				$image_error = array('error' => $this->upload->display_errors());
				echo json_encode(array("success"=>"0","msg"=>$image_error['error']));
				exit;
			}
			else
			{
				$image_data = array('upload_data' => $this->upload->data());
				$thumnail_value = $image_data['upload_data']['file_name'];
			}
		}
		else
		{
			$thumnail_value = $_POST['cleo_mk3_presets'];
		}
		
		$data['cleo_mk3_presets'] = $thumnail_value;

		if(isset($_FILES) && isset($_FILES["oyster_presets"]["name"]))
		{
			/* Unlink previous file */
			if(!empty($_POST['prod_id']))
			{
				$result = $this->productsmodel->getFormdata($_POST['prod_id']);
			
				if(!empty($result[0]->oyster_presets))
				{			
					$oyster_presets_path = DOC_ROOT_FRONT."/software/presets/".$result[0]->oyster_presets;
					if(file_exists($oyster_presets_path))
					{
						unlink($oyster_presets_path);
					}
				}
			}
			
			$thumnail_value = '';
			$config['upload_path'] = DOC_ROOT_FRONT."/software/presets/";
			$config['max_size']       = '1000000';
			$config['allowed_types'] = '*';
			//$config['file_name']     = md5(uniqid("100_ID", true));
			$config['file_name'] =  $_FILES["oyster_presets"]["name"];
			
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			
			if (!$this->upload->do_upload("oyster_presets"))
			{
				$image_error = array('error' => $this->upload->display_errors());
				echo json_encode(array("success"=>"0","msg"=>$image_error['error']));
				exit;
			}
			else
			{
				$image_data = array('upload_data' => $this->upload->data());
				$thumnail_value = $image_data['upload_data']['file_name'];
			}
		}
		else
		{
			$thumnail_value = $_POST['oyster_presets'];
		}
		
		$data['oyster_presets'] = $thumnail_value;





	/* cleo_mk3_presets */
	if(!empty($result[0]->cleo_mk3_presets))
	{
		$cleo_mk3_presets_path = DOC_ROOT_FRONT."/software/presets/".$result[0]->cleo_mk3_presets;
		if(file_exists($cleo_mk3_presets_path))
		{
			unlink($cleo_mk3_presets_path);
		}
	}
	
	/* oyster_presets */
	if(!empty($result[0]->oyster_presets))
	{
		$oyster_presets_path = DOC_ROOT_FRONT."/software/presets/".$result[0]->oyster_presets;
		if(file_exists($oyster_presets_path))
		{
			unlink($oyster_presets_path);
		}
	}

How do I stop the files being unlinked when I press the submit button. ?

maybe you just remove the unlink() calls then?

I need to be able to unlink as the files regularly get updated.

While I haven’t written an update portion of my image gallery section, I have written a delete blog posts where not only does it delete the blog post but the images as well.

<?php

require_once '../private/initialize.php';

use Library\CMS\CMS;

confirm_user_logged_in();
is_session_valid();

$cms = new CMS();
if (isset($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT)) {
    /*
     * Delete the image and the record from the database table journal
     */
    $id = filter_var($_GET['id']);
    $image_path = $cms->readImagePath($id);
    $thumb_path = $cms->readThumbPath($id);

    unlink($image_path);
    unlink($thumb_path);
    
    $result = $cms->deleteRecord($id);
    if ($result) {
        header("Location: member_page.php");
        exit();
    } 
} 

I treat unlink() function as delete file which it is and keep the paths to the images in a MySQL Database Table. When I get around to portion where I want the ability to edit my pictures, I will simply upload the the new images and delete the old images using unlink then store (update) the new paths for the images in the database table. The important thing to get out of this is unlink is delete.

Sponsor our Newsletter | Privacy Policy | Terms of Service