PHP open file url is not working (I use pdf js for pdf view only)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script src="https://mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script>
<script type='module'>
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = <?php'$fileUrl';?>
// Loaded via script> tag, create shortcut to access PDF.js exports.
var { pdfjsLib } = globalThis;
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.mjs';
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 0.8,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
/**
* Get page info from document, resize canvas accordingly, and render page.
* @param num Page number.
*/
function renderPage(num) {
pageRendering = true;
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport({scale: scale});
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
// Wait for rendering to finish
renderTask.promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
// New page rendering is pending
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
// Update page counters
document.getElementById('page_num').textContent = num;
}
/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
pageNumPending = num;
} else {
renderPage(num);
}
}
/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById('prev').addEventListener('click', onPrevPage);
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById('next').addEventListener('click', onNextPage);
/**
* Asynchronously downloads PDF.
*/
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
</script>
<?php
// Function to trim the extension from a filename
function trimExtension($filename) {
// Check if the filename has a dot indicating an extension
if (strpos($filename, '.') !== false) {
// Remove everything after and including the last dot
$filename = substr($filename, 0, strrpos($filename, '.'));
}
return $filename;
}
// Directory containing the files
$directory = "POLICY/"; // Change this to your folder path
echo "<table>
<tr>
<th>Serial No.</th>
<th>Filename</th>
<th>File URL</th>
</tr>";
// Open the directory
if ($handle = opendir($directory)) {
$serialNumber = 1; // Initialize serial number
// Loop over all the files in the directory
while (false !== ($file = readdir($handle)))
{
// Skip the current and parent directory entries
if ($file != "." && $file != "..")
{
// Trim the extension from the filename
$trimmedFilename = trimExtension($file);
// Convert filename to uppercase
$upperFilename = strtoupper($trimmedFilename);
$fileUrl = $directory . '/' . $file;
echo "<tr>
<td>$serialNumber</td>
<td>$upperFilename</td>
<td><a href= "?>
<canvas id='the-canvas'></canvas>
<?php echo "Open File</a></td>
</tr>";
$serialNumber++; // Increment serial number
}
}
echo "</table>";
// Close the directory handle
closedir($handle);
} else {
echo "Failed to open directory.";
}
?>
</code>
<code><script src="https://mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script> <script type='module'> // If absolute URL from the remote server is provided, configure the CORS // header on that server. var url = <?php'$fileUrl';?> // Loaded via script> tag, create shortcut to access PDF.js exports. var { pdfjsLib } = globalThis; // The workerSrc property shall be specified. pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.mjs'; var pdfDoc = null, pageNum = 1, pageRendering = false, pageNumPending = null, scale = 0.8, canvas = document.getElementById('the-canvas'), ctx = canvas.getContext('2d'); /** * Get page info from document, resize canvas accordingly, and render page. * @param num Page number. */ function renderPage(num) { pageRendering = true; // Using promise to fetch the page pdfDoc.getPage(num).then(function(page) { var viewport = page.getViewport({scale: scale}); canvas.height = viewport.height; canvas.width = viewport.width; // Render PDF page into canvas context var renderContext = { canvasContext: ctx, viewport: viewport }; var renderTask = page.render(renderContext); // Wait for rendering to finish renderTask.promise.then(function() { pageRendering = false; if (pageNumPending !== null) { // New page rendering is pending renderPage(pageNumPending); pageNumPending = null; } }); }); // Update page counters document.getElementById('page_num').textContent = num; } /** * If another page rendering in progress, waits until the rendering is * finised. Otherwise, executes rendering immediately. */ function queueRenderPage(num) { if (pageRendering) { pageNumPending = num; } else { renderPage(num); } } /** * Displays previous page. */ function onPrevPage() { if (pageNum <= 1) { return; } pageNum--; queueRenderPage(pageNum); } document.getElementById('prev').addEventListener('click', onPrevPage); /** * Displays next page. */ function onNextPage() { if (pageNum >= pdfDoc.numPages) { return; } pageNum++; queueRenderPage(pageNum); } document.getElementById('next').addEventListener('click', onNextPage); /** * Asynchronously downloads PDF. */ pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) { pdfDoc = pdfDoc_; document.getElementById('page_count').textContent = pdfDoc.numPages; // Initial/first page rendering renderPage(pageNum); }); </script> <?php // Function to trim the extension from a filename function trimExtension($filename) { // Check if the filename has a dot indicating an extension if (strpos($filename, '.') !== false) { // Remove everything after and including the last dot $filename = substr($filename, 0, strrpos($filename, '.')); } return $filename; } // Directory containing the files $directory = "POLICY/"; // Change this to your folder path echo "<table> <tr> <th>Serial No.</th> <th>Filename</th> <th>File URL</th> </tr>"; // Open the directory if ($handle = opendir($directory)) { $serialNumber = 1; // Initialize serial number // Loop over all the files in the directory while (false !== ($file = readdir($handle))) { // Skip the current and parent directory entries if ($file != "." && $file != "..") { // Trim the extension from the filename $trimmedFilename = trimExtension($file); // Convert filename to uppercase $upperFilename = strtoupper($trimmedFilename); $fileUrl = $directory . '/' . $file; echo "<tr> <td>$serialNumber</td> <td>$upperFilename</td> <td><a href= "?> <canvas id='the-canvas'></canvas> <?php echo "Open File</a></td> </tr>"; $serialNumber++; // Increment serial number } } echo "</table>"; // Close the directory handle closedir($handle); } else { echo "Failed to open directory."; } ?> </code>
<script src="https://mozilla.github.io/pdf.js/build/pdf.mjs" type="module"></script>
    <script type='module'>
                      // If absolute URL from the remote server is provided, configure the CORS
                      // header on that server.
                      var url = <?php'$fileUrl';?>

                      // Loaded via script> tag, create shortcut to access PDF.js exports.
                      var { pdfjsLib } = globalThis;

                      // The workerSrc property shall be specified.
                      pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.mjs';

                      var pdfDoc = null,
                          pageNum = 1,
                          pageRendering = false,
                          pageNumPending = null,
                          scale = 0.8,
                          canvas = document.getElementById('the-canvas'),
                          ctx = canvas.getContext('2d');

                      /**
                       * Get page info from document, resize canvas accordingly, and render page.
                       * @param num Page number.
                       */
                      function renderPage(num) {
                        pageRendering = true;
                        // Using promise to fetch the page
                        pdfDoc.getPage(num).then(function(page) {
                          var viewport = page.getViewport({scale: scale});
                          canvas.height = viewport.height;
                          canvas.width = viewport.width;

                          // Render PDF page into canvas context
                          var renderContext = {
                            canvasContext: ctx,
                            viewport: viewport
                          };
                          var renderTask = page.render(renderContext);

                          // Wait for rendering to finish
                          renderTask.promise.then(function() {
                            pageRendering = false;
                            if (pageNumPending !== null) {
                              // New page rendering is pending
                              renderPage(pageNumPending);
                              pageNumPending = null;
                            }
                          });
                        });

                        // Update page counters
                        document.getElementById('page_num').textContent = num;
                      }

                      /**
                       * If another page rendering in progress, waits until the rendering is
                       * finised. Otherwise, executes rendering immediately.
                       */
                      function queueRenderPage(num) {
                        if (pageRendering) {
                          pageNumPending = num;
                        } else {
                          renderPage(num);
                        }
                      }

                      /**
                       * Displays previous page.
                       */
                      function onPrevPage() {
                        if (pageNum <= 1) {
                          return;
                        }
                        pageNum--;
                        queueRenderPage(pageNum);
                      }
                      document.getElementById('prev').addEventListener('click', onPrevPage);

                      /**
                       * Displays next page.
                       */
                      function onNextPage() {
                        if (pageNum >= pdfDoc.numPages) {
                          return;
                        }
                        pageNum++;
                        queueRenderPage(pageNum);
                      }
                      document.getElementById('next').addEventListener('click', onNextPage);

                      /**
                       * Asynchronously downloads PDF.
                       */
                      pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
                        pdfDoc = pdfDoc_;
                        document.getElementById('page_count').textContent = pdfDoc.numPages;

                        // Initial/first page rendering
                        renderPage(pageNum);
                      });
                    </script>
  
<?php
// Function to trim the extension from a filename
function trimExtension($filename) {
    // Check if the filename has a dot indicating an extension
    if (strpos($filename, '.') !== false) {
        // Remove everything after and including the last dot
        $filename = substr($filename, 0, strrpos($filename, '.'));
    }
    return $filename;
}

// Directory containing the files
$directory = "POLICY/"; // Change this to your folder path

echo "<table>
        <tr>
            <th>Serial No.</th>
            <th>Filename</th>
            <th>File URL</th>
        </tr>";

// Open the directory
if ($handle = opendir($directory)) {
    $serialNumber = 1; // Initialize serial number
    // Loop over all the files in the directory
    while (false !== ($file = readdir($handle))) 
    {
        // Skip the current and parent directory entries
        if ($file != "." && $file != "..") 
        {
            // Trim the extension from the filename
            $trimmedFilename = trimExtension($file);
            // Convert filename to uppercase
            $upperFilename = strtoupper($trimmedFilename);
            $fileUrl = $directory . '/' . $file;
            echo "<tr>
                    <td>$serialNumber</td>
                    <td>$upperFilename</td>
                    <td><a href= "?> 
                    <canvas id='the-canvas'></canvas>

                    
                    
                    <?php echo "Open File</a></td>
                  </tr>";
            $serialNumber++; // Increment serial number
        }
    }

    echo "</table>";

    // Close the directory handle
    closedir($handle);
} else {
    echo "Failed to open directory.";
}
?>

I want open file link option working. As while clicking on open file option it redirects to itself or redirects to something canvas. I tried using button click or renderpdf function but that is also not working. I have write these code to open pdf file in only view option not to download or print option enabled using java scrip.
Someone expert in php codes please help as soon as possible.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật