PHP/MySQL PDF upload
I'm using the code below to add/edit pdf's yet it doesn't seem to be working at all. Any ideas/help greatly appreciated. S
if (is_uploaded_file($_FILES['pdfFile']['tmp_name'])) {
$format = strtolower(substr(strrchr($_FILES['pdfFile']['name'],"."),1));
$str = strtolower(trim($_FILES['pdfFile']['name']));
$str = preg_replace('/[^a-z0-9-]/', '-', $str);
$str = preg_replace('/-+/', "-", $str);
$pdfFileName=$str.'.'.$format;
$pdfUploadFile=$pdfFileDir.$pdfFileName;
$format!='pdf' ? $error='Invalid format uploaded for PDF File!开发者_高级运维<br />Please only upload files with the ".pdf" extension.' : NULL;
if (!$error && move_uploaded_file($_FILES['pdfFile']['tmp_name'], $pdfUploadFile)){
if($_POST['docFilename'] && file_exists($pdfFileDir.'/'.$_POST['docFilename'])) {unlink($pdfFileDir.'/'.$_POST['docFilename']);}
$_POST['docFilename']=$pdfFileName;
mysql_query("INSERT INTO table(docFilename, assoc_cat, assoc_object) VALUES('".$_POST['docFilename']."', '".$_POST['categoryID']."', '".$_POST['id']."')");
} else {
file_exists($pdfUploadFile) ? unlink($pdfUploadFile) : NULL;
!$error ? $error='The chosen PDF file failed to upload correctly.<br />Please try again, or attempt to upload an alternative PDF.' : NULL;
}
}
Make sure to specify proper settings for:
- file_uploads
- upload_max_filesize
- memory_limit
- max_execution_time
- post_max_size
See:
- How to optimize your PHP installation to handle large file uploads
Also make sure that:
- You have specified the
enctype="multipart"
in theform
- Check the files array with
print_r($_FILES);
精彩评论