Jump to content

To Create an hyperlink in PHP which is used to open an file with any format which is saved on the server folder


Sandeep590
Go to solution Solved by Sandeep590,

Recommended Posts

Hello Everyone,

 

Kindly please help me on this task.

 

On part of my Graduate Final Project, I should display a link on PHP page which opens the file stored in folder upon clicking the hyperlink.

 

 

1) I have a file uploaded on the folder in the server

2) I need to display the file as hyperlink

3) Clicking on hyperlink should open the file in the same window

 

 

Iam done with step 1 . However , Iam stuck with step 2 and step 3

 

 

I need a PHP code which is used to open a file when clicked on hyperlink in previous page

 

 

For any information , please ping me here

Link to comment
Share on other sites

 

2) I need to display the file as hyperlink

Start with using glob to list files in a directory. Outputting each file in HTML anchor tag, in the url for each link pass the filename as a querystring parameter

 

 

3) Clicking on hyperlink should open the file in the same window

Use $_GET superglobal variable to get the filename from the querystring. Checking to make sure the file exists. if the file does exist use then usefile_get_contents to output the contents of the file. You may to use header for setting the correct mime type in order for the browser to display the contents of the file properly.

Link to comment
Share on other sites

foreach (glob("*.txt") as $filename) {
    echo '<a href="$filename">$filename</a><br>';
}

This code will scan the current directory for txt files and return the file names. You can use these names as anchor tags.

You can change the extension ".txt" with your desired extension or use * for any extension

Edited by Falakistan
Link to comment
Share on other sites

Hello,

 

I need to display a hyperlink

 

Attachment :    File.txt

 

When I double click on File.txt ,  the file should be opened which is stored in the server folder.

 

 

Note :  Assume that there are multiple files in the folder . How do we identify that we need to display that particular file to be opened

 

 

Assume , I have three files    text.txt , word.doc and pd.pdf

 

how to make these files open by clicking on the hyperlink

Edited by Ch0cu3r
Link to comment
Share on other sites

Did you miss my post? I have explained what you need to do also pointed to relevant documentation pages. I am not going to write the code for you. Have a go yourself if you become stuck then post what you have tried.

 

I have removed your contact number, not a good idea posting your personal contact details on open forum on the internet.

Link to comment
Share on other sites

@Sandeep590, you state that these could be any type of file. If that is true, then what you are asking cannot be achieved. Only some file types can be opened within a browser. Other file types can be opened by the user's system if they have the corresponding application for the particular file type. But, that is a hit and miss based on the user's browser and the particular file type.

 

Also,

When I double click on File.txt ,  the file should be opened which is stored in the server folder.

 

Double-click? Don't tell me you double-click on links in a web page.

Link to comment
Share on other sites

@Pyscho :

 

The File can be any type , implies that the file extension can be any one of the common file formats - doc , png , jpg , pdf or txt files.

 

And , this file needs to be opened , once we click on hyperlink which means that target can be marked as target=_blank  ; 

 

"_blank" opens the new document in a new window where the file needs to be opened
Link to comment
Share on other sites

Hello Everyone

 

I was able to download the files. However, there is an issue here ..

 

<td><a href = "downloadfile.php?file='.$_FILES['FileName']['name'].'">Download files</a></td>

 

When I have multiple requests , under single page , when we click on the above download files link , it is displaying the same attachment all the time.

 

Can anyone please help me on how to display different attachments in single page so that each time , when a user clicks on the attachment , the file which he uploaded on the server should be opened.

 

Kindly help me on this .

Link to comment
Share on other sites

  • Solution

Thank you all for your valuable replies.  Iam able to upload the file, store the file in a folder , display an attachment which opens the file stored in the folder which was uploaded earlier.

 

I learnt 3 things which will be helpful to the viewers. Therefore , I want to list out those key points.

 

1) It is efficient to pass the filename by using querystring in a href tag through which the filename will be retrieved by the database.

    Example : <td><a href="downloadfile.php?file='.$data['Filename'].' ">'.data['Filename'].'</a>

   

2) It is a good method to use session variables SESSION['sessionvar'] to pass the data through PHP variables from one page to another.

   

    In one php page

    <?php

    session_start();

    $_SESSION['sessionvar'] = $_FILES['FileName']['name'];

 

     In another php page

     session_start();

     $filename = $_SESSION['sessionvar'];

   

3) It is always a good practice to store the data (here in this case : filename ) in the database and later retrieve it based on the conditions assumed.

 

 

Please let me know for any further information on this post . 

Link to comment
Share on other sites

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.