Could anyone help me with code for a simple PDF file uploader in PHP I want to upload to a file on a server. Ive tried to write one several times and I simply can't get it to work!

Member Avatar for diafol

How is this different from a form file field?

Show us your best effort.

Thats my best effort, it returns sucess. However the file is not uploading!

<form action="../_cms/upload_pdf2.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit" class="upload-button"/>

    $errors= array();
    $file_name = $_FILES['image']['name'];
    $file_size =$_FILES['image']['size'];
    $file_tmp =$_FILES['image']['tmp_name'];
    $extensions = array("jpeg","jpg","png","pdf");      

    if(in_array($file_ext,$extensions )=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";

    if($file_size > 2097152){
        $errors[]='File size must be excately 2 MB';

        echo "Success";
Member Avatar for diafol

Are you sure that you're uploading to the right directory? ../_cms/upload_pdf2.php

So the form is not sending to itself? Is this php code in the same page as the form? Confused.
Can you show your directory structure and where the relevant files are? Thanks

I have it working now. The file path was wrong no the code for the form is in upload.php and the upload is in upload_pdf2.php

I have the uploader working however I want to print the file name and path on the page. Is there a way of doing this using PHP?

Member Avatar for diafol

Yes of course. When you do the upload routine it depends what happens thereafter.
If the upload routine redirects to a new page after successful upload, the easiest way would be to store the filename in a session variable.

$path = '../pdf';
    //echo "Success";
    $_SESSION['last_upload'] = $path . $filename;
    $_SESSION['upload_error'] = $errors;
header("Location: newpage.php");

Then in newpage.php:

$uploadtext = '';

    $uploadtext = $_SESSION['last_upload'];
    $uploadtext = $_SESSION['upload_error'];

//At the appropriate place

echo $uploadtext;

It might need a little bit of work, but I hope you get the idea.

Yes the upload form is in upload.php when they click the upload button the upload_pdf2.php is loaded up. So should I put the first section with my form in upload.php then the second section in upload_pdf2.php? I understand how it works, however calling that header breaks my header Ive already called. What do you think would be the solution to this. Thankyou for your help by the way!

Member Avatar for diafol

however calling that header breaks my header Ive already called

You didn't show that in your code. So I was unaware of it.

My approach would be (my naming):

contains the form with action pointing to formhandler.php and the session variable stuff (setting and displaying $uploadtext) noted in the second snippet of my previous post.

moves uploaded file to location and writes the path/file to DB and to session variable (or the error msgs). Redirect back to form.php

That's pretty much it. Let formhandler.php do all the heavy lifting with regard to files and DBs (like a "controller/model"). form.php can be thought of just presentation or a "view".

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.