Jump to content

UPLOAD PDF?..


jushiro

Recommended Posts

You would probably be best to upload the file to a folder on the server.

 

You can open the pdf in a browser by using an <iframe> tag.

 

Use something like this to retrieve it:

 

<?php
$imgDir = "path_to_upload_folder";
if(($files = @scandir($imgDir)) && (count($files) > 2))  
{  
if($handle = opendir($imgDir))
{
    while($file = readdir($handle))
    {
        clearstatcache();
        if(is_file($imgDir.'/'.$file))
           echo "<iframe height='1200' width='100%'  src='".$imgDir."/".$file."'></iframe>";	
    }
    closedir($handle);
}
?>

 

Hope this helps. Good luck.

Link to comment
Share on other sites

What error are getting?

 

Here's a break down of code:

 

$imgDir = "path_to_upload_folder";  -- obvious

 

if(($files = @scandir($imgDir)) && (count($files) > 2))  -- scans the folder where the file is kept and if there are more than 3 items the folder is not empty (3 because it accounts for the . and .. that are present in folders) and the following curly braces are read

 

if($handle = opendir($imgDir)) -- opens the folder so the files can be used

 

while($file = readdir($handle)) -- process the following code for each file that is present in the folder

 

clearstatcache(); -- clear the information that php has cached about the file

 

if(is_file($imgDir.'/'.$file)) -- if the file is present

 

          echo "<iframe height='1200' width='100%'  src='".$imgDir."/".$file."'></iframe>"; -- open the file in an iframe

    }

 

    closedir($handle); -- close the directory that was opened

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Basically I'm developing an e-learning system.. so i need to make a code that'

a teacher Uploads lesson in the database in form of PDF. then another user which is the

student can access the uploaded PDF file in browser.. :(' but im that PRO for coding in php.

but i know the basics of PHP.  help pls.  :-[

Link to comment
Share on other sites

I didn't do any code for that YET.. but I'm planning to make a Drop-down list and use POST.

 

This is how i want to do. Teacher will upload a lesson and be uploaded in the database(SQL).

so that the list in the Drop-down list would only be those in the database made by the teacher.

 

And yea, i cant make a form with a selection because i cant put the uploaded PDF files in my database. :(

Link to comment
Share on other sites

Ok.

 

You have options for how to go about this, but probably the best for you would be:

 

Assuming if a Teacher is logged in, they will have some form of userID eg. $_SESSION['userID'].  When you create your upload form for the Teachers, have it something like this:

 

<form method="post" enctype="multipart/form-data" action="upload.php">
<label for="lesson">LESSON NAME: <input type="text" name="lesson" id="lesson" /></label><br/>
<label for="pdf">LESSON NAME: <input type="file" name="pdf" id="pdf" /></label><br/>
<input type="submit" name="submit" value="UPLOAD" />
</form>

 

Now in your upload script (upload.php) you should upload the file into a directory, but at the same time do a mysql insert into a table against the userID of the teacher along with the file name (not the file just the files name), the lesson title.  To get the file name use php $_FILES function, the teachers userID should be something like $_SESSION['userID'], and the LESSON NAME $_POST['lesson'].

 

Now you have the .pdf's uploaded into a directory and the lesson information against a teachers userID and lesson title in the db.

 

You can now retrieve from the db a record set of lessons from individual teachers and populate the students list of lessons with the lesson titles as the labels and the file_names as the values.

 

Now when the student selects the lesson from the menu, collect the file name on the form action page as $_POST (like you say), and display it like this on the page.

 

<iframe height='1200' width='100%'  src='/path/to/folder/<?php echo $_POST['filename_inputs_name']; ?>'></iframe>

 

And you should be close to what you want.

 

 

 

 

Link to comment
Share on other sites

site for an example i want to retrieve my pdf file which is "Chapter.pdf" inside the folder "upload"

What code should i put for $_FILE?

 

I think the array your thinking of is $_FILES and it has nothing to do with retrieving files. Ig you already have the file uploaded, you simply need to use a html anchor tag to link to the file. Nothing at all to do with php.

Link to comment
Share on other sites

Hi. thx for the reply.. But i dont get it.

 

I've got my DB in my sql.. I want to get the value of the uploaded file.

Ive got This table.

 

TITLE | FILE NAME |

Lesson / CHAPTER.pdf

 

This table is created when User uploaded files in the folder "upload"

 

How can i open a pdf file of my choice? im thinking of $_FILE but i dont know how it works.

Link to comment
Share on other sites

Jushiro, I think you would be best helped by picking up a book on PHP/Mysql...

 

You don't actually store the PDF file IN the database, there is merely a reference to the file name in there (and sometimes even the complete file path, if your aren't worried about bit size in the DB).

 

1. You are missing the primary key column for the database.. if you don't know what this is, i highly recommend reading up on the basics of MySQL

 

2. Use a standard upload form as BorderLine suggested to upload the PDF (or any other file)

 

3. Retrieve the file (as thorpe said) by using a standard anchor <a> "link" tag. the $_FILES array is used when uploading files from a webform to the server and has nothing to do with the database processes

 

4. use the primary key as the connection between your select menu and the appropriate file

 

eg:

 

<select name="teachers-docs">

<option value="primary_key_goes_here">Document for math homework</option>

<option value="primary_key_goes_here">Document for science homework</option>

<option value="primary_key_goes_here">Document for history homework</option>

</select>

 

when the student selects an option, a query can be submitted to the database calling for the file_name column for primary_key # x which will give you the appropriate file name.

 

I recommend using $_GET to submit your form, as it will make testing and implementation slightly easier.

 

Also, provide a link to the file and allow the student to download the PDF (to make things easier)

 

good luck

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.