esoteric Posted August 16, 2011 Share Posted August 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/ Share on other sites More sharing options...
The Little Guy Posted August 16, 2011 Share Posted August 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258191 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258192 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258193 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 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... } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258195 Share on other sites More sharing options...
The Little Guy Posted August 16, 2011 Share Posted August 16, 2011 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 />"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258197 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 So i take it that script would be the form action? How would the form look? i tried wrapping my php code in form tags but it didn't work Thanks for your patience Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258203 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 Before the <table> tag, add this line - <form action='form_processing_code_file_name.php' method='post'> After the </table> tag, add these lines - <input type='hidden' name='submit'> <input type='submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258204 Share on other sites More sharing options...
The Little Guy Posted August 16, 2011 Share Posted August 16, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258205 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258209 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 That code is nothing like either of us suggested in this thread. I recommend that you go back to the starting code and make just the changes to it that were suggested. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258215 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258238 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 you need to echo the input value. <td>Archive: <input type='checkbox' name='archive' value='<? echo $read'; ?>></td> <td>Delete: <input type='checkbox' name='delete' value='<? echo $read; ?>'></td> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258240 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 When you unlink, make your sure att the dir path and value. PFMaBiSmAd The Little Guy cox of you I have learnt some codes. THANK YOU. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258245 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258252 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 What is your error? Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258256 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 Warning: rename(../edit/news/news/test2.txt,../edit/news/archives/) [function.rename]: Not a directory in /xxxxx/xxxxx/xxxxx/xxxxx/xxxxx/admin/control/listnews.php on line 56 Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258258 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 means problem in dir path! you need to check -> ../edit/news/news/test2.txt,../edit/news/archives/ try removing .. Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258260 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 finally got it to move but it just appears in the same directory as my script and it is named whatever filename i give so for example rename($value, "$filename"); the file would then be called "$filename" Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258267 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 Pl check The Little Guy's code rename($value, "../archives/$filename"); Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258271 Share on other sites More sharing options...
esoteric Posted August 16, 2011 Author Share Posted August 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258275 Share on other sites More sharing options...
voip03 Posted August 16, 2011 Share Posted August 16, 2011 rename http://php.net/manual/en/function.rename.php <? // rename (' dir path/filename ', ' new dir path/ filename rename('/edit/news/news/'.$value, "/edit/news/archives/$value"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244938-move-file/#findComment-1258285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.