fluidsharp Posted December 14, 2009 Share Posted December 14, 2009 Dears. Please help. I would like to upload file to some directory and get the file lists of that dir.. So I created index.html (see code below) It has 2 forms: first form with buttons "Browse", "Send File", second form with button "get_names". So I can upload file to some directory, there is the upload.php help in this(see code below). And I have other file getfilename.php. I haven't been written it yet, but just put there code " echo "hello"; " So when I press button "get_names", buttons from first form disappear. How I need write code that buttons from both forms don't disappear. code of index.html : <html> <head> <title>Загрузка файлов на сервер</title> </head> <body> <form enctype="multipart/form-data" action="upload.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="1024*3*1024" /> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> <form enctype="multipart/form-data" action="upload.php" method="GET"> <input type="submit" value="get_files" /> </form> </body> </html> ------------------------ code of upload.php: <? //require_once('getfilenames.php'); echo '<pre>'; $uploaddir = "C:\\temp\\"; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; echo $uploadfile; echo "<br>"; print_r($uploadfile); echo "<br>"; } else { echo "File upload err!\n"; } ?> ------------------------- code of getfilenames.php (haven't written yet): <?php //require_once('upload.php'); Echo "hello"; ?> thanks a lot in advance. Link to comment https://forums.phpfreaks.com/topic/185147-upload-file-to-directory-and-get-file-list-there/ Share on other sites More sharing options...
killerb Posted December 14, 2009 Share Posted December 14, 2009 It is common practice to put PHP processing at top of the file and have the form posting to the same file, this way PHP attempts to process a submitted form if possible and displays the form afterwards, whether or not the form was processed. Your task to list files and upload files is better to do with two files: upload.php list.php Begin with upload.php, place your html form into that file and above your form, put your processing logic within PHP tags. eg: <?php if(count($_POST)>0){ // processing goes here, validate and store the uploaded files } ?> <html> <body> <?php // output any errors that were encountered during form processing ?> <form method="post"> <!-- ommitting action attribute forces form submission to same uri --> </form> </body> </html> On your list page you will want to get all files into an array and then loop them: <?php $dh = opendir(dirname(__FILE__)); $files = array(); while($file = readdir($dh)){ if($file=='..' || $file=='.') continue; $files[] = $file; } ?> <html> <body> <h1>List of files:</h1> <ul> <?php foreach($files as $file){ echo '<li>'.$file.'</li>'; } ?> </ul> </body> </html> This way the PHP logic is separated from the HTML markup, and the PHP logic within HTML is for display only. It is generally advised that all PHP data retrieval and preparation is done before HTML begins, as you can see in 2nd example that all files are prepared into an array before the template begins. Its a personal preference of course, but you will find such conventions assist the separation of roles according to MVC principles, even without applying strict MVC rules. Also using the same file (or identical file path and file name in /controllers and /templates) for processing and template assists in tracking down the right file when it comes to maintaining a code base in future. Link to comment https://forums.phpfreaks.com/topic/185147-upload-file-to-directory-and-get-file-list-there/#findComment-977335 Share on other sites More sharing options...
fluidsharp Posted December 15, 2009 Author Share Posted December 15, 2009 Thanks a lot. I'll do it Link to comment https://forums.phpfreaks.com/topic/185147-upload-file-to-directory-and-get-file-list-there/#findComment-978073 Share on other sites More sharing options...
fluidsharp Posted December 15, 2009 Author Share Posted December 15, 2009 so solution which I looking for is header("Location: $url"); all buttons are keep. Link to comment https://forums.phpfreaks.com/topic/185147-upload-file-to-directory-and-get-file-list-there/#findComment-978124 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.