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!

Recommended Answers

All 7 Replies

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"/>
</form>

<?php
if(isset($_FILES['image'])){
    $errors= array();
    $file_name = $_FILES['image']['name'];
    $file_size =$_FILES['image']['size'];
    $file_tmp =$_FILES['image']['tmp_name'];
    $file_type=$_FILES['image']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['image']['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';
    }

    if(empty($errors)==true){
        move_uploaded_file($file_tmp,"../pdf/".$file_name);
        echo "Success";
    }else{
        print_r($errors);
    }
}
?>        
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';
if(empty($errors)==true){
    move_uploaded_file($file_tmp,$path.$file_name);
    //echo "Success";
    $_SESSION['last_upload'] = $path . $filename;
}else{
    $_SESSION['upload_error'] = $errors;
}
header("Location: newpage.php");
exit;

Then in newpage.php:

session_start();
$uploadtext = '';

if(isset($_SESSION['last_upload']))
{
    $uploadtext = $_SESSION['last_upload'];
    unset($_SESSION['last_upload']);
}
if(isset($_SESSION['upload_error']))
{
    $uploadtext = $_SESSION['upload_error'];
    unset($_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):

form.php
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.

formhandler.php
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.