我花了3天阅读和做有关PHP上传脚本的教程上传PDF文件到一个网站,重命名和移动的文件。对于如何格式化脚本,我似乎找不到一致的答案。从我的阅读来看,以下内容对这一过程非常重要。我还没有找到一个好的工作答案PDF上传问题。
下面是我使用的表单:
<form action ="PHP UPLOAD.php" method="post" encrypte="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="upload!">
</form>这是我从一个在线教程开始使用的PHP。
<?php
$uploaddir = "cover";
$allowed_ext = "pdf";
$max_size = "5000000";
//$max_height = "";
//$max_width = "";
$extension = pathinfo($_FILES['file'] ['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths [$i] == "$extension") {
$ok = "1";
}
}
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File is too big!";
exit;
}
// Include for images
//if ($max_width && $max_height) {
//list ($width, $height, $type, $w) =
//getimagesize($_FILES['file']['tmp_name']);
//if ($width . $max_width || $height > $max_height)
//{
//print "File height and/or width are too big!";
//exit;
//}
//}
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your Cover Letter has been successfully uploaded!";
} else {
print "Incorrect file extension!";
}
?>它向我返回了一条无效的文件类型消息。
我发现了这段代码,它似乎是一个更好的验证通过Mime,但似乎是不完整的。
$allowedExts = array(
"pdf",
"doc",
"docx"
);
$allowedMimeTypes = array(
'application/msword',
'text/pdf',
'image/gif',
'image/jpeg',
'image/png'
);
$extension = end(explode(".", $_FILES["file"]["name"]));
if ( 20000 < $_FILES["file"]["size"] ) {
die( 'Please provide a smaller file [E/1].' );
}
if ( ! ( in_array($extension, $allowedExts ) ) ) {
die( 'Please provide another file type [E/2]. );
}
if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) )
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}
else
{
die( 'Please provide another file type [E/3]. );
}我正在努力学习,但我似乎无法找到一条非常清晰的道路来解决我的问题。有人能给我一些明确的答案,什么是特别的PDF上传和这个过程?
作为背景,我使用MAMP和Dreamweaver & Text Wrangler在我的MAC上构建这个。我将在一个GoDAddy站点上实现这一点。所以,如果你知道我将要面对的问题,我会很感激你提醒我。
提前谢谢!
发布于 2012-10-08 13:46:27
我认为错误信息是因为您错过了键入的enctype加密,请也检查一下。
<form action ="PHP_UPLOAD.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="file" size="30">
<input type="submit" value="upload!">
</form>发布于 2017-10-02 00:00:38
嗨,第二个代码,通过Mime进行验证,但似乎不是这样完成工程。
$allowedExts = array(
"pdf",
"doc",
"docx"
);
$allowedMimeTypes = array(
'application/msword',
'application/pdf',
'text/pdf',
'image/gif',
'image/jpeg',
'image/png'
);
$nameexploded = explode(".", $_FILES["file"]["name"]);
$extension = end($nameexploded);
if ( 9000000 < $_FILES["file"]["size"] ) {
die( 'Please provide a smaller file [E/1].' );
}
if ( ! ( in_array($extension, $allowedExts ) ) ) {
die( 'Please provide another file type [E/2].' );
}
if ( in_array( $_FILES["file"]["type"], $allowedMimeTypes ) )
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}
else
{
die( 'Please provide another file type [E/3].' );
}我使用$nameexploded来避免:注意:在第16行的filepath\test.php中,只应该通过引用传递变量。
这对我很有用。希望能帮助别人。
https://stackoverflow.com/questions/12782980
复制相似问题