Jump to content

move file


esoteric

Recommended Posts

Hi guys, i have written this script to display all files in a folder, what to do is somehow turn this into a form or table with options to move the file to another folder. Basically its so i can move news article to a 'archive' folder other wise they all display on my page which before long is just too many and i have too delete them.

 

so far i have

<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2">News Articles</td>
  </tr>
  <tr>
    <td width="140"> </td>
    <td width="480"> </td>
  </tr>
  <tr>
    <td align="center"> </td>
    <td align="center"><?php
    $dir = opendir('../edit/news/news/');
echo '<ul>';

while ($read = readdir($dir))
{

if ($read!='.' && $read!='..')
{
echo '<li><a href="../edit/news/news/'.$read.'">'.$read.'</a></li>';
}

}

echo '</ul>';

closedir($dir);
?></td>
  </tr>
</table>

 

Ideally it would be great if i had say a menu next to each file with the option to 'delete' or 'send to achive'

 

thank you

Link to comment
Share on other sites

I didn't code everything, so here is an example:

 

This is the page that has the main form of the files you want to move:

<?php
foreach(glob('../edit/news/news/*') as $file){
echo "<input type='checkbox' name='file[]' value='$file' /> $file<br />";
}
?>

 

This is the page that processes the files once you submit the form:

<?php
foreach($_POST['file'] as $file){
if(is_file($file)){
	$filename = basename($file);
	rename($file, "../archives/$filename");
}
}
?>

 

Note: I didn't test this.

Link to comment
Share on other sites

You can enclose your existing code in a form and add two checkboxes to your <li></li> tag (note I put your ../edit/news/news value into the variable $path so that it could be set at one place in your code, but used anywhere it is needed) -

 

	echo "<li><a href='$path/$read'>$read</a> Delete: <input type='checkbox' name='delete' value='$read'> Archive: <input type='checkbox' name='archive' value='$read'></li>";

 

You would then loop over any of the 'delete' checkbox values to delete files and loop over any of the 'archive' checkbox values to archive files.

Link to comment
Share on other sites

You would then loop over any of the 'delete' checkbox values to delete files and loop over any of the 'archive' checkbox values to archive files.

 

Your code works to add checkboxes, but i don't understand what you mean by this? Could you please explain? Thank you.

Link to comment
Share on other sites

Upon further review (and testing) of the code I posted, you would need to make the name="" attributes arrays (delete[] and archive[]).

 

You would then be able to process the checkboxes using code similar to this -

 

<?php
// your authentication/log in check code would go here...

if(isset($_POST['submit'])){
if(isset($_POST['delete']) && is_array($_POST['delete'])){
	foreach($_POST['delete'] as $value){
		echo "Delete $value<br />";
		// your code to delete the file given by $value would go here...
	}
}
if(isset($_POST['archive']) && is_array($_POST['archive'])){
	foreach($_POST['archive'] as $value){
		echo "Archive $value<br />";
		// your code to archive the file give by $value would go here...
	}
}
}
?>

Link to comment
Share on other sites

Using PFMaBiSmAd's code, I added some error checking:

 

<?php
// your authentication/log in check code would go here...

if(isset($_POST['submit'])){
if(isset($_POST['delete']) && is_array($_POST['delete'])){
	foreach($_POST['delete'] as $value){
		if(file_exists($value)){
			echo "Delete $value<br />";
			unlink($value);
		}else{
			echo "File Not Found $value; Did Not Delet<br />e";
		}
	}
}
if(isset($_POST['archive']) && is_array($_POST['archive'])){
	foreach($_POST['archive'] as $value){
		if(file_exists($value)){
			echo "Archive $value<br />";
			rename($value, "../archives/$filename");
		}else{
			echo "File Not Found $value; Did Not Move<br />";	
		}
	}
}
}
?>

Link to comment
Share on other sites

You would make your form kinda like this:

 

<form action="/process/processFiles.php" method="post">
<table>
<tr>
	<td>Filename</td>
	<td>Archive</td>
	<td>Delete</td>
</tr>
<?php
foreach(glob('../edit/news/news/*') as $file){
	echo "<tr>";
	echo "<td>$file</td>";
	echo "<td><input type='checkbox' name='archive[]' value='$file' /> $file</td>";
	echo "<td><input type='checkbox' name='delete[]' value='$file' /> $file</td>";
	echo "</tr>";
}
?>
</table>
</form>

Link to comment
Share on other sites

Thanks for all your replies, i have read through and tried each one without much luck, im still struggling to grasp php

 

This is what i have, i check a checkbox and press submit query then i take me to a page to a '0' in the top left, but nothing happens teh file doesnt move or delete

 

<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2">News Articles</td>
  </tr>
  <tr>
    <td width="140"> </td>
    <td width="480"> </td>
  </tr>
  <tr>
    <td align="center"> </td>
    <td align="center">
<?php
$path = ('../edit/news/news/');
    $dir = opendir('../edit/news/news/');
echo '<ul>';

while ($read = readdir($dir))
{

if ($read!='.' && $read!='..')
{
?>
<form action="/admin/edit/news/process.php" method="post">
<table>
<tr>
	<td>Filename <? echo "<a href='$path/$read'>$read</a>" ?></td>
	<td>Archive: <input type='checkbox' name='archive' value='$read'></td>
	<td>Delete: <input type='checkbox' name='delete' value='$read'></td>
</tr>
</table>
    <input type='hidden' name='submit'>
<input type='submit'>
</form>
<?
}

}

echo '</ul>';

closedir($dir);
?>
</td>
  </tr>
  <tr>
    <td align="center"> </td>
    <td align="center"> </td>
  </tr>
</table>

 

Im guessing the page above is completely wrong?

Link to comment
Share on other sites

Ok the following works to delete it but the problem im having is it wont move it too archive,

 

Warning: rename(../edit/news/news/test news article.txt,../edit/news/archives/) [function.rename]: Not a directory

Thing is there is a folder there, my folder my txt files are in is: 'website'/admin/edit/news/news/ and the archive folder is at 'website'/admin/edit/news/archive/

 

<form action='' method='post'>
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
	<td>Filename</td>
	<td>Archive</td>
	<td>Delete</td>
</tr>
<?php
$path =('../edit/news/news/');
    $dir = opendir('../edit/news/news/');
echo '<ul>';

while ($read = readdir($dir))
{

if ($read!='.' && $read!='..')
{
echo '<li><a href="'.$path.$read.'">'.$read.'</a></li>';
}

}

echo '</ul>';

foreach(glob('../edit/news/news/*') as $file){
	echo "<tr>";
	echo "<td>$file</td>";
	echo "<td><input type='checkbox' name='archive[]' value='$file' /> $file</td>";
	echo "<td><input type='checkbox' name='delete[]' value='$file' /> $file</td>";
	echo "</tr>";
}
closedir($dir);
?>
</table>
<input type='hidden' name='submit'>
<input type='submit'>
</form>
<?php
// your authentication/log in check code would go here...

if(isset($_POST['submit'])){
if(isset($_POST['delete']) && is_array($_POST['delete'])){
	foreach($_POST['delete'] as $value){
		if(file_exists($value)){
			echo "Delete $value<br />";
			unlink($value);
		}else{
			echo "File Not Found $value; Did Not Delete<br />";
		}
	}
}
if(isset($_POST['archive']) && is_array($_POST['archive'])){
	foreach($_POST['archive'] as $value){
		if(file_exists($value)){
			echo "Archive $value<br />";
			rename($value, "../edit/news/archives/$filename");
		}else{
			echo "File Not Found $value; Did Not Move<br />";	
		}
	}
}
}
?>

 

The only other thing is can i have it display the file name as in 'test news article.txt' rather than the whole path ''../edit/news/news/test news article.txt''

 

Thanks again, i know its a pain help complete idiots out  ;)

 

 

edit: forgot the mention that this script is in the location "'website'/admin/control/ "if that helps at all.

Link to comment
Share on other sites

I tried what you guys said but nothing, when i try to echo the value it does nothing (cant delete or move), it keeps putting out an error on line 56 which is

rename($value, "../edit/news/archives/$filename");

 

I tried many variation of this line but nothing solves it, any ideas?

Link to comment
Share on other sites

The path i wrong with that code, the variable $file holds the path to the existing file, how can i make a new variable with the new file path?

 

i know i can do something like

$new = ../edit/news/archives/

 

but i don't know ho to get the file name on the end, all the variable seem to be holding the path as well

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.