SOLVED : Help with arrays!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

SOLVED : Help with arrays!!

Post by andylyon87 »

hey guys,

am having problems with arrays!! Its doing my head in!!

Right here is the problem, I have a load of inputs and I wanna be able to rename a file using them.

Ok well say I have a file called img1.jpg and img2.jpg

I have been trying to feed them both into an array so that img1.jpg equals the rename1.jpg

I have been using rename[] as the name of the hidden input containing all the img names, then newname[] as the renaming text field.

I havent put in any code as my attempt was way off mark!
Last edited by andylyon87 on Thu Sep 04, 2008 2:50 pm, edited 1 time in total.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Help with arrays!!

Post by andyhoneycutt »

Could you explain a little more clearly or at least jot out some pseudo-code for what you're trying to do? I'm having a difficult time understanding what you're trying to attempt.

-Andy
ahowell
Forum Newbie
Posts: 17
Joined: Mon Sep 01, 2008 9:18 pm

Re: Help with arrays!!

Post by ahowell »

Your description isn't very... descriptive, but I'll try.

If you can't pick up what you need from this example, please give more details.

Code: Select all

<?php
 
$newNameList = array(
    'old_name_img1' => 'new_name_img1',
    'old_name_img2' => 'new_name_img2'
);
 
 
$oldFile = 'REPLACE_WITH_PATH_TO_FILE';
$oldFileInfo = pathInfo($oldFile);
 
if (!rename($oldFile, $newNameList[$oldFileInfo['basename']])) {
    // handle error
}
 
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Re: Help with arrays!!

Post by andylyon87 »

andyhoneycutt wrote:Could you explain a little more clearly or at least jot out some pseudo-code for what you're trying to do? I'm having a difficult time understanding what you're trying to attempt.

-Andy
Im finding it hard to explain ok here goes:

Code: Select all

 
<?
if(form submitted){
     
     //make rename = newname in an array
 
     foreach(occurence of newname[] not being empty){
          //replace rename[] with newname[]
     }
}
?>
 
<input type=hidden name=rename[] value="originalimgname.jpg" /> <input type=text name=newname[] />
 
 
I hope that makes more sense now?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Help with arrays!!

Post by andyhoneycutt »

Code: Select all

 $original_name = $_POST['rename'];
$new_name      = $_POST['newname'];
 
// See if we have values to work with.
if( !empty($original_name) && !empty($new_name) )
{
  // Make sure the values submitted are of equal length
  // in elements.
  $a = count($original_name);
  $b = count($new_name);
  if($a == $b)
  {
    for($i=0; $i<$a; $i++)
    {
      // rename expects the full /path/to/the/files
      rename($original_name[$i], $new_name[$i]);
    }
  }
}
Hope this helps!
-Andy
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Re: Help with arrays!!

Post by andylyon87 »

that is exactly what I wanted, its so simple but I just could not work out how I could do it!!

Thanks
Post Reply