Jump to content

How to read off an uploaded filename to create a link ?


tmyonline

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).  Any ideas and suggestions ?  Many thanks!

$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
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.