Jump to content

[SOLVED] pdf problem with PHP?


Jago6060

Recommended Posts

I have a website for a client where they can upload a newletter.  I have PHP pull the directory info and put it into a link so the newsletter can be opened from the website.  Here's the problem...when you try to open it, adobe opens and gives an error message saying "The document is damaged and cannot be repaired".

 

So what I'm looking for is if someone knows how to fix this error message, or can give me an alternative like using PHP to auto convert a pdf to doc or something.  Having the client save things in an alternate format isn't an option unfortunately, boss' orders.

Link to comment
Share on other sites

PDF is the format you should stick with.

 

-Does the PDF file open fine on your PC before uploading?

-Once uploaded, can you go to the URL directly and have it open properly?

-What is your code you use for uploading the PDF?

Link to comment
Share on other sites

I can have the client email the pdf to me and open it fine.  Grab it from email and upload it.  That works fine.  But if I click the link to view it in the browser, it gives me the aforementioned error.  It does the same if I do a Save As...

 

Here is the code...

 

<?
session_start();

if (!isset($_SESSION[user])){
    header ('Location: index.php');
}

include 'connect.php';

$month = $_POST[month];
$day = $_POST[day];

// Where the file is going to be placed 
$target_path = "./uploads/news/";

/* Add the original filename to our target path.  
Result is "/uploads/news/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name'];  

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    @mysql_select_db($database) or die( "Unable to select database");

    $delete_record = "DELETE FROM crc_news WHERE news_id=1";
    $delete_record_result=mysql_query($delete_record);

    addcslashes($target_path, ' ');
    $sql = "INSERT INTO crc_news (news_id,news_dir,month,day)values('1','$target_path','$month','$day')";
    $result=mysql_query($sql);

    mysql_close();
    header ('Location: logout.php');
}else{
    echo "There was an error uploading the file, please try again!";
    echo "<a href=upload_news_form.php>Back</a>";
}
?>
</body>
</html>

Link to comment
Share on other sites

For debugging purposes, try uploading a working/good file through FTP to the same location. If the file then opens correctly you know that the problem is (almost) definitely with the script upload process.

 

Edit: modified my response slightly. I shouldn't use absolutes, lest I feel like a fool later.

Link to comment
Share on other sites

Ok now nothing will upload.  The "if" statement fails everytime.  This has happened before with other scripts used on the site.  They just stop working.  I'm the only person with access to the files btw.

 

Is it possible that its an issue with the service provider?

Link to comment
Share on other sites

For debugging purposes, try uploading a working/good file through FTP to the same location. If the file then opens correctly you know that the problem is (almost) definitely with the script upload process.

 

Edit: modified my response slightly. I shouldn't use absolutes, lest I feel like a fool later.

 

Here's the update on your suggestion.  I can grab the pdf from my email and put it on my desktop--opens fine.  I can upload it to the host provider manually, but cannot open it once its there.  This would point to the host provider being a problem right?

Link to comment
Share on other sites

Have you tried to perform a readfile() on the file and provide your own headers. Then it does not matter if the provider supports that file type or not. So long as they are not corrupting the file. If that is the case can you FTP the file back to your computer and still open it successfully?

Link to comment
Share on other sites

Have you tried to perform a readfile() on the file and provide your own headers. Then it does not matter if the provider supports that file type or not. So long as they are not corrupting the file. If that is the case can you FTP the file back to your computer and still open it successfully?

 

I'm a novice with PHP so I'm not sure what readfile() is.  That fact aside, I ftp-ed in and grabbed the file from the host provider, put it on my desktop, and it encounters the error.  It seems to be an issue that arises out of uploading to the host provider.  I don't really understand why because this has worked in the past, its only been the past few weeks that its stopped working.  The code hasn't changed.

 

P.S. A brief explanation of readfile() would be appreciated.

Link to comment
Share on other sites

Well sounds like your provider is corrupting the data. But you might want to make sure that your connection to there server is using the right encoding like UTF-8. What uploader are you using? If you are not using you might want to try Filezilla, then you can see it communicate with the server to ensure correct encoding.

readfile() reads a file and outputs it directly to the output buffer. Thus you can have a user download a file with out ever having direct access to it.

http://www.php.net/manual/en/function.readfile.php

Link to comment
Share on other sites

Well sounds like your provider is corrupting the data. But you might want to make sure that your connection to there server is using the right encoding like UTF-8. What uploader are you using? If you are not using you might want to try Filezilla, then you can see it communicate with the server to ensure correct encoding.

readfile() reads a file and outputs it directly to the output buffer. Thus you can have a user download a file with out ever having direct access to it.

http://www.php.net/manual/en/function.readfile.php

 

I used the host service's provided uploader.  I didn't actually ftp the file to them, just grabbed it via from them.  I'll try an ftp client and see if that makes a difference.

Link to comment
Share on other sites

Your last statement before bumping was

I used the host service's provided uploader.  I didn't actually ftp the file to them, just grabbed it via from them.  I'll try an ftp client and see if that makes a difference.

 

Did you try that?

 

Gave the direct ftp a shot, no dice...I used gFTP to do the transfer.  Ftp-ed the pdf over to the host service, tried opening up the pdf in the file manager and it gave the same error.  Mention was made to using UTF-8 encoding, I don't see anywhere in gFTP to change that, but there is a check box labeled "Transfer files in ASCII mode".  Will this fix the corruption issue?

Link to comment
Share on other sites

... there is a check box labeled "Transfer files in ASCII mode".  Will this fix the corruption issue?

 

:psychic:

The spirits say . . . "how the hell should we know?!"

 

Seriously though, it would take you how long to actually try it as opposed to posting on here to ask us if it will work? If the solution to your problem was patently obvious to us we would have said "change 'x' to 'y' to fix the problem". But, your problem is not an obvious one.

 

You will need to use critcal thinking skills to identify the root cause. Once you have that, then you can find a solution. You will need to come up with specific tests that will rule out opne thing vs. another. Hopefully someone will have had a similar issue will be able to piont you in a specific direction. But, as of right now I *think* we have rules out the PHP code as being the issue since you can't even upload a PDF file via FTP and have it work correctly. If I were you I would try the other transfer mode. If that doesn't work contanct your host provider as it seems like a problem on the server.

 

On a side note, have you verified that you are not over your storage limit?

Link to comment
Share on other sites

That was a way drawn out anwser, haha.  Tried the ASCII mode in gFTP and it worked.  So now my problem is, how do I apply that with PHP?

 

I don't really understand what gFTP is doing when it uses ASCII mode for the file transfer.  My thought would be that its changed the file encoding.  Is this a safe assumption?

 

P.S. The default transfer mode is Binary

Link to comment
Share on other sites

What encoding are you using on your form for the file upload? Post the form.

 

I didn't specify encoding in the script as it was working up until recently.

 

The form...

<?
session_start();

if (isset($_SESSION[user])){
echo "
<h2>Upload a Newsletter</h2>
<form enctype=multipart/form-data action=upload_news.php method=POST>
<input type=hidden name=MAX_FILE_SIZE value=2097152 />
Choose a file to upload: <input name=uploadedfile type=file /><br />
Select the corresponding month:
<select name=month>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select><br>
Please select a day:
<select name=day>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
<br>
<input type=submit value=Upload File />
</form>
";
}else{
header ('Location: index.php');
exit;
}
?>

 

The upload script...

<?
session_start();

if (!isset($_SESSION[user])){
    header ('Location: index.php');
}

include 'connect.php';

$month = $_POST[month];
$day = $_POST[day];

// Where the file is going to be placed 
$target_path = "./uploads/news/";

/* Add the original filename to our target path.  
Result is "/uploads/news/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name'];  

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    @mysql_select_db($database) or die( "Unable to select database");

    $delete_record = "DELETE FROM crc_news WHERE news_id=1";
    $delete_record_result=mysql_query($delete_record);

    addcslashes($target_path, ' ');
    $sql = "INSERT INTO crc_news (news_id,news_dir,month,day)values('1','$target_path','$month','$day')";
    $result=mysql_query($sql);

    mysql_close();
    header ('Location: logout.php');
}else{
    echo "There was an error uploading the file, please try again!<br>";
    echo "<a href=upload_news_form.php>Back</a>";
}
?>
</body>
</html>

 

P.S. I missed your question about storage limit earlier.  I'm not over the storage limit, and just for reference, the file size is 51KB

Link to comment
Share on other sites

I wqas referring to the enctype, which you have set.

 

I dont think this has anything to do with your problem, but is there any reason why you do not put any of yout HTML tag parameter values in quotes? That's really bad form.

 

In this line:

<input type=submit value=Upload File />

 

The value of that field is NOT interpreted as "Upload File". It will simply be passed as "Upload". Looking at the actual FORM fields I don't see any that would be affected by this type of problem. BUt, it would be best to have properly formatted HTML to begin with.

 

Plus, you are using short tags (i.e. <? ) which are not always supported and are discouraged. Also, I don't even see HTML opening and closing tags. Again, I don't think this has anything to do with your problem, just things I would expect on a properly created page. Here is a modified version of your page that corrects those errors and is more efficient

 

<?php

session_start();

if (!isset($_SESSION[user]))
{
    header ('Location: index.php');
    exit;
}
else
{
    $dateOptions = "";
    for($day=1; $day<=31; $day++)
    {
        $dateOptions .= "<option value=\"{$day}\">{$day}</option>\n";
    }
    $monthOptions = "";
    for($monNum=1; $monNum<=12; $monNum++)
    {
        $mon = date("F", mktime(0, 0, 0, $monNum+1, 0, 0, 0));
        $monthOptions .= "<option value=\"{$mon}\">{$mon}</option>\n";
    }
}
?>
<html>
<body>
<h2>Upload a Newsletter</h2>
<form enctype="multipart/form-data" action="upload_news.php" method="POST">
  <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
  Choose a file to upload: <input name="uploadedfile" type="file" /><br />
  Select the corresponding month:
  <select name="month">
  <?php echo $monthOptions; ?>
  </select><br>
  Please select a day:
  <select name="day">
    <?php echo $dateOptions; ?>
  </select>
  <br>
  <input type="submit" value="Upload File" />
</form>
</body>
</html>

Link to comment
Share on other sites

An upload with the updated code still gives the same error.  "Document is damaged and cannot be repaired".  I DO greatly appreciate the updated code you provided.  I guess I'll need to contact the host provider and see if they can tell me anything.  I'll be sure to update when I find out what's going on.

Link to comment
Share on other sites

I am throwing the B/S flag on that one. I can ftp, sftp, upload via form and dowload via form with out an issue any .pdf file and still open it with out any corruption on my server. Tell your provider you need better than that. Passing the blame is unacceptable. Other wise I would tell them if that is the best they have got, then it is time to switch hosts.

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.