[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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

0
ST3VO
Asked:
ST3VO
  • 7
  • 6
  • 5
  • +1
1 Solution
 
ST3VOAuthor Commented:
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


0
 
RobinSoftware EngineerCommented:
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

0
 
agamalCommented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
agamalCommented:
you can specify which directory you have your file in ......and it will list the file and rename it in same directory
0
 
RobinSoftware EngineerCommented:
> 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?
0
 
Ionut A. TudorPHP ProgrammerCommented:
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

0
 
ST3VOAuthor Commented:
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!!!
0
 
RobinSoftware EngineerCommented:
If you enter the complete path in the new file textbox, you can set the location it is saved to ?
0
 
agamalCommented:
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

0
 
agamalCommented:
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

0
 
ST3VOAuthor Commented:
hmmm...destination Dir is currently hardcoded I need to select it from the form.

Hope you can help!

Cheers

0
 
RobinSoftware EngineerCommented:
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

0
 
ST3VOAuthor Commented:
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.
0
 
Ionut A. TudorPHP ProgrammerCommented:
This might be what you're looking for:
 

<?php
if (isset($_POST['submit']) AND $_POST['submit']=="submit") {
$file = (string)$_POST['files'];
$newdir = (string)$_POST['dirs'];
$newname = (string)$_POST['newfile'];
 
if (!copy($file, $newdir."/".$newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$file." has been moved at ".$newdir." and renamed into: ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<br>
<?php
if ($path = getcwd())
    {
		echo 'Which file<select name="files">';
		foreach ( scandir ($path)  as $val)
        {
		if ( is_file ( $val ) )
            {
				echo '<option value="'.$val.'">'.$val.'</option>';
			}
		}
		echo '</select>';
	}
?>
<br>
<?php
if ($path = getcwd())
    {
		echo 'Save in which directory<select name="dirs">';
		foreach ( scandir ($path)  as $val)
        {
		if ( is_dir ( $val ) AND $val != '.' AND $val != '..')
            {
				echo '<option value="'.$val.'">'.$val.'</option>';
			}
		}
		echo '</select>';
	}
?>
<br>
New File name: <input name="newfile">
 
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

0
 
agamalCommented:
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
0
 
ST3VOAuthor Commented:
Spot on!!!! Thanks a lot!!!! :o)
0
 
ST3VOAuthor Commented:
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
0
 
agamalCommented:
i don't know how ... because i didn't ask a Q here before but you may assign assisted points to whom helped
0
 
Ionut A. TudorPHP ProgrammerCommented:
ST3VO thanks for the points, glad to spot your problem and be able to help you. Cheers
0
 
RobinSoftware EngineerCommented:
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...
0
 
ST3VOAuthor Commented:
Sorry about that...never thought of that! I will keep it in mind for the future!!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now