Forum Moderators: coopster

Message Too Old, No Replies

PDF file upload errors via html form

html form upload of a PDF file via PHP

         

russkern

12:06 pm on Dec 9, 2007 (gmt 0)

10+ Year Member



Is there something that needs to be done differently to upload a PDF file to a server as opposed to uploading an image file?

My script simply wants to re-name and upload a small PDF file to the server for storage. it then writes the user info and file name to a MySQL database for retrieval later.

In my error reporting I get a "File could not be uploaded because:
A system error occured."

But then the user info writes to the database (which it shouldn't if there is an upload error but that is a different problem and one I will tackle later).

I am using a slightly modified script that I use for image uploads aqll the time.

My upload directory name is set and it is writeable.

Thanks in advance.
R

dreamcatcher

12:39 am on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If its an image upload system originally, is it set to only allow images?

dc

russkern

2:15 am on Dec 10, 2007 (gmt 0)

10+ Year Member



No... I changed it to allow "application/pdf"

russkern

1:55 pm on Dec 10, 2007 (gmt 0)

10+ Year Member



If it helps.. the section of code that pertains is below:

$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

if (empty($errors)) {

$allowed = array ('application/pdf');
if(in_array($_FILES['upload']['type'], $allowed)) {

$upload_dir = "resumes/";
$newname = $fname."_".$lname.".".$file_ext;
$filename = $upload_dir.$newname;

if(move_uploaded_file($_FILES['upload']['tmp_name'], $filename)) {
chmod($filename, 0644);

$content .="<p>Your resume was uploaded.</p>\n";

} else {
$content .='<p><font color="red">File could not be uploaded because:<b></p>';

switch ($_FILES['upload']['error']) {
case 1:
$content .='<p>The file exceedes the upload_max_filesize setting in php.ini.</p></font>';
break;

case 2:
$content .='<p>The file exceeded the MAX_FILE_SIZE setting in the HTML form.</p></font>';
break;
case 3:
$content .='<p>The file was only partially uploaded.</p></font>';
break;
case 4:
$content .='<p>No file was uploaded.</p></font>';
break;
case 6:
$content .='<p>No temporary folder available.</p></font>';
break;
default:
$content .='<p>A system error occured.</p></font>';
break;
} //end of Switch

}//end of if move uploaded file

}//end of Allowed
else {
$content .="We currently only allow PDF files for upload.";
}

russkern

3:38 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Does anyone have any suggestions as to what may be wrong here? I've been over this repeatedly and can find no reason for the upload to fail.

The file size is small, The destination directory is writeable, My form encoding is "multipart/form-data.

Any suggestions as to where to look would be appreciated.

Russ

gergoe

3:52 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



The reason is that move_uploaded_file fails for a (probably good) reason, only need to find out why. Try opening the destination file ($filename) with fopen for reading, put something in it and close the handle, see if it succeeds. Or better to start with a simpler approach, echo the content of the $filename variable, and double-check that it exists (from the directory of your script).

phranque

3:56 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what is the value of $_FILES['upload']['error'] and what does the file upload error code [php.net] mean?

have you check your server error log for clues?

you might also compare your php.ini settings for "post_max_size" and "upload_max_size" with the actual file size.

russkern

4:52 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



OK,

I've tried the things you have suggested....even added 2 new error codes based in the error codes link that i didn't have, It still returns the default - a system error occured...

I can write a file to that directory via FTP, so I know it's there... and I can read it back out.

File size shouldn't be an issue, the test file is only 30k and I used to allow 10 Mb video uploads on this server.

Error log turned up nothing...

I'm at a loss...Maybe I'll re-write the script from scratch... or look elsewhere in the script...

Any other thoughts would be helpful, Thanks a bunch for the input you have given. I appreciate it.

phranque

6:37 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what is the value of $_FILES['upload']['error']?

gergoe

7:09 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Writing a file with FTP and writing a file from php is not the same. What I wanted to make sure that the php can write a file using the value of the $filename - whatever is in there.

[edit]I just realized now that my previous post was wrong, I meant open the file using the filename from $filename for writing - not reading[/edit]

russkern

7:40 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Phranque,

The error that is reported is the "default" error from my script above - A value (1,2,3,4,6,7 or 8) is not passed. The default is just sort of a catch-all and, as a result, the least helpful of all the error messages I report.

Gergoe,
When the file is re-named, I use the First Name and the last name from my form, seperated by an underscore. (i.e. russ_kern.pdf) so is what you are asking is can php write that file name to the directory on the server?

I'm not sure how to do what you are describing with fopen. I've been learning php for a while, but haven't used that particular function yet.

One thing I can tell you, is that the correct $filename does get written to my database.

Sorry... think I'm showing a bit of my "green-ness" here.

Russ

gergoe

7:58 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



Put the following just before move_uploaded_file, and post the results here:

echo '<pre>'; 
ini_set('display_errors', 1);
error_reporting(E_ALL);
echo "Dump the contents of \$_FILES\n";
var_dump($_FILES);
echo "Try to open target file for writing, and write 4 kb of data in it\n";
$h = fopen($filename, 'w');
if ($h!== false) {
var_dump(fwrite($h, str_repeat("TEST", 1024)));
fclose($h);
} else echo "Can not open {$filename} for writing\n";
echo "Try to move uploaded file to target\n";
var_dump(move_uploaded_file($_FILES['upload']['tmp_name'], $filename));
echo '</pre>';

This piece of code will in the first instance make sure you will see all runtime errors, then shows that the file has been successfully uploaded, the target file is writable (ever checked the permissions in that directory? The ftp user and the user running php is not the same, you might only ran into a permission issue) and the actual result of move_uploaded_file

russkern

8:44 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



And the output is:

Dump the contents of $_FILES
array(1) {
["upload"]=>
array(5) {
["name"]=>
string(11) "XOneXDA.pdf"
["type"]=>
string(15) "application/pdf"
["tmp_name"]=>
string(14) "/tmp/phpP3oMGP"
["error"]=>
int(0)
["size"]=>
int(38005)
}
}
Try to open target file for writing, and write 4 kb of data in it

Warning: fopen() [function.fopen]: SAFE MODE Restriction in effect. The script whose uid is 10006 is not allowed to access /var/www/vhosts/example.com/subdomains/dev/httpdocs/resumes owned by uid 0 in /var/www/vhosts/example.com/subdomains/dev/httpdocs/resume_upload.php on line 74

Warning: fopen(resumes/sss_sss.pdf) [function.fopen]: failed to open stream: No such file or directory in /var/www/vhosts/example.com/subdomains/dev/httpdocs/resume_upload.php on line 74

Can not open resumes/sss_sss.pdf for writing
Try to move uploaded file to target

Warning: move_uploaded_file() [function.move-uploaded-file]: SAFE MODE Restriction in effect. The script whose uid is 10006 is not allowed to access /var/www/vhosts/example.com/subdomains/dev/httpdocs/resumes owned by uid 0 in /var/www/vhosts/example.com/subdomains/dev/httpdocs/resume_upload.php on line 80

bool(false)

Warning: move_uploaded_file() [function.move-uploaded-file]: SAFE MODE Restriction in effect. The script whose uid is 10006 is not allowed to access /var/www/vhosts/example.com/subdomains/dev/httpdocs/resumes owned by uid 0 in /var/www/vhosts/example.com/subdomains/dev/httpdocs/resume_upload.php on line 84

Notice: Undefined variable: content in /var/www/vhosts/example.com/subdomains/dev/httpdocs/resume_upload.php on line 90

[edited by: eelixduppy at 9:00 pm (utc) on Dec. 11, 2007]
[edit reason] example.com [/edit]

phranque

8:52 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



A value (1,2,3,4,6,7 or 8) is not passed.

a value has certainly been passed.
you have only tested that it is not in that set of 8 integers.
is it 0?
greater than 8?
a negative number?
perhaps a floating point.
echo [php.net] is your friend here - you want to see the value of the error no matter what it is.

i'm still interested if you run out of other things to try...

phranque

8:57 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



SAFE MODE Restriction in effect.

to what directory were you uploading the image files?
check permissions and ownership there vs the resumes directory.

russkern

9:06 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



I'm developing in a subdomain (dev.mydomain.com)

I based this script off of an image upload script that lives in the domain itsself (www.mydomain.com/admin) and that script works fine... so maybe I should start looking at that...

Thanks for the help again...I learn a ton more from the things that don't go right than from the things that do. :) ...

Russ

gergoe

11:25 pm on Dec 11, 2007 (gmt 0)

10+ Year Member



The problem is not in the script you wrote, that would work perfectly, but rather in the ownership of your directory - as it was mentioned by phranque already.

As the error message states, the owner of the resumes directory (uid 0 - that's system I think?) is not the same as the user running the php process (probably that's your user). Try removing the directory with ftp, and creating it again, if that fails and you have SSH access, log in with SSH, and create the directory there. If you can not remove the directory (I'm not really good in *nix style permissions), then you may need contact support, or choose a different name for your directory.

You can read more about the behavior of php when set to work in safe_mode here: [php.net ].

russkern

12:13 am on Dec 12, 2007 (gmt 0)

10+ Year Member



Well would you look at that...Deleted the directory, re-created it, re-applied permissions and Violá... a perfectly successful file upload....

Of all the simple things....

Thank you all so much for your help...

Russ