Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP assistance with some code needed.

Hi all,

I've got a bit of code which I need to expand on. (thanks agamal for the code).
Anyway, this code attached below is a form which makes a copy of an existing file and saves it renamed.
I need to expand on it but due to my basic php knowledge I'm going to need help.

This is the additional functionality I need on this code in order:

1. Select a filename from a dropdown box (does it already but it's hardcoded and only one at the moment)

2. Select where you want to save it ...

3. save it.

That's it!

Please let me know if you need extra explanation

Cheers

st3vo

see current code below:



<?php
if ($_POST['submit']=="submit") {
$tamp1 = "array.php";
$newname = $_POST['newfile'];
if (!copy($tamp1, $newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$tamp1." has been renamed to ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

Avatar of ST3VO
ST3VO
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Note:

reference: [1. Select a filename from a dropdown box (does it already but it's hardcoded and only one at the moment) ] - I don't need it to read the files on the directory, the select box filenames I need to pre-define them.

Same as $tamp1 = "array.php"; but in a select box so I have several choices.

Thx


Avatar of Robin Uijt
Is this what you want: ?
<?php
  if ($_POST['submit']=="submit") {
    $tamp1 = $_POST['oldfile'];
    $newname = $_POST['newfile'];
    if (!copy($tamp1, $newname)) {
      echo "failed to copy $file...\n";
    }
    else {
      echo "File ".$tamp1." has been renamed to ".$newname.".";
    }
}
else {
?>
<form name="form" action="" method="POST">
<select name="oldfile">
<option value="array1.php">array1.php</option>
<option value="array2.php">array2.php</option>
<option value="array3.php">array3.php</option>
</select>
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

check this

<?php
if ($_POST['submit']=="submit") {
$tamp1 = $_POST['filelist'];
$newname = $_POST['newfile'];
if (!copy($tamp1, $newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$tamp1." has been renamed to ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<select name="filelist" size="1">
<?php
//directory store files
    $directory = ".";
    // create a handler for the directory
    $handler = opendir($directory);
    $filenames = array();
    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {
        if ($file != '.' && $file != '..') {
echo "<option value=\"$file\">$file</option>";
}                }
 
?>
</select>
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

you can specify which directory you have your file in ......and it will list the file and rename it in same directory
> I don't need it to read the files on the directory, the select box filenames I need to pre-define them.

You mean with pre-defining them, the same a just a simple select box with choices in them?
I created a php 5 example below, it takes as default directory the php script location.

<?php
if (isset($_POST['submit']) AND $_POST['submit']=="submit") {
$file = (string)$_POST['files'];
$newname = (string)$_POST['newfile'];
if (!copy($file, $newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$file." has been renamed to ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<?php
if ($path = getcwd())
    {
		echo '<select name="files">';
		foreach ( scandir ($path)  as $val)
        {
		if ( is_file ( $val ) )
            {
				echo '<option value="'.$val.'">'.$val.'</option>';
			}
		}
		echo '</select>';
	}
?>
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

Avatar of ST3VO

ASKER

OK, I've checked all codes...

Although I said I wanted to pre-define the filenames I like the idea of how agamal has done it.

Having said that none of the samples let you select where you want to save the new file.

thx's all!!!
If you enter the complete path in the new file textbox, you can set the location it is saved to ?
check this ... now you can set the destination dir .... ;)  have code

<?php
if ($_POST['submit']=="submit") {
$tamp1 = $_POST['filelist'];
$desdir = "c:\\";
$newname = $desdir.$_POST['newfile'];
if (!copy($tamp1, $newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$tamp1." has been renamed to ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<select name="filelist" size="1">
<?php
//directory store files
    $directory = ".";
    // create a handler for the directory
    $handler = opendir($directory);
    $filenames = array();
    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {
        if ($file != '.' && $file != '..') {
echo "<option value=\"$file\">$file</option>";
}                }
 
?>
</select>
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

check this ... now you can set the destination dir .... ;)  have code
 

<?php
if ($_POST['submit']=="submit") {
$tamp1 = $_POST['filelist'];
$desdir = "c:\\";
$newname = $desdir.$_POST['newfile'];
if (!copy($tamp1, $newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$tamp1." has been renamed to ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<select name="filelist" size="1">
<?php
//directory store files
    $directory = ".";
    // create a handler for the directory
    $handler = opendir($directory);
    $filenames = array();
    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {
        if ($file != '.' && $file != '..') {
echo "<option value=\"$file\">$file</option>";
}                }
 
?>
</select>
<input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

Avatar of ST3VO

ASKER

hmmm...destination Dir is currently hardcoded I need to select it from the form.

Hope you can help!

Cheers

Something like this ?

You check whether the entered paths & filename are valid!
<?php
  if ($_POST['submit']=="submit") {
    $tamp1 = $_POST['oldfile'];
    $newpath = $_POST['newpath'];
    $newname = $_POST['newfile'];
    $newname = $newpath ."\". $newname;
//add checks for valid paths,etc!
    if (!copy($tamp1, $newname)) {
      echo "failed to copy $file...\n";
    }
    else {
      echo "File ".$tamp1." has been renamed to ".$newname.".";
    }
}
else {
?>
<form name="form" action="" method="POST">
<select name="oldfile">
<option value="array1.php">array1.php</option>
<option value="array2.php">array2.php</option>
<option value="array3.php">array3.php</option>
</select>
New path name : <input name="newpath">
New file name : <input name="newfile">
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

Avatar of ST3VO

ASKER

sort of .... but not exactly what I'm looking for...

I need something like agamal's code (when it get's the filenames and populates it in the combobox .... I need to see the folders and select where I want to save it...kind of explorer.
ASKER CERTIFIED SOLUTION
Avatar of Ionut A. Tudor
Ionut A. Tudor
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
robinu has gave a nice example how to write the dest folder and dest file name

if you want to have a list to destination folders tell us where you will get the folders list
Avatar of ST3VO

ASKER

Spot on!!!! Thanks a lot!!!! :o)
Avatar of ST3VO

ASKER

Thanks all for all your samples and help!

Wish I could give you all top points!

In this case al3cs12 has got it spot on....although all other codes worked too!!!

Thanks all!!

st3vo
i don't know how ... because i didn't ask a Q here before but you may assign assisted points to whom helped
ST3VO thanks for the points, glad to spot your problem and be able to help you. Cheers
You could give assisted points to me and agamal for giving the code that allowed you to pick a file from a directory, although you specifically did not ask for that!
We gave solutions for the rest, albeit you chose the best solution. Too bad...
Avatar of ST3VO

ASKER

Sorry about that...never thought of that! I will keep it in mind for the future!!!