dear all,

I want to make a digital library goverment regulation which related to export import activity in my aoffice. The problem is:
I dont know how to upload the .pdf file to local server and provide the link to acess it automatically, Please help me..:(

Dani AI

Generated

Good goal, . The basic flow is: save each upload into a single writable folder and then print the URL to that file. is right about listing the directory, and is right that a normal anchor link is fine for viewing. Where mentioned $_SERVER: use server variables for two different things: a filesystem path for saving, and a URL for linking. Prefer __DIR__ (or dirname(__FILE__) on very old PHP) for paths; use HTTP_HOST and the current scheme for URLs.

Here is a compact, safer handler that saves a PDF and immediately prints its link. It validates size and MIME, creates the uploads folder if missing, and generates a unique, URL-safe name.

<?php
// Assumes a form with input name="pdf" posted to this script.
$uploadDir = __DIR__ . '/uploads';
if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); }

if (!empty($_FILES['pdf']) && $_FILES['pdf']['error'] === UPLOAD_ERR_OK) {
    if ($_FILES['pdf']['size'] > 10 * 1024 * 1024) { exit('File too large.'); }

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime  = finfo_file($finfo, $_FILES['pdf']['tmp_name']);
    finfo_close($finfo);
    if ($mime !== 'application/pdf') { exit('Only PDF files are allowed.'); }

    $slug = preg_replace('~[^a-z0-9]+~i', '-', pathinfo($_FILES['pdf']['name'], PATHINFO_FILENAME));
    $name = strtolower(trim($slug, '-')) . '-' . time() . '.pdf';

    $dest = $uploadDir . DIRECTORY_SEPARATOR . $name;
    if (move_uploaded_file($_FILES['pdf']['tmp_name'], $dest)) {
        $scheme  = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
        $baseUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
        $url     = $scheme . '://' . $_SERVER['HTTP_HOST'] . $baseUrl . '/uploads/' . rawurlencode($name);
        echo 'Saved. Link: <a href="' . htmlspecialchars($url, ENT_QUOTES) . '">' . htmlspecialchars($url, ENT_QUOTES) . '</a>';
    } else {
        exit('Upload failed.');
    }
}

To build your digital library automatically (per ), render an index page that scans uploads/*.pdf, sorts by filemtime, and prints links. Also check file_uploads, upload_max_filesize, and post_max_size in php.ini. If you need downloads to start instead of opening in the browser, serve through a small download.php and send Content-Disposition: attachment.

Recommended Answers

All 9 Replies

Upload the PDF files to a directory and have the PHP script check the directory and list any PDF files in there.

When uploading the file I think you mean to the remote server, and you can just use an FTP client for this such as Filezilla, Dreamweaver, FTP Pro, LeechFTP, etc...

When linking to the pdf document make sure you use
$_SERVER

And then the file path and filename to the PDF file.

When uploading the file I think you mean to the remote server, and you can just use an FTP client for this such as Filezilla, Dreamweaver, FTP Pro, LeechFTP, etc...

When linking to the pdf document make sure you use
$_SERVER

And then the file path and filename to the PDF file.

Hi,
Instead of simply specifying the link as

<a href="www.sitename.com/documentname.pdf">DocumentPDF</a> , why should we use $SERVER['DOCUMENT_ROOT']

???
Is there any specific advantage with that?! Please let me know as am a novice.
Thank you.

Sorry, I meant if you're using a force download or if you're attaching the PDF in an email.

If you're simply linking to it then you can just use the relative file path.

Sorry for the confusion.

Sorry, I meant if you're using a force download or if you're attaching the PDF in an email.

If you're simply linking to it then you can just use the relative file path.

Sorry for the confusion.

Ok, so $SERVER forces download.
Thank you dats a good info.

Adding that code doesn't force the download, but when using force download with PDFs you can't use a relative file path, you must go back to the root.

To do a force download you will first need to set some PHP headers at the top of your page.

So why not just use an anchor link for downloading? using PHP seems a bit tricky and long winded to me. The user will have to click a link a some point to get to the page so if you want it to automatically download just write the ducoment into the anchor text like <a href"http://www.somesite.com/thepdf.pdf">the pdf</a> - it will be a lot easier in the long run :)

Thank you a lot for your advice. I have try the code
and I found the suitable one

function upload_dir()
{
    $dir = $_SERVER['PHP_SELF'];
    for($i=0;$i<strlen($dir);$i++){
        if(substr($dir,$i,1)=="/") $slashpos=$i;
    }
    $dir = substr($dir,0,$slashpos);
    $dir = $_SERVER['DOCUMENT_ROOT'].$dir."/files/";
    return($dir);
}

if($upl==1){
    $uploaddir = upload_dir();
    $uploaddir=$uploaddir.substr($ext,0);

    if(!file_exists($uploaddir))
    {
        makeindex($uploaddir."log.txt");
    }

    $uploadfile = $uploaddir.$_FILES['userfile']['name']; 

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {
       $address = getimgplace(substr($ext,0));
       echo "File is valid, and was successfully uploaded.<br>\n";
       echo "Location:<font color=\"blue\"><b> $address </b></font>\n";
       echo "Copy the blue address as the link address! <br> <br> \n";
       echo "Your IP: <font color=\"red\">".$_SERVER['REMOTE_ADDR']."</font>\n";

       //menyimpan alamat upload
       include "cd_connect.inc.php";
       pg_dbname();
       $query = "INSERT INTO t_master_reg VALUES ('$kode',null,null,null,null,null,null,'$address',null)";
       pg_query($connection,$query);
       header("location:frm_input_reg.php");     

    }
    else
    {
       echo "Possible file upload attack!\n";
    }
}

thank you very much :)

see this

PHP » File Directory » File Upload         
    
Upload PDF file and rename it

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
   Last Name:<br /> <input type="text" name="name" value="" /><br />
   Class Notes:<br /> <input type="file" name="classnotes" value="" /><br />
   <p><input type="submit" name="submit" value="Submit Notes" /></p>
</form>

<?php
   define ("FILEREPOSITORY","./");

   if (is_uploaded_file($_FILES['classnotes']['tmp_name'])) {

      if ($_FILES['classnotes']['type'] != "application/pdf") {
         echo "<p>Class notes must be uploaded in PDF format.</p>";
      } else {
         $name = $_POST['name'];
         $result = move_uploaded_file($_FILES['classnotes']['tmp_name'], FILEREPOSITORY."/$name.pdf");
         if ($result == 1) echo "<p>File successfully uploaded.</p>";
         else echo "<p>There was a problem uploading the file.</p>";
      } #endIF
   } #endIF
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.