我正在尝试通过PHP中的表单上传文件。不幸的是文件上传不起作用。似乎未选择文件。下面是我的代码。我在下面复制了原始代码。
<html>
<body>
<table>
<form action="submit.php" method="POST" enctype="multipart/form-data">
<tr>
<td><label>Resume</label></td>
<td><input type="file" name="cv" id="cv"></td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td colspan="2"><button type="submit">Submit</button></td>
</tr>
</form>
</table>
</body>
submit.php
<?php
if(isset($_POST['cv']))
{
$filename = $_FILES["cv"]["name"];
echo $filename;
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$allowed_file_types = array('.doc','.docx','.rtf','.pdf');
// Rename file
$newfilename = $file_basename.$random.'_cv'. $file_ext;
$filepath = "resume/" . $newfilename;
if(move_uploaded_file($_FILES["cv"]["tmp_name"], $filepath))
{
echo "moved";
}
else
{
echo "not moved";
}
}
else
{
echo "no file";
}
?>
每当我运行此代码时,总是会收到错误no file
。
任何人都可以帮助我找到我要去哪里的地方。
编辑1
我已经更新了合并评论的答案的代码。
submit.php
<?php
if(!empty($_FILES['cv']))
{
$filename = $_FILES["cv"]["name"];
echo $filename;
// $random = substr(number_format(time() * rand(),0,'',''),0,5);
// $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
// $file_ext = substr($filename, strripos($filename, '.')); // get file name
// $allowed_file_types = array('.doc','.docx','.rtf','.pdf');
// // Rename file
// $newfilename = $file_basename.$random.'_cv'. $file_ext;
// $filepath = "resume/" . $newfilename;
// if(move_uploaded_file($_FILES["cv"]["tmp_name"], $filepath))
// {
// echo "moved";
// }
// else
// {
// echo "not moved";
// }
}
else
{
echo "no file";
}
?>
现在,我只是在注释文件移动代码,并试图获取文件名。它仍然显示no file
错误
答案 0 :(得分:0)
您正在使用POST
进行提交,但是您需要测试文件字段是否存在并且不为空-因此本质上您要测试isset( $_FILES['cv'] )
等〜或可以使用!empty()
进行测试如果已设置并且其限定值不等于零或null等
<?php
$random=uniqid();
$status=false;
if( !empty( $_FILES['cv'] ) ){
$obj=(object)$_FILES['cv'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$ext=strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
$allowed=array('doc','docx','rtf','pdf');
if( is_uploaded_file( $tmp ) && in_array( $ext, $allowed ) ){
$filename=basename( $name ) . $random . '_cv.' . $ext;
$filepath='resume/' . $filename;
$status=move_uploaded_file( $tmp, $filepath );
}
echo $status ? 'moved' : 'not moved';
} else {
echo 'bogus';
}
?>
一个有效的例子
<!DOCTYPE html>
<html>
<head>
<title>PHP: Upload resumé</title>
<meta charset='utf-8' />
</head>
<body>
<form method='post' enctype='multipart/form-data'>
<input type='file' name='cv' />
<input type='submit' />
</form>
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$directory='c:/temp/resume';
if( !file_exists( $directory ) ) mkdir( $directory, 0777, true );
$random=uniqid();
$status=false;
if( !empty( $_FILES['cv'] ) ){
$obj=(object)$_FILES['cv'];
$name=$obj->name;
$tmp=$obj->tmp_name;
$ext=strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
$allowed=array('doc','docx','rtf','pdf');
if( is_uploaded_file( $tmp ) && in_array( $ext, $allowed ) ){
$filename=basename( $name ) . $random . '_cv.' . $ext;
$filepath=$directory . '/' . $filename;
$status=move_uploaded_file( $tmp, $filepath );
}
echo $status ? 'moved' : 'not moved';
}
}
?>
</body>
</html>
答案 1 :(得分:0)
只需更改Submit.php文件
if (isset($_FILES['cv']['tmp_name']) && !empty($_FILES['cv']['tmp_name'])) {
$random = rand(11111, 999999);
$filename = $_FILES["cv"]["name"];
//echo $filename;
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$allowed_file_types = array('.doc','.docx','.rtf','.pdf');
// Rename file
$newfilename = $file_basename.$random.'_cv'. $file_ext;
$filepath = "resume/" . $newfilename;
if(in_array($file_ext, $allowed_file_types)){
if(move_uploaded_file($_FILES["cv"]["tmp_name"], $filepath))
{
echo "moved";
}
else
{
echo "Not moved";
}
}else{
echo "File type invalid";
}
}
else
{
echo "no file";
}
完成
答案 2 :(得分:0)
您应该首先查找可能的错误,如下所示:
if (UPLOAD_ERR_OK == $_FILES["cv"]["error"]) {
$tmp_name = $_FILES["cv"]["tmp_name"];
$real_name = basename($_FILES["cv"]["name"]);
move_uploaded_file($tmp_name, "resume/{$real_name}");
} else {
// process the file upload errors here
var_dump($_FILES["cv"]["error"]);
}
寻找错误。例如,可能有UPLOAD_MAX_FILESIZE或POST_MAX_SIZE限制(请参见here)。