wladkov Posted March 10, 2009 Share Posted March 10, 2009 Hello Everyone. Im in the progress of creating a script that will create multiplay files at once, the script wiill do those things: Will get the name of the file. WIll get the source of the file. Will put 777 chmods on it. And it will save everything. Ok so here's my code and my problems: <html> <body> <head> <title>Random File Saver</title> </head> <form method="post" action=""> <textarea name="nazwa">Name Of File</textarea> <br /> <textarea name="source" cols="40" rows="20">Enter text</textarea> <br /> <input type="submit" value="Create"> </form> <?php //variables $filename = $_POST['nazwa']; $filesource = stripslashes($_POST['source']); //function create the file function create_file($filename, $filesource){ $fileh = fopen($filename, 'w+') or die("Cannot create a file"); fwrite($fileh, $filesource); fclose($fileh); chmod("$filename", 0777); } //use the function create_file($filename, $filesource); //if statment if (create_file==true){ echo "<pre>File(s) that have been created: <br /><br /></pre>"; echo $filename; } ?> 1. How can i prevent message "Cannot create a file" to show whenever the code is executed. Meaning it shows when i execute it once, then when i put the name and source there is no problem. 2. If you look at the last line, only the file name will print, is there a way that the user will be able to click on that file? Like in html using href option. Meaning i want some option that will read the file name from $_POST['nazwa'] and then it will allow the user to click on it. 3. Is there a way that i can upload many files at once? Like 5 files at once, or something and you can save each one with different name and source? Okay , that would be all. Thank you ! Quote Link to comment https://forums.phpfreaks.com/topic/148721-few-problems-with-my-code-creating-multiplay-files/ Share on other sites More sharing options...
aseaofflames Posted March 10, 2009 Share Posted March 10, 2009 1. How can i prevent message "Cannot create a file" to show whenever the code is executed. Meaning it shows when i execute it once, then when i put the name and source there is no problem. you should prevent the code from executing unless a post has been sent if(!isset($_POST['nazwa'])) exit; 2. If you look at the last line, only the file name will print, is there a way that the user will be able to click on that file? Like in html using href option. Meaning i want some option that will read the file name from $_POST['nazwa'] and then it will allow the user to click on it. echo "<a href=\"".$filename."">".$filename."</a>"; 3. Is there a way that i can upload many files at once? Like 5 files at once, or something and you can save each one with different name and source? Loops Quote Link to comment https://forums.phpfreaks.com/topic/148721-few-problems-with-my-code-creating-multiplay-files/#findComment-780901 Share on other sites More sharing options...
wladkov Posted March 10, 2009 Author Share Posted March 10, 2009 Could you show me like an example? With loops and saving many files at once? Quote Link to comment https://forums.phpfreaks.com/topic/148721-few-problems-with-my-code-creating-multiplay-files/#findComment-781505 Share on other sites More sharing options...
wladkov Posted March 11, 2009 Author Share Posted March 11, 2009 Okay guys, i added some things etc. I have a favor could you check those both codes and tell me what you think about them. How could or should i improve them? <html> <head> <title> File Delete </title> </head> <body bgcolor="black" text="red"> <center><pre><h2> > . file delete . < </pre></h2> <form method="post" action=""> <textarea name="delete">Delete File </textarea> <br /> <pre>Are you sure? </pre><br /> <input type="submit" value="Yes"> <input type="reset" value="No"> </form></center> </body> </html> <?php $filedelete = $_POST['delete']; function delete_file($filedelete){ if(!isset($_POST['delete'])) exit; unlink($_POST['delete']) or die("<pre>File couldn't be deleted.</pre>"); } delete_file($fieledelete); if (delete_file==true) { echo "<pre>File has been deleted.</pre>"; } ?> I know that if you execute this code without proper name it will show en error of Warning: unlink().... and then print "File couldnt be deleted"; How could i fix that? And is my if statment correct? Also here is the second code: <html> <head> <title>File Saver</title> </head> <body bgcolor="black" text="red"> <center><pre><h2> > . file saver . < </pre></h2> <form method="post" action=""> <textarea name="nazwa">Name Of File</textarea> <br /> <textarea name="source" cols="35" rows="20">Enter text</textarea> <br /> <input type="submit" value="Create"> </form></center> <?php //variables $filename = $_POST['nazwa']; $filesource = stripslashes($_POST['source']); //function create the file function create_file($filename, $filesource){ if(!isset($_POST['nazwa'])) exit; $fileh = fopen($filename, 'w+') or die("<pre>>>Cannot create a file.</pre>"); fwrite($fileh, $filesource); fclose($fileh); //uncoment to use chmods chmod("$filename", 0777); } //use the function if (create_file==true){ create_file($filename, $filesource); echo "<pre>>>File(s) that have been created: <br /><br /></pre>"; echo '<a href='.$filename.'>'.$filename.'<a/>'; } ?> What you think is everything looks correct? How could i use loops to allow me put more than 1 file at the time and give him a name? == THANKS !!!! Quote Link to comment https://forums.phpfreaks.com/topic/148721-few-problems-with-my-code-creating-multiplay-files/#findComment-781806 Share on other sites More sharing options...
aseaofflames Posted March 16, 2009 Share Posted March 16, 2009 if(!isset($_POST['delete'])) exit; change to if(!isset($_POST['delete']) || !file_exists($_POST['delete'])) exit; it will then also exit if the file doesn't exist Quote Link to comment https://forums.phpfreaks.com/topic/148721-few-problems-with-my-code-creating-multiplay-files/#findComment-786229 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.