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!
SOLVED : Help with arrays!!
Moderator: General Moderators
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
SOLVED : Help with arrays!!
Last edited by andylyon87 on Thu Sep 04, 2008 2:50 pm, edited 1 time in total.
- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: Help with arrays!!
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
-Andy
Re: Help with arrays!!
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.
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!!
Im finding it hard to explain ok here goes: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
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[] />
- andyhoneycutt
- Forum Contributor
- Posts: 468
- Joined: Wed Aug 27, 2008 10:02 am
- Location: Idaho Falls
Re: Help with arrays!!
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]);
}
}
}-Andy
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
Re: Help with arrays!!
that is exactly what I wanted, its so simple but I just could not work out how I could do it!!
Thanks
Thanks