Page 1 of 1
SOLVED : Help with arrays!!
Posted: Thu Sep 04, 2008 2:09 pm
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!
Re: Help with arrays!!
Posted: Thu Sep 04, 2008 2:18 pm
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
Re: Help with arrays!!
Posted: Thu Sep 04, 2008 2:22 pm
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
}
Re: Help with arrays!!
Posted: Thu Sep 04, 2008 2:28 pm
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?
Re: Help with arrays!!
Posted: Thu Sep 04, 2008 2:36 pm
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
Re: Help with arrays!!
Posted: Thu Sep 04, 2008 2:49 pm
by andylyon87
that is exactly what I wanted, its so simple but I just could not work out how I could do it!!
Thanks