Announcement

#1 2004-12-16 13:18:48

hakrz1
Member
2004-12-12
8

rename file from admin

Here is a simple rename script (you can rename your files from the admin)
this is useful for people who use

Code:

 include/config.inc.php
 $conf['order_by'] = ' order by file asc';

and want the pictures to sort a certain way.
I should really note this is somewhat buggy and has no fullproof error checking and might not even work on some servers (mostly ones with safemode on or strict hosting companys)
so far I have only tested this on my local webserver (Apache/1.3.33,php 4.3.9, linux)
MAKE A BACKUP of your SQL and files before trying ANY script that can alter files.

It won't replace space with underscore or check for any kind of malformed names it's pretty much direct so be careful when using this.

sample's
picture modify
default name
**link dead**
renamed
**link dead**

info images
default filename
**link dead**
renamed filename
**link dead**
**link dead**
//error if file has same name as another file
**link dead**

Code:

##############################################################################
## MOD Title: FileRename                                                     
## MOD Author: hakrz1                   
## MOD Description: Rename file from admin & keep all the info for the image
## MOD Version: -0.0.1 (works with PhpWebGallery 1.3.4)                       
##                                                                           
## Installation Level: moderate                                              
## Installation Time: 10~ Minutes                                            
## Files To Edit: admin/picture_modify.php,infos_image.php                   
##                template/default/admin/picture_modify.vtp,infos_image.vtp  
## Included Files: (none)                                                    
##############################################################################
############################################################################## 
## Author Notes: This script renames the image, its thumbnail 
## and also updates the SQL to correspond with the new File Name.
##
############################################################################## 
## MOD History: 
##
##   2004-12-20 - Version "-"0.0.3
##      - Inital Release.
##
##   2004-12-22 - Version "-"0.0.2
##      - support for "Images Info" so you can do mass rename instead of one
##      - file at a time.
##
##   2005-01-02 - Version "-"0.0.1 
##      - Updated code so it's more easy to read and install.
## 
############################################################################## 
## Before Adding This MOD You Should BACKUP FILES SQL AND IMAGES related 
## to this mod 
## please don't continue if you don't plan on doing this.
############################################################################## 
#
#-----[ OPEN ]----------------------------------------------------------------
#
admin/picture_modify.php
#
#-----[ FIND ]----------------------------------------------------------------
#
    $query.= "'".htmlentities( $_POST['name'], ENT_QUOTES )."'";
#
#-----[ AFTER, ADD ]----------------------------------------------------------
#
    $query.= ', file = ';
if ( $_POST['file'] == '' )
    $query.= 'NULL';
  else
$why_error="";
if (isset($_REQUEST['why_error'])) $why_error = $_REQUEST['why_error']; 
  { error_reporting(E_ALL); @ini_set('display_errors', '1'); }
      $nameinfo = array( 'file','tn_ext','storage_category_id' );
      $filename = 'SELECT '. implode( ',', $nameinfo );
      $filename.= ' FROM '.PREFIX_TABLE.'images';
      $filename.= ' WHERE id = '.$_GET['image_id'];
      $filename.= ';';
      $fn = mysql_fetch_array( mysql_query( $filename ) );
      $tninfo = array('prefix_thumbnail');
      $tnprefix = 'SELECT '. implode( ',', $tninfo );
      $tnprefix.= ' FROM '.PREFIX_TABLE.'config';
      $tnprefix.= ';';
      $tn = mysql_fetch_array( mysql_query( $tnprefix ) );
      $dir_url = get_complete_dir( $fn['storage_category_id'] );
      $dir_url.= str_replace( '','',($fn['file']) );
      $new_url = get_complete_dir( $fn['storage_category_id'] );
      $tn_url = get_complete_dir( $fn['storage_category_id'] );
      $tn_url.= 'thumbnail/' . $tn['prefix_thumbnail'];
      $tn_url.= str_replace( '','',($fn['file']) );
      $new_tn = get_complete_dir( $fn['storage_category_id'] );
      $new_tn.= 'thumbnail/'. $tn['prefix_thumbnail'] ;
      $result = mysql_query( $filename );
while ( $fn = mysql_fetch_array( $result ) )
if ($_POST['file'] == $fn['file'])
  {
}
elseif (file_exists(".$new_url"."$file"))
{
// array_push( $errors, 'There is already a file with this name please specify another name'); uncomment if want a better format but may cause issues
    exit("There is already a file with this name please specify another name<a href ONCLICK='window.parent.location.href=window.parent.location.href;'>
    <br><FONT color=#043460 size=4>go back</font></a>");
} else {
    rename(".$dir_url",".$new_url"."$file") or 
    die("<a ONCLICK='window.parent.location.href=window.parent.location.href;'><FONT color=#043460 size=4>go back</font></a> $why_error");
    rename(".$tn_url",".$new_tn"."$file") or 
    die("<a ONCLICK='window.parent.location.href=window.parent.location.href;'><FONT color=#043460 size=4>go back</font></a> $why_error");
}
// $query.= "'".htmlspecialchars( $_POST['file'], ENT_QUOTES )."'";
$query.= "'".htmlentities( $_POST['file'], ENT_QUOTES )."'";

#
#-----[ OPEN ]----------------------------------------------------------------
#
template/default/admin/picture_modify.vtp
#
#-----[ FIND ]----------------------------------------------------------------
#
            <td>{#f_file}</td>
#
#-----[ REPLACE WITH ]---------------------------------------------------------
#
            <td><input type="text" name="file" value="{#f_file}" /></td>
                      
#
#-----[ OPEN ]----------------------------------------------------------------
#
admin/infos_image.php

#
#-----[ FIND ]----------------------------------------------------------------
#
    $query = 'SELECT id,file';
    
#
#-----[ REPLACE WITH ]---------------------------------------------------------
#
    $query = 'SELECT id,file,storage_category_id';
    
#
#-----[ FIND ]----------------------------------------------------------------
#
    $query.= ' WHERE category_id = '.$page['cat'];
    $query.= ';';
    
#
#-----[ AFTER, ADD ]---------------------------------------------------------
#
    $row = mysql_fetch_array( mysql_query( $query ) );
    
#
#-----[ FIND ]----------------------------------------------------------------
#
      $name          = 'name-'.$row['id'];
      
#
#-----[ AFTER, ADD ]---------------------------------------------------------
#
      $file          = 'file-'.$row['id'];
      
#
#-----[ FIND ]----------------------------------------------------------------
#      
      $query.= "'".htmlentities( $_POST[$name], ENT_QUOTES )."'";    
      
#
#-----[ AFTER, ADD ]---------------------------------------------------------
#
$query.= ', file = ';
  if ( $_POST[$file] == '' )
    $query.= 'NULL';
else
$why_error='';
if (isset($_REQUEST['why_error'])) $why_error = $_REQUEST['why_error']; 
{ error_reporting(E_ALL); @ini_set('display_errors', '1'); }
    // sql 
    $infoo = array('prefix_thumbnail');
    $queryt = 'SELECT '. implode( ',', $infoo );
    $queryt.= ' FROM '.PREFIX_TABLE.'config';
    $queryt.= ';';
    $rows = mysql_fetch_array( mysql_query( $queryt ) );
// do some things here and hope it doesnt rename whole file system :O
    $dir_url = get_complete_dir( $row['storage_category_id'] );
    $dir_url.= str_replace( '','',($row['file']) );
    $new_url = get_complete_dir( $row['storage_category_id'] );
    $tn_url = get_complete_dir( $row['storage_category_id'] );
    $tn_url.= 'thumbnail/' . $rows['prefix_thumbnail'];
    $tn_url.= str_replace( '','',($row['file']) );
    $new_tn = get_complete_dir( $row['storage_category_id'] );
    $new_tn.= 'thumbnail/'. $rows['prefix_thumbnail'];

if ("$_POST[$file]" == "$row[file]")
{
    }
elseif (file_exists(".$new_url"."$_POST[$file]")){
     array_push( $errors, 'There is already a file with this name please specify another name');
    break;
} else {
rename(".$dir_url",".$new_url"."$_POST[$file]") or 
die("<a ONCLICK='window.parent.location.href=window.parent.location.href;'><FONT color=#043460 size=4>go back</font></a> $why_error");
rename(".$tn_url",".$new_tn"."$_POST[$file]") or 
die("<a ONCLICK='window.parent.location.href=window.parent.location.href;'><FONT color=#043460 size=4>go back</font></a> $why_error");
// post sql data after files have been renamed  
}
// $query.= "'".htmlspecialchars( $_POST[$file], ENT_QUOTES )."'";
$query.= "'".htmlentities( $_POST['file'], ENT_QUOTES )."'";

#
#-----[ FIND ]----------------------------------------------------------------
#
                'infoimage_keyword_separation','infoimage_associate',
    
#
#-----[ REPLACE ]------------------------------------------------------------
#    
                'infoimage_keyword_separation','file','infoimage_associate',

#
#-----[ OPEN ]----------------------------------------------------------------
#
template/default/admin/infos_image.vtp

#
#-----[ FIND ]----------------------------------------------------------------
#
      <th colspan="7">{#infoimage_detailed}</th>
#
#-----[ REPLACE ]------------------------------------------------------------
#    
       <th colspan="8">{#infoimage_detailed}</th>    
        
#
#-----[ FIND ]----------------------------------------------------------------
#
      <td class="row2" style="text-align:center;">{#infoimage_title}</td> 
    
#
#-----[ AFTER,ADD ]---------------------------------------------------------
#    
      <td class="row2" style="text-align:center;">{#file}</td>  
      
#
#-----[ FIND ]----------------------------------------------------------------
#
      <td style="text-align:center;">{#default_name}<br /><input type="text" name="name-{#id}" value="{#name}" maxlength="255"/></td>
  
#
#-----[ AFTER,ADD ]---------------------------------------------------------
#    
      <td style="text-align:center;"><input type="text" name="file-{#id}" value="{#filename}" /></td> 
#
#-----[ SAVE/CLOSE FILES ]------------------------------------------------
#
# Eof

Last edited by hakrz1 (2005-01-04 02:51:50)

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact