Jump to content

PDF file upload to MySQL


mxcnlink

Recommended Posts

Hi everyone, I've been stuck with this problem for a while now, and I really can't figure out what's going on; the script is simple: get a file from the user through the web form, upload the file to whatever the tmp directory is, read the file, insert the contents into the database...

 

Here is the script code  [upload.php]

 

<?php

if(isset($_POST[go])) {
    if(!empty($_FILES['pdf_file']['name']) && $_FILES['pdf_file']['type'] == "application/pdf") {
        $file_name = $_FILES['pdf_file']['tmp_name'];
        $instr = fopen($file_name, "rb");
        $file_image = mysql_real_escape_string(fread($instr,filesize($file_name)));

        $connect    = @mysql_connect("localhost", "XlocalYuserZ", "UsersPassWord");
        $select_db  = @mysql_select_db("DbName", $connect);
        $query = mysql_query("INSERT INTO files_tbl (pdf_file) VALUES('" . $file_image ."')");

        fclose($instr);
    } else {
?>
<p>Not a valid file!</p>
<?php
    }
?>
<p>File uploaded correctly.</p>
<?php
}
?>
<div style="width:735px">
<form method="post" action="upload.php" class="page_form" enctype="multipart/form-data">
    <fieldset><legend>Upload file</legend>
        <table border="0" cellpadding="3" cellspacing="1" style="width:700px;">
            <tr>
                <td class="left_middle"><label for="pdf_file">PDF File to upload:</label></td>
                <td class="right_middle"><input type="file" name="pdf_file" id="pdf_file" class="txt_common" /></td>
            </tr>
            <tr>
                <td colspan="2" class="right_middle"><input type="submit" name="submit" value="Upload to Database" class="button" /></td>
            </tr>
        </table>
        <input type="hidden" name="go" value="1" />
    </fieldset>
</form>
</div>

 

The code is pretty straight-forward, as you can see; it does not work however and I've been unable to find out why. It seems to upload some times, some others not, which is weird, but it might just be the server messing around with me... hehe... I have already granted 775 permissions on this PHP file.

 

All the script does is hang forever, it happens with any web browser, no error messages, nothing...

 

I hope some of you guys can give me a hand; I've been using Google in order to find related problems but it's not been enough... maybe apache permissions? .htaccess files? I truly don't know what to try.

 

Regards,

 

-

Javier

Link to comment
Share on other sites

The code is pretty straight-forward
It is too straight-forward, while it is checking for some of the possible errors (all at once so you don't know which one is failing) it is not checking for any where near all the possible errors. See this link for more error checking and error reporting ideas - http://www.phpfreaks.com/forums/index.php/topic,278952.msg1320835.html#msg1320835

 

Edit: and please remove the @ from in front of your mysql_ functions. Don't you suppose that if you were getting errors when you called those functions that you would want to know about them as they could be relevant to why your code is not doing what you expect? You should NEVER put @ in code to suppress errors.

 

Link to comment
Share on other sites

Thanks for the follow-up!  I'm taking a look at your suggestions now. Regarding the "@" in the mysql statements: that's just because of a copy/paste from a production script, and for sampling the problem here... the script never does reach that far anyways, and I do have it without the "@" right now.

 

I appreciate your help. I'll post again with my results.

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.