gazalec Posted February 19, 2007 Share Posted February 19, 2007 I'm not really sure how this would work but its worth a try, i was wondering if it would be possible to have a Browse button on a web page using PHP (or any other language but i dont know which i'd use), the reason is i have an admin section of a website and i want the whole website to be maintained from here so if an image has to be changed they use this browse button and select the image, just wondering if it is possible or another possible solution if dont know if the HTML code <input type="file" size="48" name=""> would be what i'm looking for Quote Link to comment Share on other sites More sharing options...
trq Posted February 19, 2007 Share Posted February 19, 2007 If you wanted to browse files on the webserver you would need to write this functionality yourself. Take a look at the filesystem functions in the manual. Quote Link to comment Share on other sites More sharing options...
gazalec Posted February 19, 2007 Author Share Posted February 19, 2007 no i want it to browse the users/ admin's hard drive because they would have the new image on their hard drive but i dont know how i could then upload it to the webserver Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 19, 2007 Share Posted February 19, 2007 Then you will want to look at the section of the manual on file uploads... http://us2.php.net/manual/en/features.file-upload.php Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 I made one of these before. I don't have time to go through my old code and make it easier to read, but its posted below. This is an old browse script that was in a popup, which isn't a good idea, but I used it in a custom content management system I made. Keep in mind I made this a long time ago and the coding isn't great, but you can understand the functionality from it. <?php session_start(); $dir="../items/"; function execute_upload($cfile) { // root path $path = ''; // upload directory. path will originate from root. $dirname = $_GET['dir']; // permission settings for newly created folders $chmod = 0755; // create file vars to make things easier to read. $filename = $_FILES[$cfile]['name']; $filesize = $_FILES[$cfile]['size']; $filetype = $_FILES[$cfile]['type']; $file_tmp = $_FILES[$cfile]['tmp_name']; $file_err = $_FILES[$cfile]['error']; $file_ext = strrchr($filename, '.'); // check if user actually put something in the file input field. if (($file_err == 0) && ($filesize != 0)) { // Check extension. if (!$file_ext) { unlink($file_tmp); die('File must have an extension.'); } // extra check to prevent file attacks. if (is_uploaded_file($file_tmp)) { /* * check if the directory exists * if it doesnt exist, make the directory */ $dir = $path . $dirname; if (!is_dir($dir)) { $dir = explode('/', $dirname); foreach ($dir as $sub_dir) { $path .= '/' . $sub_dir; if (!is_dir($path)) { if (!mkdir($path, $chmod)) { unlink($file_tmp); die('<strong>Error:</strong> Directory does not exist and was unable to be created.'); } } } } /* * copy the file from the temporary upload directory * to its final detination. */ if (@move_uploaded_file($file_tmp, $dir . '/' . $filename)) { // success! echo $filename ." <b>uploaded!</b> "; if (!chmod($dir . '/' . $filename, 0777)) { // Could not change permissions unlink($dir . '/' . $filename); echo '<strong>Error:</strong> Unable to change permissions.'; } } else { // error moving file. check file permissions. unlink($file_tmp); echo '<strong>Error:</strong> Unable to move file to designated directory.'; } } else { // file seems suspicious... delete file and error out. unlink($file_tmp); echo '<strong>Error:</strong> File does not appear to be a valid upload. Could be a file attack.'; } } else { // Kill temp file, if any, and display error. if ($file_tmp != '') { unlink($file_tmp); } switch ($file_err) { case '0': echo 'That is not a valid file. 0 byte length.'; break; case '1': echo 'This file, at ' . $filesize . ' bytes, exceeds the maximum allowed file size as set in <em>php.ini</em>. '. 'Please contact your system admin.'; break; case '2': echo 'This file exceeds the maximum file size specified in your HTML form.'; break; case '3': echo 'File was only partially uploaded. This could be the result of your connection '. 'being dropped in the middle of the upload.'; case '4': echo 'You did not upload anything... Please go back and select a file to upload.'; break; } } return; } ?> <html> <head> <title>Browse Files</title> <? if (isset($_GET['file'])) { echo '<script>opener.document.'. $_GET['ret'] .'.link1.value = \''. $_GET['file'] .'\';</script>'; } ?> </head> <body> <? if (isset($_GET['dir'])) { $dir=$_GET['dir']; } $handle=opendir($dir); $x=0; $y=0; if (isset($_GET['a'])) { if ($_GET['a'] == "upload") { echo ' <form name="upload" method="POST" action="browse.php?ret='. $_GET['ret'] .'&dir='. $_GET['dir'] .'&a=cupload" enctype="multipart/form-data"> Select Files To Upload:<br> <input type="file" name="file1" tabindex="1" size=12 /> <input type="file" name="file2" tabindex="1" size=12 /> <input type="file" name="file3" tabindex="1" size=12 /> <input type="file" name="file4" tabindex="1" size=12 /> <input type="file" name="file5" tabindex="1" size=12 /> <input type="file" name="file6" tabindex="1" size=12 /> <input type="file" name="file7" tabindex="1" size=12 /> <input type="file" name="file8" tabindex="1" size=12 /> <input type="file" name="file9" tabindex="1" size=12 /> <input type="hidden" name="upl" value="1"> <input type="submit" name="Submit" value="Upload Now!"> </form> '; } if ($_GET['a'] == "cupload") { for ($x=1; $x < 10; $x++) { $fname="file". $x; $filesize = $_FILES[$fname]['size']; $file_err = $_FILES[$fname]['error']; if (($file_err == 0) && ($filesize != 0)) { echo execute_upload($fname); } } } } echo " <br><br><a href=\"browse.php?ret=". $_GET['ret'] ."&dir=". $dir ."&a=upload\"><b>Upload Files</b></a><br> <b>Select the file you wish to link to:</b><br> Directory: ". substr($dir, strpos($dir, '/', 0)) ." <table>"; while (false !== ($file = readdir($handle))) { if (($file != "..")&&($file != ".")) { $ext = ltrim(strrchr($file,'.'),'.'); if (empty($ext)) { $icon="folder.gif"; $folder[$y]=" <tr><td><img src=\"". $icon ."\"></td><td><a href=\"browse.php?ret=". $_GET['ret'] ."&dir=". $dir . $file ."/\">". $file ."</a></td></tr> "; $y++; } else { $icon="file.gif"; $files[$x]=" <tr><td><img src=\"". $icon ."\"></td><td><a href=\"#\" onClick=\"opener.document.". $_GET['ret'] .".iSrc.value = '". substr($dir, strpos($dir, '/', 0)+1).$file ."'; opener.document.". $_GET['ret'] .".IMAGE.src='../". substr($dir, strpos($dir, '/', 0)+1).$file ."'; window.close();\">". $file ."</a></td></tr> "; $x++; } } } sort($folder); sort($files); if ($dir != "../") { $count=substr_count($dir, "/"); echo "<tr><td><img src=\"up.gif\"></td><td><a href=\"browse.php?ret=". $_GET['ret'] ."&dir=". substr($dir, 0, strpos($dir, '/', $count)+1) ."\">Up One Level</a></td></tr>"; } for ($a=0; $a <= $y; $a++) { echo $folder[$a]; } for ($b=0; $b <= $x; $b++) { echo $files[$b]; } echo "</table>"; closedir($handle); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
gazalec Posted February 19, 2007 Author Share Posted February 19, 2007 i dont know if the code will do this but what i want to happen is the same idea as an aviator, where you browse your hard drive hit add/update/upload whatever this then takes that image uploads it to the server which whenever you send a message it shows your aviator Quote Link to comment Share on other sites More sharing options...
tom100 Posted February 19, 2007 Share Posted February 19, 2007 Looks like I'm cutting through my code anyway: $handle=opendir($dir); echo "<table>"; while (false !== ($file = readdir($handle))) { if (($file != "..")&&($file != ".")) { $ext = ltrim(strrchr($file,'.'),'.'); if (empty($ext)) { $icon="folder.gif"; $folder[$y]=" <tr><td><img src=\"". $icon ."\"></td><td><a href=\"browse.php?ret=". $_GET['ret'] ."&dir=". $dir . $file ."/\">". $file ."</a></td></tr> "; $y++; } else { $icon="file.gif"; $files[$x]=" <tr><td><img src=\"". $icon ."\"></td><td><a href=\"#\" onClick=\"opener.document.". $_GET['ret'] .".iSrc.value = '". substr($dir, strpos($dir, '/', 0)+1).$file ."'; opener.document.". $_GET['ret'] .".IMAGE.src='../". substr($dir, strpos($dir, '/', 0)+1).$file ."'; window.close();\">". $file ."</a></td></tr> "; $x++; } } } sort($folder); sort($files); if ($dir != "../") { $count=substr_count($dir, "/"); echo "<tr><td><img src=\"up.gif\"></td><td><a href=\"browse.php?ret=". $_GET['ret'] ."&dir=". substr($dir, 0, strpos($dir, '/', $count)+1) ."\">Up One Level</a></td></tr>"; } for ($a=0; $a <= $y; $a++) { echo $folder[$a]; } for ($b=0; $b <= $x; $b++) { echo $files[$b]; } echo "</table>"; closedir($handle); Once again this is crappy code, but look at the functionality and you'll be on your way. Quote Link to comment 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.