Jump to content

Recommended Posts

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.

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));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.