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). Also, I just want to create the link to the uploaded file (I don't want to list all the images in the folder as one forum helper mistakenly thought !!!). Any idea or suggestion? Many thanks! EDITED BY akitchin: please be kind and code tag-it. Quote Link to comment https://forums.phpfreaks.com/topic/63190-how-to-read-off-an-uploaded-file-name-to-create-a-link/ Share on other sites More sharing options...
akitchin Posted August 3, 2007 Share Posted August 3, 2007 even if $file1_renamed had a value, it wouldn't echo the link properly, as you're closing your quotes on the href attribute: <td><a href="/comments/<?php echo $file1_renamed; ?>">Draft</a></td> are you running the upload code from within a function? and have you tried dumping your variables prior to and after running the upload code? a handy line for debugging variables is: exit('<pre>'.print_r(get_defined_vars(), TRUE)); Quote Link to comment https://forums.phpfreaks.com/topic/63190-how-to-read-off-an-uploaded-file-name-to-create-a-link/#findComment-314954 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.