Jump to content

[SOLVED] Changing file import script


bbunlock

Recommended Posts

First of thanks for taking the time to read this and help if possible.

 

I have a script that allows me to import data into the database, it works great for the current system setup but I have just changed it and can not figure out how to alter the script to apply the new changes to the system.

 

the current system works like this, I upload a number of theme files (example.sis) and a screenshot of each theme file (example.jpg), now the screenshot file needs to be named exactly the same as the theme file but of course different extension, example "Blue_Theme.sis" "Blue_Theme.jpg" and the script works great.

 

However here is my problem, the new system uses 2 screenshot files for the the themes and they are named like this "Blue_Theme.sis" "Blue_Theme-1.jpg" "Blue_Theme-2.jpg" and I cant figure out what to do on the current script to make it work with the 2 screenshot files named a little differently.

 

I will paste the original code (before I changed it 100 times trying to get it working) for you to have a look at and hopefully get it working with the new system

 

<?
require ('inc_auth.php');
include ('functions.php');
include ('lib/lib_image.php');
$link = 10;

if (isset($_POST['directory']) && count($_POST) == 1){
  $directory = trim($_POST['directory']);
  if (is_dir($directory)) {
      if ($dh = opendir($directory)) {
         while (($file = readdir($dh)) !== false) {
            $arr[] = $file;
         }
         closedir($dh);
         foreach ($arr as $v){
          preg_match("~^(.+?)\.(\w{3,4})~is", $v, $m);
          if (!in_array($m[1], $fileArr) && $m[1] != null) 
            $fileArr[] = $m[1];
        }
      }
    }
}

if (isset($_POST['dir'])){
  $directory = trim($_POST['dir']);
  if (is_dir($directory)) {
      if ($dh = opendir($directory)) {
        while (($file = readdir($dh)) !== false) {
          $arr[] = $file;
        }
        closedir($dh);
        foreach ($arr as $v){
          preg_match("~^(.+?)\.(\w{3,4})~is", $v, $m);
          if ($m[1] != null){
            if ($m[2] == "sis")
              $fileArr[$m[1]]["sis"] = $v;
            else $fileArr[$m[1]]["image"] = $v;
          }
        }
        $category_id = (int)$_POST['category'];
        $model = (int)$_POST['model'];
        $thm_type_id = (int)$_POST['thm_type'];
        
        $r = $sql->load_obj("SELECT id FROM mobiles WHERE thm_type={$thm_type_id} LIMIT 1");
		  $mobile_id = $r->id;
		  $r = $sql->load_obj("SELECT * FROM thm_types WHERE id={$thm_type_id}");
		  $thm_type  = $r->type;
		  $r = $sql->load_obj("SELECT * FROM categories WHERE id={$category_id}");
		  $category = $r->name;
		  			  
		  $files = $_POST['f'];
		  foreach ($files as $v=>$value){ 
          
          if (isset($fileArr[$v]['image']) && isset($fileArr[$v]['sis'])){
         		 $sql->insert("files", array(
         		 
         		 'name'			=> $fileArr[$v]['sis'],
         		 'file_type'		=> "theme",
         		 'user_id'		=> 1,
         		 'category_id'		=> $category_id,
         		 'upl_date'		=> date("Y-m-d H:i:s"),
         		 'active'		=> 1,
         		 'mobile_id'		=> $mobile_id));

  					$id = $sql->last_id();

	$sql->insert("ratings", array(
				'id'		=> $id,
				'total_value'	=> 0,
				'total_votes'	=> 0,
				'used_ips'	=> 0));

  					$sys_file_dest = "{$id}.sis";
  					$thumb_file  = "{$id}_thumb.jpg";
  					$dest_file	 = "{$id}.jpg";
  						
  					copy("{$directory}/{$fileArr[$v]['image']}", "{$themesDir}/".folderName($thm_type)."/".folderName($category)."/{$dest_file}");
  					
  					$thumb = imageCreateResized("{$directory}/{$fileArr[$v]['image']}", $themeThumbWidth);
  					
            imagejpeg($thumb, "{$themesDir}/".folderName($thm_type)."/".folderName($category)."/{$thumb_file}");
            
            copy("{$directory}/{$fileArr[$v]['sis']}", "{$themesDir}/".folderName($thm_type)."/".folderName($category)."/{$sys_file_dest}");
            
            unlink("{$directory}/{$fileArr[$v]['image']}");
            unlink("{$directory}/{$fileArr[$v]['sis']}");
            
            $savedFiles[] = "{$directory}/{$fileArr[$v]['image']} - {$directory}/{$fileArr[$v]['sis']}";
            
            echo $sql->error();
          }
      }
      $auth->redirect("import.php?count=".count($savedFiles));
      }
  	}
  }
?>
<HTML>
<HEAD>
	<TITLE>Import themes</TITLE>
	<link rel="stylesheet" href="style.css" type="text/css">
</HEAD>
<BODY>
	<? include ('admin/left_menu.php'); ?>
	<DIV style="float: left"> 
	  <h3>Import themes</h3>
	  <form method='POST' action=''>
	    <table class='design' cellspacing="0" width=400>
	      <?
	        if (count($_POST) == 0 && count($_GET) == 0){ ?>
	      <tr>
	        <td colspan="2">
	          <label for='directory'>Directory</label>
	          public_html/<input type='text' name='directory' id='directory'>
	        </td>
	      </tr>
	      <tr>
	        <td colspan="2" align="right"><input type='submit' value='Submit' class='button'></td>
	      </tr>
	      <? }elseif (isset($_POST['directory'])) {?>
	      <tr>
	        <td>
	          <label for='category'>Category</label>
	          <select id='category' name="category" style="width: 140px">
	            <?
	              $sql->query("SELECT * FROM categories WHERE type='Theme' ORDER BY name");
	              while($r = $sql->obj()){
	                echo "<option value='{$r->id}'>{$r->name}</option>";
	              }
	            ?>
	          </select>
	        </td>
	      </tr>
	      <tr>
	        <td>
	          <label for='thm_type'>Type</label>
    		        <select name='thm_type' id='thm_type' style="width: 140px">
    		          <?
    		            $sql->query("SELECT * FROM thm_types ORDER BY type");
    		            while ($r = $sql->obj()){
    		              echo "<option value='{$r->id}'>{$r->type}</option>";
    		            }
    		          ?>
    		        </select>
    		      
    		    </td>
	      </tr>
	      <?
	        foreach ($fileArr as $k=>$v){
	          echo "<tr><td colspan='2'><input type='checkbox' value='1' checked name='f[".htmlspecialchars($v)."]}'>".htmlspecialchars($v)."</td></tr>"; 
	        }
	      ?>
          <input type='hidden' name='dir' value='<?=$_POST['directory']?>'>
          <tr>
	        <td align="right"><input type='submit' value='Submit' class='button'></td>
	      </tr>
	      <?} elseif(isset($_GET['count'])){?>
	          <tr>
	            <td>
	              <? echo (int)$_GET['count']." themes have been saved"; ?>
	            </td>
	          </tr>
	      <?}?>
	    </table>  
	  </form>
	</DIV>
</BODY>
</HTML>

 

thanks in advance

 

wayne

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.