tmyonline Posted August 3, 2007 Share Posted August 3, 2007 Dear all: I want to read an uploaded filename, say "mypic.jpg" to create a link to this image without having to ask the users to enter the filename "mypic.jpg" when uploading it. My upload code works file but it's not on the same page. I'm working on my "index.php" page and included the upload.php page "<?php include("upload.php"); ?>" in it. Here's the sample of my code in the upload.php page (the code for the form is separate and not included here): <?php if (array_key_exists('submit', $_POST)) { // (array_key_exists('upload', $_POST)) // define constant for upload folder define('UPLOAD_DIR', '/var/www/itrdweb/advancednetworkingplan/comments/'); // replace any spaces in original filename with underscores and assign to a simpler variable (optional but important) $file1 = str_replace(' ', '_', $_FILES['upload1']['name']); // move the file to the upload folder and rename it $file1_renamed = time().$file1; move_uploaded_file($_FILES['upload1']['tmp_name'], UPLOAD_DIR.$file1_renamed); } ?> I want to use the variable "$file1_renamed" in my index.php page to create a link to this file like this: <table> <tr> <td<a href="/comments/"<?php echo($file1_renamed); ?>>Draft</a></td> </tr> </table> I have tried many different ways and it seemed that this variable "$file1_renamed" does not carry any value (although it does contain the filename for upload). Any ideas and suggestions ? Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63174-how-to-read-off-an-uploaded-filename-to-create-a-link/ Share on other sites More sharing options...
tmyonline Posted August 3, 2007 Author Share Posted August 3, 2007 Sorry, I made a mistake when copying the code, here's that line again: <td><a href="/comments/"<?php echo($file1_renamed); ?>>Draft</a></td> Quote Link to comment https://forums.phpfreaks.com/topic/63174-how-to-read-off-an-uploaded-filename-to-create-a-link/#findComment-314875 Share on other sites More sharing options...
calabiyau Posted August 3, 2007 Share Posted August 3, 2007 $dir = "path/to/your/files"; $dh = opendir($dir) or die ("could not open dir"); while ( !(($file = readdir($dh)) === false) ) { if (($file != ".") && ($file != "..")) { //echo your link here using $file for name of the file //this will list all the images in the folder } } Quote Link to comment https://forums.phpfreaks.com/topic/63174-how-to-read-off-an-uploaded-filename-to-create-a-link/#findComment-314879 Share on other sites More sharing options...
tmyonline Posted August 3, 2007 Author Share Posted August 3, 2007 No, I use "image" as an example but it's actually document instead. I just want to make the link to the document (or image) that the user uploads. I don't want to list all the documents (or images) in the folder !!! Quote Link to comment https://forums.phpfreaks.com/topic/63174-how-to-read-off-an-uploaded-filename-to-create-a-link/#findComment-314882 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.